What´s the task of sketch in the real world?
and that's what I described in posts #2 amd #6
Yes
What should happen if the LED is turned on and the button is released within 5 seconds ? Does the LED stay on for ever ?
yes bro
It's a very common thing in the Real World that a button has to be held for (at least) a certain time before an action occurs!
Please, I am not your bro
Try this
const byte buttonPin = A3;
const byte ledPin = 3;
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); //start with LED off
}
void loop()
{
static boolean ledStaysOn = false;
static unsigned long waitStartTime;
unsigned long currentTime = millis();
const unsigned long waitPeriod = 5000;
static byte currentButtonState;
static byte previousButtonState;
previousButtonState = currentButtonState;
currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && ledStaysOn == false) //button state has changed
if (currentButtonState == LOW) //button became pressed
{
digitalWrite(ledPin, LOW); //turn on the LED
waitStartTime = currentTime; //save time it was turned on
}
else //button was released
{
if (currentTime - waitStartTime >= waitPeriod) //waited longer than period
{
ledStaysOn = true; //set flag to true
}
else
{
digitalWrite(ledPin, HIGH); //turn off the LED
}
}
}
when i hold button the led doesn't turn off she stay on
As you have confirmed in post #27.
How are the button and LED wired ?
The button should be wired to take the input LOW when the button is pressed and the LED should be wired so that it come on when its Arduino pin goes LOW
no i say i wannt the led to turn off if i keep pressing the button for 5 s
The "5 seconds" press is called "long press" in the instructions for electronic gadget world... You may see it written: 长按
Here is a longpress simulation:
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.