passcode before execution of program

Hello! The codes below is what I want to happen. But before that, I need first to set a passcode by pressing buttons.
for example my passcode would be switches 1,4 and 3. Then it will proceed to run the below codes. If I entered incorrect password (132), then all LED’s will on then off. Then I can try another passcode. It has 6 switches, 1 for reset and 8 LED’s.
Please help me on the codes :smiley:
Thanks a lot!

int timer = 100;
int ledPins = {
13,12,11, 10, 9, 8, 7, 6};
int pinCount = 8;
void setup() {

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {

if (buttonState == HIGH) {
digitalWrite(ledPin, LOW);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);

}
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin–) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}

Put the button press detection in setup as a while loop:
void setup(){
pinMode (pinX, INPUT_PULLUP); // button with internal pullup resistor, connect to Gnd when pressed
//other setup stuff, like Serial.begin, SPI.begin, etc.
:
:
// last thing:
while (digitalRead(pinX) == HIGH){
// hang out waiting for a LOW
}
// LOW received, proceed
}

http://arduino.cc/en/Tutorial/ButtonStateChange

this should work.

int timer = 100;        
int ledPins[] = { 
  13,12,11, 10, 9, 8, 7, 6};     
int pinCount = 8;        
int button = 0;

long lastbutton=0;   

void setup() {
 
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
  
  
  pinMode(button, INPUT);   
}

void loop() {
  
  long currentbutton= digitalRead (button);   
  if(currentbutton!=lastbutton)  {
  if(currentbutton==HIGH){
    
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);    

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }    
  }
  }

  

}

Thanks guys :slight_smile: it's working.
If I add another 2 buttons what will I do?

Very last question. :smiley:

sharmane18:
Thanks guys :slight_smile: it's working.
If I add another 2 buttons what will I do?

Very last question. :smiley:

my project has 4 buttons. here is how I do it.

int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};
int bPress(int bPin){                                                             //button function
buttonState[bPin]=digitalRead(buttonPin[bPin]);

if(buttonState[bPin]!=lastButtonState[bPin]){
  lastButtonState[bPin]=buttonState[bPin];
  if(buttonState[bPin]==LOW){
    return 1;}
  else return 0;  
}      
}
int buttonTest1=bPress(1);
if(buttonTest1==0) mode++;

int buttonTest2=bPress(2);
if(buttonTest2==0)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

arduinomagbit:

sharmane18:
Thanks guys :slight_smile: it's working.
If I add another 2 buttons what will I do?

Very last question. :smiley:

my project has 4 buttons. here is how I do it.

int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4
int buttonState[5]={0,0,0,0,0};

int lastButtonState[5]={0,1,1,1,1};







int bPress(int bPin){                                                             //button function
buttonState[bPin]=digitalRead(buttonPin[bPin]);

if(buttonState[bPin]!=lastButtonState[bPin]){
  lastButtonState[bPin]=buttonState[bPin];
  if(buttonState[bPin]==LOW){
    return 1;}
  else return 0; 
}     
}







int buttonTest1=bPress(1);
if(buttonTest1==0) mode++;

int buttonTest2=bPress(2);
if(buttonTest2==0)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

THANKS a lot! Really big help :slight_smile:
It's also my project, my teacher also added, if any of the 5 random buttons he press, it will proceed to the main program.

Good day :smiley:

Hi guys, I need help in this matter :slight_smile: I need 3 passcode buttons before proceeding to the main program. Below are my codes. I had already made 1 passcode, I have trouble on where and how will I add another two passcodes. Please help :frowning: I really need this right now =(

int timer = 100;
int ledPins = {
13,12,11, 10, 9, 8, 7, 6};
int pinCount = 8;
int buttonPin = 0;
int ledPin =13;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, LOW);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin–) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}

sharmane18:
Hi guys, I need help in this matter :slight_smile: I need 3 passcode buttons before proceeding to the main program. Below are my codes. I had already made 1 passcode, I have trouble on where and how will I add another two passcodes. Please help :frowning: I really need this right now =(

int timer = 100;
int ledPins = {
13,12,11, 10, 9, 8, 7, 6};
int pinCount = 8;
int buttonPin = 0;
int ledPin =13;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, LOW);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin–) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}

sry not reply on time. i am a newbie too, normally I come here to ask for help. in your case about adding a pass-code. I suggest that you add 3 values to your code.

when one button is pressed, value1=1;
then if another button is pressed , if value1==1, value2=1; else value1=0; value2=0;
then if last button is pressed , if (value1==1 && value2==1) value3=1; else value1=0; value2=0; value3=0;

&& this means and; only if value1 and value2=1, value3 will equal 1

then last do this if(value1==1 && value2==1 && value3==1 ) then { go to the main program screen}

good luck