Pretty new to this...so please bear with me. I currently have a two push-button password circuit working... Press the correct button sequence (0, 1, 1, 1, 0), LEDs light up, etc... (I found a 4-button code, but modified it for 2 buttons).
what I'd like to do is modify my code to have 2 sequences to do something... for example:
type in first PIN code...light up LED and within 15 seconds, type in the 2nd PIN code and do something.
below is my code...Any help is appreciated!
#define RED 13 //red led
#define GREEN 12 //green led
#define BLUE 11 //blue led
#define VIBE 10 //vibe motor
const int pin[] = {0, 1, 1, 1, 0}; // The code to unlock
const int pinLength = sizeof(pin) / sizeof(int); // Length of the code
const int buttons[] = {3, 2}; // Arduino pins the buttons are connected to
const int buttonsLength = sizeof(buttons) / sizeof(int); // Number of buttons connected
bool pressed[buttonsLength]; // Which buttons were pressed pressed when last checked
int index = 0; // Index of current pin digit
bool correct = true; // If input matches pin
int attempt = 0;
//these will be used for button 1 prior to entering unlock PIN
int buttonState = 0;
int buttonPushCounter = 0;
int lastbuttonState = 0;
int randnum;
void setup() {
// Set LED and unlock pins to output
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(VIBE, OUTPUT);
// Set buttons to inputs
for (int i = 0; i < buttonsLength; ++i) {
pinMode(buttons*, INPUT);*
- }*
}
void loop() { - // digitalWrite(VIBE, !index); // Show blue LED if index is 0*
- // Loop buttons*
- for (int i = 0; i < buttonsLength; ++i) {*
- //If the button is pressed*
_ if (digitalRead(buttons*)) {_
_ if (pressed) return; // Ignore if already pressed*
pressed = true; // Set current button as pressed
* correct = correct && i == pin[index]; // If the pin doesn't match set correct to false*
* if (++index == pinLength) { // Increment digit index and check if last digit*
* if (correct) { // Unlock and show green LED if code is correct*
* digitalWrite(GREEN, HIGH);
delay(3000);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
delay(3000);
digitalWrite(BLUE, LOW);
attempt = 0; // Reset timer*
* //vibe motor setup:: if 3, vibe for 3x, other wise,vibrate for 3 sec.
randnum = random(1, 4);
Serial.println(randnum);
if ((randnum != 3))
{
Serial.println("SUCCESS");
for (int x = 0; x < 3; ++x)
{
digitalWrite(VIBE, HIGH);
delay(1000);
digitalWrite(VIBE, LOW);
delay(1000);
}//end blink 3 times after success*
* }//end of rand gen for vib motor*
* else*
* {
Serial.println("FAILURE");
digitalWrite(VIBE, HIGH);
delay(3000);
digitalWrite(VIBE, LOW);
}//end if not 3, then fail*
* } else { // The code is incorrect*
* attempt++; // Increase a timer*
* for (int j = 0; j < attempt; j++) { // Take longer if is not first attempt*
* for (int k = 0; k < 5; k++) { // Flash red LED*
* digitalWrite(RED, HIGH);
delay(100);
digitalWrite(RED, LOW);
delay(100);
}
}
}
// Reset for next attempt*
* index = 0;
correct = true;
}
} else { // Button is not pressed*
pressed = false; // Set current button as not pressed
* }
}
delay(10); // Run every 10ms*
}//end main loop_