Make a light turn off one second after I let go of the button

Hi I am using the Arduino Uno with this code and this circuit and I am trying to make it so that when I press the button it turns off and when I let go of the button it will turn on and then turn off after one second but now when I le go it blinks. How can I fix this.

int switchstate = 0;
bool light = true;

void setup() {
  
  pinMode(3, OUTPUT);
  
  pinMode(2, INPUT);
}

void loop() {

  switchstate = digitalRead(2);

  if (switchstate == LOW) {
    light = true;
    
 }
  else {
    light = false;
  }
  if (light == false){

digitalWrite(3, LOW);
//delay(1000);
    
    }
    if (light == true){
      digitalWrite(3, HIGH);
      delay(1000);
      digitalWrite(3, LOW);
      delay(1000);
      light = false;
    }
}

you need a small state machine with one variable to remember the steps you went through

J-M-L:
you need a small state machine with one variable to remember the steps you went through

sorry I am a beginner how can I do this

comodomo:
Hi I am using the Arduino Uno with this code and this circuit and I am trying to make it so that when I press the button it turns off and when I let go of the button it will turn on and then turn off after one second but now when I le go it blinks. How can I fix this.

Your description is a bit unclear. "I am trying to make it so that when I press the button it turns off..." This means it was already on. "...when I let go of the button it will turn on and then turn off after one second..."

So, is this a one time event or, is there a next time for pushing the button?

Ιf I understand well you want 3 things.

  1. When button press turn off the light.
  2. When button released turn on the light.
  3. after i second turn off the light.

So the light it will be already close before you press the button again.

dougp:
Your description is a bit unclear. "I am trying to make it so that when I press the button it turns off..." This means it was already on. "...when I let go of the button it will turn on and then turn off after one second..."

So, is this a one time event or, is there a next time for pushing the button?

there is a next time for me pressing the button

GrOnThOs:
Ιf I understand well you want 3 things.

  1. When button press turn off the light.
  2. When button released turn on the light.
  3. after i second turn off the light.

So the light it will be already close before you press the button again.

yes.

Why do you need this ? Is that a school project?

J-M-L:
Why do you need this ? Is that a school project?

yes It is a school project

comodomo:
there is a next time for me pressing the button

So, the second time, is the light on or off?

boolean switchState = LOW;
bool previousSwitchState = LOW;

void setup() {

  pinMode(3, OUTPUT);

  pinMode(2, INPUT);
}

void loop() {

  switchState = digitalRead(2);

  if (switchState == LOW && previousSwitchState == HIGH) {
    digitalWrite(3, HIGH);
    delay(1000);
    digitalWrite(3, LOW);

  }


  previousSwitchState = switchState;
}

This works fine, but needs debounce solution.