Ran into the following; want to press button 1, release button 1, press button 2 within a certain time, release button 2, make output high for a certain time.
Since i'm posting this from my cell, i can't add all the code that i've tried already. Already tried to digitalread the button1 state while he is active in a certain time, (button1state && button2 pin), but can't make it work
This should give you the general idea. The delay() function has no role to play in this sort of code
Read the two buttons
If the first button is pressed but was NOT pressed the pervious time it was tested
save the value of millis() eg. startMillis = millis();
set a state variable e.g. buttonOneReady = true;
check the current value of millis() e.g.
if (millis() - startMillis >= interval) {
buttonOneReady = false; // it's too late
}
If (buttonOneReady = true && buttonTwoPressed = true) {
// do whatever is needed
}
repeat
Note sure that does what OP wants, Robin, since button1 needs to be pressed and released to arm the system.
I'd go with flags like button1NeverBeenPressed and button1NotPressedAtTheMoment and systemArmed.
Soon as you detect button1 is pressed, set button1NeverBeenPressed to false, and any time after that when you find button1NotPressedAtTheMoment is true and button1NeverBeenPressed is false, then set systemArmed to true because that means button 1 was pressed and released and do the button2 stuff.
gunske:
What am I missing? Below 2 different codes that would not work
The principal thing you are missing is thinking and a systematic approach.
I spent my career writing reports and such. When you are writing it is OK to scribble a quick paragrah to capture a thought and maye tidy it up a bit later, or move it somewhere else in the essay. Almost no matter what words you put in the paragraph a human reader will get the sense of it.
Programming is not at all like that - there are far too many ways in which something can be wrong. You must plan what is oing to happen systematically. That may mean reading some stuff (including some of the earlier posts here) ten or a dozen times and thinking about what they imply - and don't imply.
What has already been suggested is that you use variables to record the state of the buttons.
From looking at your code you seem to have forgotten completely that the state variables have to be set by pressing the buttons because I can see no evidence of reading the buttons. Also you seem to be confusing the idea of state variables with the pin numbers for the buttons.
You need to work through your code line by line in your head or with pencil and paper pretending that your brain is the Arduino. At each stage work out the values of the variables and follow the logic when you come to branching or repeating statements such as IF and WHILE.
Slapping some code together in the way you might write an essay and then being disappointed when it won't work is futile.
I have added a few comments in this code - but I'm sure there are many more issues.
int buttonOneReady = 2; // <--------this should be named buttonOnPin
int buttonTwoPressed = 3; //<-------and this is also a pin number
const int led2Pin = 13;
unsigned long startMillis;
void setup() {
Serial.begin(9600);
pinMode(led2Pin, OUTPUT);
pinMode(buttonOneReady, INPUT);
pinMode(buttonTwoPressed, INPUT);
}
void loop() {
startMillis = millis();
buttonOneReady = true; //<-----------why would a pin number ever be true
//<-----------if it is a state variable why set it to true just because
// loop() has repeated
if (millis() - startMillis >=2000) {
buttonOneReady = false; //< ------------or false
}
if (buttonOneReady == true && buttonTwoPressed == true) { //<--------how did buttonTwoPressed get to be true
digitalWrite(led2Pin, HIGH);
delay(2000);
digitalWrite(led2Pin, LOW);
}
}