High School Project :|

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.

Can anyone explain this to me?

Welcome to the forum

Both of these cannot be true

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

First of all, the circuit is shown below:

Secondly, sorry for my ambiguity -
i meant

I have to make the LED stay on for 3 seconds before turning off when the button is released.

(Please not this is my first school term working with these things so I might not be too good at it)

The led series resistors are missing....

The button state is not defined when not pressed.
Set pinMode to INPUT_PULLUP.
Buttonstate will be low when pressed...

This appears to be the schematic of the shield being used

Note that the LED resistors are present and that there is a pulldown resistor on the button input

Might I ask do you need the LED to turn on 3 seconds after holding the button or do you need it to stay on for 3 seconds after the button is released?

Still need help or did you figure it out?

... 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?

whoops my bad

press
release
led turns on for 3 seconds
led turns off

No it is not. Not even close. Show a drawing of the circuit you are using.

The sketch works exactly as you want if you connect the circuit according to the sketch.

[edit]

If the shield works, and the sketch works, this tells me you have plugged the Shield into the Arduino incorrectly. Verify.

Wokwi's circuit is wrong (according to many of my classmates)
However I know :a_button_blood_type: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.

Yes thank you
@xfpd
the circuit is here

Ok I figured it out
Thank you @UKHeliBob and @build_1971