const int resetButtonPin = 3;
const int ledPin = 13;
const int ledPin2 = 7;
const int debounceDelay = 50;
const unsigned long holdTime = 5000;
int buttonState = 0;
int lastButtonState = 0;
int resetButtonState = 0;
unsigned long buttonPressTime = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
resetButtonState = digitalRead(resetButtonPin);
// Check if the reset button is pressed
if (resetButtonState == LOW) {
delay(debounceDelay);
if (digitalRead(resetButtonPin) == LOW) {
// Call the reset function to restart the Arduino
reset();
}
}
// Regular button handling
if (buttonState == HIGH && lastButtonState == LOW) {
startButtonPress();
} else if (buttonState == LOW && lastButtonState == HIGH) {
endButtonPress();
} else if (buttonState == HIGH && millis() - buttonPressTime >= holdTime) {
longButtonPress();
}
lastButtonState = buttonState;
}
// Function to handle LED control based on the button press
void startButtonPress() {
digitalWrite(ledPin, HIGH);
buttonPressTime = millis();
}
void endButtonPress() {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
void longButtonPress() {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
}
// Function to perform a software reset
void reset() {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(8000); // Give a short delay to allow LEDs to turn off completely
}
Why did you start a topic in the Uncategorised category of the forum when its description is
DO NOT CREATE TOPICS IN THIS CATEGORY ![]()
Your topic has been moved to the Programming category
thank you
can you define "one time" ? does it require the loop to run many times ?
you could use a bool
bool codeDidRunOnceAlready = false;
void setup() {
•••
}
void loop() {
if (codeDidRunOnceAlready) {
if (restartButtonPressed()) codeDidRunOnceAlready = false;
} else {
•••
if (•••) codeDidRunOnceAlready = true; // when the code is done, make a note of it
}
}
don't see any reason to check again if the resetButton is pressed (LOW) nor a need for the debounce delay because the reset condition exists and reset() is called as soon as the LOW condition occurs
conversely in the code below it's not clear why the reset button pin is configured as INPUT_PULLUP but the button pin in just configured as INPUT. it also appears the expectation is the button pin is active HIGH instead of LOW
assuming the pin is active HIGH (somehow), without a debounce delay the end button condition will occur immediately after the button press
another issue is there a need to recognize a long button press only once or repeatedly after the holdTime has expired? there's nothing that prevents the following condition from occurring repeatedly
Your code does not compile
One of your buttons is RESET (resetButtonPin), which is different from RESTART. "Reset" means to set all the values to the initial state. "Restart" begins the program. Which are you asking about?
[edit] And... are you connecting the resetButtonPin to the Arduino reset pin?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.