Hello, I am new to working with the Arduino, and I am having trouble with coding. What I'm trying to do is make a button that you hold and when you let go after holding for at least 10 seconds, it moves on to the main part of the code. I was thinking of using a while statement, but I'm not sure. Thanks!
int pin1 = 6;
int pin2 = 7;
int pin3 = 8;
int pin4 = 9;
int pin5 = 10;
int pin6 = 11;
int pin7 = 12;
int pin8 = 13;
int timer = 500;
// int cycle = 5400000; for future use
const int buttonPin = 4;
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(pin1, HIGH);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin1, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin7, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin4, HIGH);
digitalWrite(pin8, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
digitalWrite(pin8, LOW);
}
}
}
heyitskarl121:
What I'm trying to do is make a button that you hold and when you let go after holding for at least 10 seconds, it moves on to the main part of the code.
Do you want the code to be doing anything while it's waiting for this long button press?
If the code doesn't need to do anything else but wait for the button press then you could use something like this.
void waitForLongButtonPress(byte button_Pin, unsigned long requiredPressTime, boolean activeState)
{
unsigned long beginningActiveTime = millis();
while (millis() - beginningActiveTime < requiredPressTime)
{
if (digitalRead(button_Pin) != activeState)
{
beginningActiveTime = millis();
}
}
}
To call the function use:
waitForLongButtonPress(buttonPin, 10000UL, HIGH);
Instead of "10000UL" you could use a unsigned long constant set to the desired time.
If you want the code to execute other tasks while it waits for the button press then you'll need to keep track of the button's state and how long it had been since the button was first pressed.
BlinkWithoutDelay is probably a good place to start learning to monitor states.
DuaneDegn:
Do you want the code to be doing anything while it's waiting for this long button press?
If the code doesn't need to do anything else but wait for the button press then you could use something like this.
I don't want the code to do anything while its PRESSED, the user would hold the button, but once they let go, that's when the code would start, would you use a while statement? and thanks for the suggested read, ill go check it out
This snippet shows how to test that a button remains pressed for a certain time
btnVal = digitalRead(btnPin);
if (btnVal == HIGH) { // assumes btn is LOW when pressed
lastBtnPressMillis = millis(); // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
// button has been pressed for longer than interval
}
It is assumed to be within loop() or, perhaps, within a WHILE loop