3 minutes timer

Hi,

I'm trying to light a led using bluetooth and then have it on for 3 minutes, then turn off. The problem is with the "timer", it dosen't turn off after 3 minutes (180000ms). Can anyone se what i'm doing wrong? I have just started programming so please be nice :slight_smile:

int LED = 13;
int timer = 180000;
boolean offswitch = true;
unsigned long previousMillis = 0;

void setup() {
  
  Serial.begin(9600);
  pinMode(LED, OUTPUT); 
  
}

void loop() {

  if(Serial.available() > 0){
    char data = Serial.read();
    if (data == 'a'){
      digitalWrite(LED, HIGH);
      offswitch = false;    
    }
  }
  
  if (offswitch == false){
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis > timer){
      previousMillis = currentMillis;
      digitalWrite(LED, LOW);
      offswitch = true;
    }   
  }
  
}

Make timer unsigned long. 180K won’t fit in an int.

Thank you!!

But now i have another problem.... The problem is that if 'a' is pressed after, for example, 10 seconds. The LED will be light off after 2 minutes and 50 seconds. How do i fix that?

When you turn the led on, set previousmillis to millis()

Thank you so much again!!