How do I wait in a function?

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);*
    }
    }
if (digitalRead(button1 == HIGH))

Think about what this statement is doing. The inner most parentheses contain button1 == HIGH. So, button1 is compared to HIGH (which has a value of 1). 1 == 1, so, digitalRead is called for pin1. It might, or might not be being pressed, so it might or might not return true.

The next statement

else if (digitalRead(button2 == HIGH))

will be false, as will all the others. Your function will, therefore return either 1 or 4.

You want something like:

if (digitalRead(button1) == HIGH)

But, even this change won't work.

The loop that calls buttonPress does not wait for a button press. If no button is being pressed, it returns 4. Your query array is likely to contain 4 4's.

Wow, you're right and you're right. Thanks PaulS. I am going to have to redo the entire function I think, maybe take it in another direction. But that definetly helps with you explaining my parenthesis problem.

There is a button library. It has a uniquePress method that returns true if the button was pressed since the button was last checked.

You could create 4 instances of the class, once for each button pin.

Then, in loop, or in a function called by loop, check each instance. If the function returns true for only one button, that button was pressed.

Add that button ID to an array. Otherwise, do nothing.

When enough buttons have been pressed, see if they were pressed in the right order.