Hello, first of all I'm sorry if there already is an answer to this question (and there prorbably is), but after weeks of searching the internet I just can't get around my problem. I'd also like to pint out I'm a beginner.
THE IDEA
I have 4 buttons (sensors in real life, but I'm just prototyping in this phase). When 3/4 buttons(sensors) are pressed/activated, output pins 8-13 will output HIGH and LOW states (1st time HHHHHH, 2nd time HHLHLH, 3rd time LLLHHH and so on), which will be sent to an external device connected to those pins.
THE PROBLEM
I have made the prototype and all is ok, but if I do not press the 3 buttons at the same time, it doesn't work.
Since eventually instead of buttons it'll be sensors, I have to take into consderation that one can't "activate" 3 sensors at the same time (keep them sending HIGH input on arduio).
How would I tell (in code) the Arduino that once the 1st button/sensor is activated to keep it in HIGH state(make him remember that it's been set to HIGH) , for example 3sec, so that the person has the time to activate the other 2 sensors, and only THEN that the output sends to an external device?
From what I researched 'till now, I'd use arrays and for loops and the function millis();, but the examples I found so far haven't worked for my problem (I haven't been able to implement it in my scenario).
I think you did.. I think I may have expressed myself wrong.. Here's a rough sketch of the code so far:
void loop(){
//read states of all 4 buttons/sensors
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
//check if the 3/4 condition is met
if (buttonState1 == HIGH && buttonState2 == 2 && buttonState3 == HIGH) {
//send output states(triggers) to external device
digitalWrite(outPin1, HIGH);
digitalWrite(outPin2, LOW);
digitalWrite(outPin3, HIGH);
digitalWrite(outPin4, LOW);
digitalWrite(outPin5, HIGH);
}
else {
//nothing happens
digitalWrite(outPin1, LOW);
digitalWrite(outPin2, LOW);
digitalWrite(outPin3, LOW);
digitalWrite(outPin4, LOW);
digitalWrite(outPin5, LOW);
}
}
The problem is, when I release the button, it doesn't stay set to HIGH, it goes back to LOW.. And it should stay HIGH for let's say 2 sec ( so I have time to press the remaining two, so all 3 can be read in the IF statement as being HIGH).
Your variables are local variables which are initialized on each iteration of loop().
If it was my project I would define them a global variables by putting int buttonState1; before setup() and just using buttonState1 = digitalRead(buttonPin1); within loop().
You can also persist the value between iterations by prefacing the declaration with static as in static int buttonState1 = digitalRead(buttonPin1); but those values will only be available within loop().
As Robin suggests the input pin does not have to stay HIGH for 2 seconds but a variable that was set to true when the button was pressed will work. Your thread title even suggests this solution
Something along these lines
start of loop
if button1 changes from not pressed to pressed
set button1Pressed = true
set button1PressedTime = millis()
end if
if button2 changes from not pressed to pressed and button1Pressed is true
do stuff here
end if
if millis() - button1PressedTime >= 2 seconds
set button1Pressed = false
end if
end of loop