231a-ag arduino

From CSclasswiki
Jump to: navigation, search

Final Arduino Sketch

/*
 * test.pde
 * Alex Cheng (231a-ag)
 * 
 * This program sets the buzzer off, and lights up the LED matrix
 * in a 1, 2, 3 sequence.  Then, it waits for the user to input the 
 * correct sequence.  When the correct sequence is entered, then
 * the buzzer turns off.
 *
 */
int ledPin7 = 7;                   // all LED pins
int ledPin8 = 8;                   
int ledPin9 = 9;

int ledPin10 = 10;               
int ledPin11 = 11;
int ledPin12 = 12;

int buzzerPin = 13;                   // buzzer pin

int keypadDPin3 = 3;                   // digital pins
int keypadDPin4 = 4;
int keypadDPin5 = 5;

int keypadAPin0 = 0;                   // analog pins
int keypadAPin1 = 1;
int keypadAPin2 = 2;

int padNumber3 = 0;                   // keypad numbers
int padNumber1 = 0;
int padNumber5 = 0;
int padNumber2 = 0;
int padNumber7 = 0;
int padNumber6 = 0;

int lastKey = 0;                   // last key pressed
int firstKey = 0;                   // first key pressed
int secondKey = 0;                   // second key pressed
int thirdKey = 0;                   // third key pressed

// SETUP
void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin7, OUTPUT);         // sets outputs and inputs
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  
  pinMode(keypadDPin3, OUTPUT);
  pinMode(keypadDPin4, OUTPUT);
  pinMode(keypadDPin5, OUTPUT);
  
  pinMode(keypadAPin0, INPUT);
  pinMode(keypadAPin1, INPUT);
  pinMode(keypadAPin2, INPUT);
  
  Serial.begin(9600);                   // display

  delay(3000);                   // delay the initial flashing of the sequence
  digitalWrite(ledPin7, HIGH);                   // flash "1" on LED matrix
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, LOW);
  digitalWrite(ledPin10, HIGH);
  digitalWrite(ledPin11, LOW);
  digitalWrite(ledPin12, LOW);  
  delay(1000);
  digitalWrite(ledPin9, HIGH);
  digitalWrite(ledPin10, LOW);
      
  digitalWrite(ledPin7, HIGH);                   // flash "2" on LED matrix
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, LOW);
  digitalWrite(ledPin10, LOW);
  digitalWrite(ledPin11, HIGH);
  digitalWrite(ledPin12, LOW);
  delay(1000);
  digitalWrite(ledPin9, HIGH);
  digitalWrite(ledPin11, LOW);
       
  digitalWrite(ledPin7, HIGH);                   // flash "3" on LED matrix
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, LOW);
  digitalWrite(ledPin10, LOW);
  digitalWrite(ledPin11, LOW);
  digitalWrite(ledPin12, HIGH);  
  delay(1000);
  digitalWrite(ledPin9, HIGH);
  digitalWrite(ledPin12, LOW);
}

void loop() {                        // run over and over again
  digitalWrite(keypadDPin3, LOW);
  digitalWrite(keypadDPin4, LOW);
  digitalWrite(keypadDPin5, LOW);

  padNumber3 = analogRead(keypadAPin0);                   // read analog pins
  padNumber1 = analogRead(keypadAPin1);
  padNumber5 = analogRead(keypadAPin2);
 
  if (padNumber3 < 200) {
    digitalWrite(keypadDPin3, HIGH);
    digitalWrite(keypadDPin4, HIGH);
    digitalWrite(keypadDPin5, LOW);  // Is it 1?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    // KEY PRESSED IS 1
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin9, LOW);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, LOW);
      lastKey = 1;
    }
    
    digitalWrite(keypadDPin3, HIGH);
    digitalWrite(keypadDPin4, LOW);
    digitalWrite(keypadDPin5, HIGH);  // Is it 4?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    //  KEY PRESSED IS 4
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin8, LOW);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin10, LOW);
      lastKey = 4;
    }  
    
    digitalWrite(keypadDPin3, LOW);
    digitalWrite(keypadDPin4, HIGH);
    digitalWrite(keypadDPin5, HIGH);  // Is it 7?
    padNumber3 = analogRead(keypadAPin0);
  
    if (padNumber3 < 200) {    // KEY PRESSED IS 7
      digitalWrite(ledPin7, LOW);
      digitalWrite(ledPin8, HIGH);
      digitalWrite(ledPin9, HIGH);
      digitalWrite(ledPin10, HIGH);
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);  
      delay(100);
      digitalWrite(ledPin7, HIGH);
      digitalWrite(ledPin10, LOW);
      lastKey = 7;
    }
  } else if (padNumber1 < 200) {
     digitalWrite(keypadDPin3, HIGH);
     digitalWrite(keypadDPin4, HIGH);
     digitalWrite(keypadDPin5, LOW);  // Is it 2?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 2
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, LOW);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);
       delay(100);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin11, LOW);
       lastKey = 2;
     }
    
     digitalWrite(keypadDPin3, HIGH);
     digitalWrite(keypadDPin4, LOW);
     digitalWrite(keypadDPin5, HIGH);  // Is it 5?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 5
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, LOW);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);
       delay(100);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin11, LOW);
       lastKey = 5;
     }
       
     digitalWrite(keypadDPin3, LOW);
     digitalWrite(keypadDPin4, HIGH);
     digitalWrite(keypadDPin5, HIGH);  // Is it 8?
     padNumber1 = analogRead(keypadAPin1);
     
     if (padNumber1 < 200) {    // KEY PRESSED IS 8
       digitalWrite(ledPin7, LOW);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, HIGH);
       digitalWrite(ledPin12, LOW);  
       delay(100);
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin11, LOW);
       lastKey = 8;
     }
  } else if (padNumber5 < 200) {
      digitalWrite(keypadDPin3, HIGH);
      digitalWrite(keypadDPin4, HIGH);
      digitalWrite(keypadDPin5, LOW);  // Is it 3?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 3
       digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, HIGH);
       digitalWrite(ledPin9, LOW);
       digitalWrite(ledPin10, LOW);
       digitalWrite(ledPin11, LOW);
       digitalWrite(ledPin12, HIGH);  
       delay(100);
       digitalWrite(ledPin9, HIGH);
       digitalWrite(ledPin12, LOW);
       lastKey = 3;
      }
      
      digitalWrite(keypadDPin3, HIGH);
      digitalWrite(keypadDPin4, LOW);
      digitalWrite(keypadDPin5, HIGH);  // Is it 6?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 6
        digitalWrite(ledPin7, HIGH);
        digitalWrite(ledPin8, LOW);
        digitalWrite(ledPin9, HIGH);  
        digitalWrite(ledPin10, LOW);
        digitalWrite(ledPin11, LOW);
        digitalWrite(ledPin12, HIGH);
        delay(100);
        digitalWrite(ledPin8, HIGH);
        digitalWrite(ledPin12, LOW);
        lastKey = 6;
      }
      
      digitalWrite(keypadDPin3, LOW);
      digitalWrite(keypadDPin4, HIGH);
      digitalWrite(keypadDPin5, HIGH);  // Is it 9?
      padNumber5 = analogRead(keypadAPin2);
      
      if (padNumber5 < 200) {    // KEY PRESSED IS 9
        digitalWrite(ledPin7, LOW);
        digitalWrite(ledPin8, HIGH);
        digitalWrite(ledPin9, HIGH);
        digitalWrite(ledPin10, LOW);
        digitalWrite(ledPin11, LOW);
        digitalWrite(ledPin12, HIGH);
        delay(100);
        digitalWrite(ledPin7, HIGH);
        digitalWrite(ledPin12, LOW);
        lastKey = 9;
      }
  }
  //Serial.print(lastKey);
  if (firstKey == 0) {                   // check that the first key is still 0
    firstKey = lastKey;                   // if 0, set it to last key pressed
  } else if (secondKey == 0) {                   // check that the second key is still 0 
    if (lastKey != firstKey) {                   // check that it's not a copy of the first key
      secondKey = lastKey;                   // if not equal, set to last key pressed
    }
  } else if (thirdKey == 0) {                   // check that the third key is still 0
      if (lastKey != secondKey) {                   // check that it's not a copy of the second key
        thirdKey = lastKey;                   // if not equal, set to last key pressed
      }
  }
  Serial.print(firstKey);
  Serial.print(secondKey);
  Serial.print(thirdKey);
  if (firstKey == 1) {                   // if the first key is 1
    if (secondKey == 2) {                   // if the second key is 2
      if (thirdKey == 3) {                   // if the third key is 3
        digitalWrite(buzzerPin, HIGH);                   // then turn off buzzer, sequence is correct
      }
    }
  } else {                   
    digitalWrite(buzzerPin, LOW);                   // otherwise, keep buzzer on
  }
}