How could I insert an argument where it says digitalWrite high to check if the output pin is high; digitalWrite low, if low digitalWrite high.
const int INPUT_PIN = 12;
const int OUTPUT_PIN = 13;
unsigned long startTime = 0;
const unsigned long interval = 5000; // 5 seconds in milliseconds
bool timerStarted = false;
const unsigned long debounceDelay = 50; // 50 milliseconds for debounce
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
void setup() {
pinMode(INPUT_PIN, INPUT_PULLUP);
pinMode(OUTPUT_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(INPUT_PIN);
// check if the input has stayed stable for longer than the debounce delay
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the current reading has been stable for longer than debounceDelay, take action
if (state == LOW) {
if (!timerStarted) {
// start the timer
startTime = millis();
timerStarted = true;
Serial.println("starting");
}
else if (millis() - startTime <= interval) {
digitalWrite(OUTPUT_PIN, HIGH); // set the output pin high
Serial.println("success"); // write to serial console
timerStarted = false; // reset the timer
}
}
}
// reset the debounce timer
if (state == LOW) {
Serial.println("resetting debounce");
lastDebounceTime = millis();
}
// time exceeds 5 seconds without a second trigger, reset the timer
if (timerStarted && millis() - startTime > interval) {
Serial.println("5 seconds no trigger; resetting");
timerStarted = false;
}
}
To start with you don't understand what the word argument means. So maybe try to use some different words that you do know and you will make more sense.
If it says digitalWrite(OUTPUT_PIN, HIGH); then the output pin is HIGH. You don't need to check. You can rest assured.
If you want to also write another pin low at that time, then digitalWrite with that pin number and LOW.
@noguru Or, is it that you want to flip the bit, in which case digitalWrite(OUTPUT_PIN, !digitalRead(OUTPUT_PIN);
would do what you want.
Please, clarify what it is you want, if not that.
I'm essentially wanting to toggle the output pin. If a second button press is sensed on the input pin within 5 seconds the output pin is set high and the timer is reset. That works. I need pin 13 to toggle so that the first time the second button press is sensed in the 5 seconds, the output pin goes high and the next cycle it goes low. Alternatively and maybe a better solution, is to have pin 13 go high every time the button is pressed twice and go low every time the button is pressed 3 times within the time period.