I need to get an input value high/low when the user pushes the button.
I want to give the user at least 2 seconds time to push the button. I tried the following but it does not work. Any suggestions?
long currentMillis = 0;
long previousMillis = 0;
for (currentMillis = millis(); (currentMillis - previousMillis) > 2000; previousMillis = currentMillis)
{
if {
// statement to check if it is high
}
else{
//do this if it is low
}
}
currentMillis = millis() - assigns current time to currentMills
currentMillis - previousMillis)> 2000 - for loop checks for 2 seconds
previousMillis = currentMillis - assign new time to previousMills
The for statement says "Start with assigning currentMillis the value returned by millis(). Then, while currentMillis minus previousMillis is greater than 2000, do some stuff. Then, assign previousMillis the value in currentMillis".
So, on the first pass, currentMillis - previousMillis will have some value based on how long the Arduino has been running. That may, or may not, be greater than 2000, so the body may, or may not be executed. Regardless of whether or not it is, previousMillis will be set to currentMillis.
Then, the while clause is evaluated again. Since previousMillis is equal to currentMillis, the difference is 0. Since 0 is not greater than 2000, the for statement ends.
I can't see how that will wait for two seconds.
unsigned long start = millis();
while(millis() - start <= 2000)
{
// Check for user input
// If present
break;
}
// You will get here as soon as the user input arrived or after waiting for 2 seconds.
// You'll need to do something in the body of the if statement to distinguish the
// two cases.
Thanks a lot for your reply.
However, I may need some more options.
I am testing switches. So not only that I need ANY input, but I have two cases when switch is passing or failing. When switch is passing, I get 5V at the pinSW, otherwise 0V. I need to display both cases.
I tried this, but it didn't work:
I just gave an example, my code is actually more complicated. When I said that it did not work, it was not because of the compiling (typing) errors but because it doesn't check the input for 2sec. I am new at this forum and I did not think that it would be an English type of class checking for spelling and grammar, rather elective class focusing on the functionality. Sorry, I will be more careful in future.
Let me be more careful and specific this time:
The only passing combination is under "if" statement bellow. Any other combination of pinSW_COL_1,2 and 3 is a fail.
Can you please tell me how to check within 2 sec if this is pass. My code here is displaying pass result, however, timing is still not working, I still have to press and hold the switch.
Can you please tell me how to check within 2 sec if this is pass.
I have no clue what "if this is pass" means.
After the while statement ends, because of the break statement or because of the timeout, you haven't a clue which reason caused the while loop to end. Why not?
bool dumbAssPressedSwitch = false;
while(millis() - start <= 2000)
{
if((digitalRead(pinSW_COL_1)) &&
!(digitalRead(pinSW_COL_2)) &&
!(digitalRead(pinSW_COL_3)))
{
dumbAssPressedSwitch = true;
break;
}
}
if(dumbAssPressedSwitch)
{
// What do you know - the dumb ass pressed a switch
}
else
{
// Dumb ass off taking a leak I guess
}
EVERY { belongs on a new line.
EVERY line in a block is supposed to be indented.
It is OK (and even preferred) to break long statements up.