Hello, I have an Arduino Uno with a MAAS ThinkerShield attached to it.
For my school project, I have to make the LED stay on for 3 seconds before turning off when the button is released.
I have written the response below:
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 12;
bool activated = false; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (activated == true) {
digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(ledPin, LOW);
activated = false;
} else {
activated = true;
do
{delay(1);
buttonState = digitalRead(buttonPin);
} while (buttonState == HIGH);
}
} else {
digitalWrite(ledPin, LOW);
}
}
(ignore the comments)
For some reason the LEDs come on whenever as soon as I press the button, even though it is stated that it must be delayed until the button is released.
Starting with the LED off, what should happen when you press the button ?
I think you want the LED to come on immediately, stay on for 3 seconds even if the button is released then turn off whether the button is still pressed or not
Is that right ?
Please post a schematic of your circuit. A photo of a hand drawn circuit is good enough. It is important that the button pin is in a known state at all times not just when the button is pressed
... or, what if the button is held for 4 seconds? Does the LED extinguish at 3 seconds while holding the button, or does the LED extinguish 3 seconds after releasing the button?
Wokwi's circuit is wrong (according to many of my classmates)
However I know BSOLUTELY NOTHING about circuitry, so please do not ask me anything about it.
It is just an Arduino with a ThinkerShield attached through the pins given.