description of what I am trying to accomplish:
-Push button to turn outputPin_3 HIGH or LOW and stay HIGH or LOW
-When HIGH, reset or (turn LOW and then back HIGH) every 9 seconds for 1 second
-When LOW, stay LOW
Below shows the HIGH/LOW pushbutton section only that I am using which is cut from my project.
Attached “All.txt” shows my entire sketch for the whole project.
Attached “idle_millis_test_2.txt” is what I’ve tried to accomplish this.
I was able to use millis() to turn LOW an output pin after x seconds as seen in the “all.txt”. It uses the same pushbutton idea as this one.
The reason I need the reset is that I have a recorded speaker playback module that only records 10 seconds, so I want a continual loop playback to imitate a constant HIGH state. Switching from HIGH to LOW to HIGH again resets the recording.
Any suggestions or tips to get me going in the right direction?
int ledPin_3 = 10; // LED pin 3
int outputPin_3 = 3; // output pin 3
int switchPin_3 = A4; // switch pin 3
int val_3; // variable for reading the pin 3 status
int buttonState_3; // variable to hold the button 3 state
int lightMode_3 = 0; // Is LED 3 on or off?
void setup() {
pinMode(switchPin_3, INPUT); // Set switch 3 pin as input
pinMode(ledPin_3, OUTPUT); // Set LED pin 3 pin as output
pinMode(outputPin_3, OUTPUT); // Set OUTPUT pin 3 pin as output
buttonState_3 = digitalRead(switchPin_3); // read the initial state 3
}
val_3 = digitalRead(switchPin_3); // read input value 3 and store it in val_3
if (val_3 != buttonState_3) { // button state 3 has changed
if (val_3 == LOW) { // check if the button 3 is pressed
if (lightMode_3 == 0) { // if lightmode_3 is off
lightMode_3 = 1; // lightMode 3 on
analogWrite(ledPin_3, 50); // turn LED 3 pin on
digitalWrite(outputPin_3, HIGH); // turn output 3 pin on
} else {
lightMode_3 = 0; // lightMode 3 off
digitalWrite(ledPin_3, LOW); // turn LED 3 pin off
digitalWrite(outputPin_3, LOW); // turn output 3 pin off
}
}
}
buttonState_3 = val_3; // save new state 3 in variable
all.txt (7.8 KB)
idle_millis_test_2.txt (6.11 KB)