LED toggle and millis()

Hi, what i'm trying to do is on switch press toggle the LED1 and turn on LED2 too but only turn LED2 off after X seconds. so each time button pressed it should reset the LED2 timer and toggle LED1.

here's the code:

int pin_switch = 2;
int pin_LED = 3;
int pin_LED2 = 4;

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean LEDstatus = LOW;
boolean LED2status = LOW;

unsigned long previousMillis = 0;
const long period = 3000; 

void setup() 
{
    Serial.begin(9600);
    pinMode(pin_switch, INPUT); 
    pinMode(pin_LED, OUTPUT);
    pinMode(pin_LED2, OUTPUT); 
    digitalWrite(pin_LED,LOW); 
}

void loop()
{
    newSwitchState = digitalRead(pin_switch);

    if ( newSwitchState != oldSwitchState ){
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= period) {
        digitalWrite(pin_LED2, LOW);
        LED2status = LOW;
      }
      else if ( LED2status == LOW ){
       digitalWrite(pin_LED2, HIGH);
       LED2status = HIGH;
      }
       previousMillis = currentMillis;
       if ( newSwitchState == HIGH ){
           if ( LEDstatus == LOW ){
            digitalWrite(pin_LED, HIGH);
            LEDstatus = HIGH;
            }
           else{
            digitalWrite(pin_LED, LOW);
            LEDstatus = LOW;
            }
       }
       oldSwitchState = newSwitchState;
       Serial.print("LED 1: ");
       Serial.println(LEDstatus);
       Serial.print("LED 2: ");
       Serial.println(LED2status);
    } 
}

LED1 toggles as expected 0/1, but LED2 only prints 1!

Something like this pseudo code (which would be in loop() )

if (button is pressed) {
   led2startMillis = millis()
}

if (millis() - led2startMillis >= X) {  // X in millisecs
   turn off LED2
}

This will keep the LED2 on until X millisecs after the most recent press of the button

...R

Robin2: Something like this pseudo code (which would be in loop() )

if (button is pressed) {
   led2startMillis = millis()
}

if (millis() - led2startMillis >= X) {  // X in millisecs   turn off LED2 }




This will keep the LED2 on until X millisecs after the most recent press of the button

...R

like this?

int pin_switch = 2;
int pin_LED = 3;
int pin_LED2 = 4;

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean LEDstatus = LOW;
boolean LED2status = LOW;

unsigned long previousMillis = 0;
const long period = 3000;

void setup()
{
    Serial.begin(9600);
    pinMode(pin_switch, INPUT);
    pinMode(pin_LED, OUTPUT);
    pinMode(pin_LED2, OUTPUT);
    digitalWrite(pin_LED,LOW);
}

void loop()
{
    newSwitchState = digitalRead(pin_switch);

    if ( newSwitchState != oldSwitchState ){
      if (millis() - previousMillis >= period) {
        digitalWrite(pin_LED2, LOW);
        LED2status = LOW;
      }
      else if ( LED2status == LOW ){
        digitalWrite(pin_LED2, HIGH);
        LED2status = HIGH;
      }
       if ( newSwitchState == HIGH ){
       previousMillis = millis();
      if ( LEDstatus == LOW ){
        digitalWrite(pin_LED, HIGH);
        LEDstatus = HIGH;
      }
      else{
        digitalWrite(pin_LED, LOW);
        LEDstatus = LOW;
      }
       }
       oldSwitchState = newSwitchState;
       Serial.print("LED 1: ");
       Serial.println(LEDstatus);
       Serial.print("LED 2: ");
       Serial.println(LED2status);
    }
}

i think i did wrong because it didn't make any difference...

AHA! it's the right way:

int pin_switch = 2;
int pin_LED = 3;
int pin_LED2 = 4;

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean LEDstatus = LOW;
boolean LED2status = LOW;

unsigned long previousMillis = 0;
const long period = 3000;

void setup()
{
    Serial.begin(9600);
    pinMode(pin_switch, INPUT);
    pinMode(pin_LED, OUTPUT);
    pinMode(pin_LED2, OUTPUT);
    digitalWrite(pin_LED,LOW);
}

void loop()
{
    newSwitchState = digitalRead(pin_switch);

    if (millis() - previousMillis >= period) {
      digitalWrite(pin_LED2, LOW);
      LED2status = LOW;
    }
    else if ( LED2status == LOW ){
      digitalWrite(pin_LED2, HIGH);
      LED2status = HIGH;
    }
    if ( newSwitchState != oldSwitchState ){
       if ( newSwitchState == HIGH ){
       previousMillis = millis();
      if ( LEDstatus == LOW ){
        digitalWrite(pin_LED, HIGH);
        LEDstatus = HIGH;
      }
      else{
        digitalWrite(pin_LED, LOW);
        LEDstatus = LOW;
      }
       }
       oldSwitchState = newSwitchState;
       Serial.print("LED 1: ");
       Serial.println(LEDstatus);
       Serial.print("LED 2: ");
       Serial.println(LED2status);
    }
}

Thanks Robin2