delay/resetting sketch(SOLVED)

I have a circuit with Arduino, LED and button and I need that sketch would be able to make these actions:

  1. Pressing the button turns the LED on for 2 seconds after which it turns off again.
  2. If the button is pressed while the LED is on, the 2-second on time shall be reset so
    that it will be on for another 2 seconds (i.e., it gets retriggered).
  3. After the LED has been on for 2 seconds and then off, it shall not be possible to turn
    the LED on again for 3 seconds.
  4. After each time the LED has been on, print a serial message with how many times the
    LED has been turned on.

I’m am completely bad in mixing several codes. In this code it is not possible to reset LED more than once, then it is off for 3sec. If I press the button more than twice, it delays on for 4 sec. Then it is off for 3sec. And I need to make it possible to reset every time LED is on.

Could you give me any advice how should I change my code? Probably I added too many stuff.

astuntas.ino (1.69 KB)

Here is my code, if you don't want to download it :)

// constants won't change. They're used here to // set pin numbers: const int buttonPin = 3; // the number of the pushbutton pin const int ledPin = 9; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status int ledState = 0; //variable for reading led status int buttonPushCounter = 0; //counter for the number of button presses int lastButtonState = 0; //previous state of the button

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); //initialize serial communication: Serial.begin(9600); }

void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); //compare the buttonState to its previous state if (buttonState != lastButtonState){ // check if the pushbutton is pressed. // if it is, the buttonState is high: if (buttonState == HIGH) { //if current state is high then count and print buttonPushCounter++; Serial.print("Number of button pushes: "); Serial.println(buttonPushCounter);

// turn LED on for 2sec: digitalWrite(ledPin, HIGH); delay (2000); //if button is not pressed again turn LED off for 3sec digitalWrite(ledPin, LOW); delay(3000);

//check led is still on and button is on again if (ledState == HIGH && buttonState == HIGH) { digitalWrite(ledPin, HIGH); //TURN LED ON delay(2000);//delay 2sec }

} else { // turn LED OFF: digitalWrite(ledPin, LOW);

} } //save the current state as the last state //for the next time through the loop lastButtonState = buttonState; }

If you want to update a value while the LED is on you will have to stop using the delay() function and rewrite your code to manage time using the technique in the Blink Without Delay example sketch. This extended demo of the technique several things at a time may help to explain the idea.

Assuming you save the value of millis() in a variable called LEDstartMillis when the button is first pressed then normally the LED would go off if(millis() - LEDstartMillis >= interval) {.

If you want the LED to stay on you just need to arrange for the button press to update LEDstartMillis to the latest value of millis() which effectively restarts the timing sequence.

...R

look at this as a possible method

it is not tested, uncompiled

const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status
int ledState = 0; //variable for reading led status
int buttonPushCounter = 0;  //counter for the number of button presses
int lastButtonState = 0;    //previous state of the button
unsigned long startTime;
boolean lockOut;

void setup() 
{
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);  
  Serial.begin(9600); 
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState && (!lockOut))
  {
    if (buttonState == HIGH) 
    {
      buttonPushCounter++;
      Serial.print("Number of button pushes: ");
      Serial.println(buttonPushCounter);
      startTime = millis();
    } 
  }
  lastButtonState = buttonState;
  liteLED();
}

void liteLED()
{
  if (millis() - startTime < 2000UL)
  {
  digitalWrite(ledPin, HIGH);
  lockOut = false;
  }
  else if (millis() - startTime < 5000UL)
  {
    digitalWrite(ledPin, LOW);
    lockOut = true;
  }
  else 
  {
    lockOut = false;
  }
}

Thank you very much for your help! Appreciated!