I'm new to Arduino and I don't have that much programming experience, so I need help from the pro's on a project I am doing.
I am trying to make a digital lock release mechanism based on a four button code. I have a strong feeling that my "buttonpress" function is the culprit, but I'm not exactly sure how to fix it. All my program does right now is flash the lockout LED. I know I will need a debounce section sooner or later, but I want to get the basic concept down. I can't figure out how to wait in the "buttonpress" function until something is pressed. Any and all help would be greatly appreciated.
Oh, and if this is beyond salvaging...feel free to say so.
int release = 10; //call to release solenoid
int lockout = 9; //incorrect code LED flasher
int ledpin = 8; //LED that lights when any button is pressed
int button1 = 1; //button for code
int button2 = 2; //button for code
int button3 = 3; //button for code
int button4 = 4; //button for code
int c; //to load int into array
int code[] = {1, 2, 3, 4}; //create two arrays, one for the correct code
int query[] = {0, 0, 0, 0}; //the other for the code to be entered
void setup()
{
pinMode(release, OUTPUT); //declaring the inputs and outputs
pinMode(lockout, OUTPUT);
pinMode(ledpin, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
}
void loop()
{
for (int i = 0; i<3; i++) //during this for loop, call function to
{ //determine which button was pressed
c = buttonpress(); //load that button value into query array
query = c;
-
}*
-
if (code[0] == query[0]) //test to see if one array matches the other*
-
{*
-
if (code[1] == query[1])*
-
{*
-
if (code[2] == query[2])*
-
{*
-
if (code[3] == query[3])*
-
{ //if it matches, release the solenoid for 5 seconds*
-
digitalWrite(release, HIGH);*
-
delay(5000);*
-
digitalWrite(release, LOW);*
-
delay(5000);*
-
}*
-
}*
-
}*
-
}*
-
else*
-
{ //otherwise flash a LED for an incorrect entry*
-
digitalWrite(lockout, HIGH);*
-
delay(500);*
-
digitalWrite(lockout, LOW);*
-
delay(500);*
-
digitalWrite(lockout, HIGH);*
-
delay(500);*
-
digitalWrite(lockout, LOW);*
-
delay(500);*
-
}*
}
int buttonpress() //I KNOW THE PROBLEM IS HERE SOMEWHERE!! I also realize that this is probably the completely wrong way to do this, but I have no other idea on how
{
if (digitalRead(button1 == HIGH))
{
- return (1);*
}
else if (digitalRead(button2 == HIGH))
{ - return(2);*
}
else if (digitalRead(button3 == HIGH));
{ - return(3);*
}
else
{ - return(4);*
}
}