using a mini keypad password

i have like 3 buttons, and i want to pragram them to be clicked in a certain order, and if that order is correct, then i want the buzzer to god off, or an led lights up, i have a "pre" code, but i cant get it working. I got this code from a guy, and i had to edit it to use it with switches, so i probably messed it up, here, take a gander:

/* * Turn an led on when a special sequence of characters * (a password) is sent over the serial connection. */ int buzzer = 3; // buzzer for wrong answer int ledPin = 10; // the led is hoooked up to this pin int passChar1 = 1; // the first character of the password int passChar2 = 2; // the second character of the password int passChar3 = 3; // the third character of the password int nextChar; int charCount;

void setup() {

pinMode(ledPin, OUTPUT); pinMode(passChar1, INPUT); pinMode(passChar2, INPUT); pinMode(passChar3, INPUT); digitalWrite(ledPin, LOW); charCount = 0; pinMode(buzzer, OUTPUT); }

void loop(){ { nextChar = Serial.read();

if (charCount == 0) { // check for the first char of the pwd... if (nextChar == passChar1) { charCount = 1; // got it, so update the character counter } else { charCount = 0; // wrong character, so reset count to zero } } else if (charCount == 1) { // check for the second char of the pwd... if (nextChar == passChar2) { charCount = 2; // got it, so update the character counter } else { charCount = 0; // wrong character, so reset count to zero } } else if (charCount == 2) { // etc. if (nextChar == passChar3) { charCount = 3; } else { charCount = 0; }

// has complete password been entered yet? if (charCount == 3) {

// led on digitalWrite(ledPin, HIGH); charCount = 0; } else if (charCount == 0) { // count was set to zero, meaning a bad char was received.

// blink the led digitalWrite(buzzer, HIGH); delay(1000); digitalWrite(buzzer, LOW); delay(10); } }

}}

see if you guys can tell me whats wrong, or how to fix it, and to make life esier, lets use an led instead of a buzzer, cuz if a buzzer sais your right, it's annoying!

p.s how do i add the code box thing, i looked allover the page here, and i dont see it... thnks in advance -big92+1

Here is a fragment that I hope will get you started. It will need some work but I hope it points you in the right direction.

  #define   PASSCODE_LEN  3    // number of presses required 
  int buttonPins[] = {2,3,4};  // pins connected to buttons: 0 & 1 are the serial port so lets start from 2
  int passcode[PASSCODE_LEN] = {2,0,1};    // the correct sequence of buttons (in this example, btn 0 -> pin2, btn 1->3, btn 2 ->4)
  int hitCount = 0;            // nbr of correct button presses
  

void loop(){

    for(int btn = 0; btn < 3; btn++){
         if(  digitalRead(buttonPins[btn]) == HIGH)
               if( btn == passcode[hitCount] ){   // was the expected button pressed
                   hitCount++;            // yes, increment the count 
                   if( hitCount == PASSCODE_LEN){  // got all our presses?
                       passcodeValid(); // yes, call function to handle valid passcode sequence                                       
                       // you may want to reset the hitCount here to wait for another entry
                   }                                           
               }
               else{      // here if an incorrect button was pressed 
                  hitCount = 0;         // reset the count
                  passcodeInvalid() // function to handle invalid sequence 
               }                      
         
    }  
}

You will need to set the pins to inputs in setup and add delays to debounce the switches (see the debounce example in the IDE). And you need some code in the functions that handle valid and invalid entries. You may want to check that only one button is pressed at a time.

p.s. use the # symbol in the editing icons to insert code tags in your post, your code goes between the ‘code] and [/code’ tags

wow, that code looks great, thanks! i’m fixing it up right now, but 1 quick question,

when you put the pin numbers where i will be attaching the code, if i want to put more then one, i do what you did right, i do this {1,2,3} and that lets me add a cuple instead of adding them 1 at a time, but how do i differentiate between the int name?
becuase if i have

int blabla = {2,3,4}

then what is pin 2 called?
and what is pin 3, 4 called? are they called blabla2, blabla3, blabla4?

it’s tellingmy the the invalid passcode line wasnot delared in the scope! was i sapposed to add it manually? and what is it sapposed to equal number wize, 0?

lol sorry, i’m really bad at coding, i really dont get how to get this code workin, even though, i do understand the code in general.

heres what i have so far, ill show you the code and i’ll tell you exactly what my hardware is:

  #define   PASSCODE_LEN  3    // number of presses required 
  int buttonPins[] = {2,3,4};  // pins connected to buttons: 0 & 1 are the serial port so lets start from 2
  int passcode[PASSCODE_LEN] = {2,0,1};    // the correct sequence of buttons (in this example, btn 0 -> pin2, btn 1->3, btn 2 ->4)
  int hitCount = 0;            // nbr of correct button presses
  int buzzer = 7;
void setup(){
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);

}

void loop(){

    for(int btn = 0; btn < 3; btn++){
         if(  digitalRead(buttonPins[btn]) == HIGH)
               if( btn == passcode[hitCount] ){   // was the expected button pressed
                   hitCount++;            // yes, increment the count 
                   if( hitCount == PASSCODE_LEN){  // got all our presses?
                       passcodeValid(buzzer, HIGH); // yes, call function to handle valid passcode sequence                                       
                       // you may want to reset the hitCount here to wait for another entry
                   }                                           
               }
               else{      // here if an incorrect button was pressed 
                  hitCount = 0;         // reset the count
                  passcodeInvalid() // function to handle invalid sequence 
               }                      
         
    }  
}

and it keeps yellin at me that passcodevalid is not in the scope, and i’m pretty sure it will tell me that the passcodeinvalid will aslo not be in the code.

hardware wise, i have 3 buttons connected to pins 2,3,4 and 7 for the buzzer, and ofcourse ground. and thats it, i’m sorry i cant even figure out how to put the rest of the code together, i thought that i really could… but thanks for showin me, i rly appreciate it.

You need to add the functions that are called (see the example below) or remove the calls and put your desired functionality inline. Here is an example of the functions:

void  passcodeValid(){ // function to handle valid passcode sequence
                       // add any parameters you need ( in this example, no paramaters are passed) 
   // activate buzzer  
   // light led 
}

void passcodeInvalid(){       // note that a semicolon was missing from your posted code calling this function
  // perhaps make a sound indicating an invalid sequence     
}

if you want to increase the sequence then make the passcode array bigger
#define PASSCODE_LEN 6
int passcode[PASSCODE_LEN] = {2,0,1,1,2,3};

if you want more buttons then make the buttonPins array bigger
#define NBR_BUTTONS 5 // using a #define makes your code more flexable
int buttonPins[NBR_BUTTONS] = {2,3,4,6,7};

In Setup() you can configure output mode in a loop as follows:
for(btn =0; btn < NBR_BUTTONS; btn++)
pinMode(buttonPins[btn], INPUT);

use NBR_BUTTONS in the for statement in the main loop() as well

the passcode and buttonPins arrays make coding efficient but they may seem obscure at first.

buttonPins is an array that holds the actual pin number for each button. The first button is buttonPins[0] and in the example above it uses pin 2. The last button above (buttonPins[4]) uses pin 7

passcode holds a sequence of indices to buttonPins. in the example, the first code has a value of 2 i.e. buttonPins[2], which is connected to pin 4 in our example.

I hope that helps. If you are still unsure what is happening here then have a read about arrays in ‘C’. Arrays are a powerful tool that becomes more intuitive as you gain practice.

p.s.
Try not to get frustrated by the compiler error messages. The compiler is not yelling at you, its helping you to write your code :). Making syntax errors is a natural part of learning a new language, so welcome the helpful advice the compiler is giving you.

Don't forget to add code that makes sure that only one key is pressed at a time. In the current code, pressing all the buttons three times will work with any passcode.

You could write a function that is passed a button index and returns true if the switch pin is high for at least (say) 50 milliseconds (to remove switch bounce) while all other switches are low and then goes low for 50 milliseconds. The function could return false if the switch was not pressed long enough or if at least one other switch is pressed.

Perhaps start a new sketch, simplified a little just to test and debug this one function before you integrate it into your existing code.

I am off to the airport for a transatlantic flight now so won't be back on until Friday, I will look forward to seeing how you get on.

p.s. Try not to get frustrated by the compiler error messages. The compiler is not yelling at you, its helping you to write your code :). Making syntax errors is a natural part of learning a new language, so welcome the helpful advice the compiler is giving you.

That's a nice piece of advice, I'm impressed you took the time to state it like that...

--Phil.

thanks! have a good flight ( u prob wont read this till u get back lol ), but ill try it and post back in a bit with what i did ( or tried ) to do lol

big93, Looking forward to seeing the results. (flight landed ok, just managed to miss the east coast storms)

Follower, It took me many years of yelling at the compiler about its cryptic error messages before I realized how helpful these messages actually are once one takes the time to understand the very precise terminology in which they are expressed. I found spending time learning to understand the compiler messages saved many many times that effort in writing working code.

glad u had a great plane ride, i also had a great time ( shoveling the snow outa my driveway, while waving to your plane flying over me! lol, i am the east coast )

but yea, it seems very complicated in which how c is done, and every time i look at a new code i'm like "wtf, i dident know i could do that!!!" and then me writing 700 lines makes me feel angry that it could have been done in 3 lines lol

so just to be clear, i can make an array, lets say i just want to call pins 0-8 pin ( and then 1-8 )

then i have to put: int pin[] = {0,1,2,3,4,5,6,7,8}

and it will all be called pin0 pin1 pin2 pin3 pin4..... right?

int pin = {0,1,2,3,4,5,6,7,8} ; creates an array of 9 integers,
the first is referenced as pin[0], the last as pin[8];

you can reference each of the members sequentially as follows:
int i;
for(i=0; i < 9; i++)
pin*; *
The 9 above may look odd at first, but remember that the for loop only counts up to 8!
*If you want to learn more about C, the first and IMHO the best ever book on C is ‘The C Programming Language’ by Kernighan & Ritchie. Its expensive for a thin technical book, but it’s explanations and examples are a model of clear and precise technical writing. *

thanks, i WILL get that book, but when talking about c, is c altered to enable these commands for arduino, or for example, could you rename that book to "Arduino programming code" and not change a thing in the book?

what i mean is: will the book exactly tell me how to program c for arduino ( becuase it's not altered for diffrent programs ) or is it slightly changed for each type of program used?

o yea, and what edition of the book do i need? i see 1, 2, and 3 ( or does edition not even matter? ) i think second edition has ansi standard...

edit:

lol i got the book already, but my person who controlls me ( aka parent ) wants to know if the microsoft step by step c tutorial book is better then this, and if i should get that instead

p.s, it sais "some" knowledge of things like variables, loops and some other stuff is needed, do i qualify for knowing that stuff. To be more accurate, it sais i need to know "variables, assignment statements, loops and functions", do i know these already? thnks -big94-1