led on/off from buton and if On with no OFF, LED switch OFF after 30 min

Hello, I`m new with Arduino so please be gentil

My project is :
I want to turn ON and OFF a LED from a push Button, but if I turn it ON and NOT OFF, the LED to switch OFF after 30 min by itself.

I found this at http://forum.arduino.cc/index.php?topic=114964.0 wich turns OFF a LED after 5 seconds.

I modified to turn ON and OFF, and if no OFF the LED turn OFF after 5 seconds by itself.
However, when I tried to extend the period ot time in wich the LED stays ON …I couldn`t make it more then 2 minute. I modified only the interval in condition:

original line: if(millis()-ledCameOn > 5000);
modified line: if(millis()-ledCameOn > 30601000L);

And the led turn off after aproximative 3 minute

Please help.
Thanks

the code:

void setup()
{
pinMode(10,OUTPUT); // LED output
pinMode(2,INPUT); // Button input
}

void loop()
{
static unsigned char ledState = LOW;
static unsigned char buttonState = LOW;
static unsigned char lastButtonState = LOW;
static unsigned long ledCameOn = 0;

// If the LED has been on for at least 5 seconds then turn it off.
if(ledState == HIGH)
{
if(millis()-ledCameOn > 30601000L) // original was if(millis()-ledCameOn > 5000)
{
digitalWrite(10,LOW);
ledState = LOW;
}
}

buttonState = digitalRead(2);
if(buttonState != lastButtonState)
{
lastButtonState = buttonState;
if((buttonState == HIGH) && (ledState == HIGH))
{
digitalWrite(10,LOW);
ledState = LOW;
ledCameOn = millis();
}
}

// If the button’s state has changed, then turn the LED on IF it is not on already.
buttonState = digitalRead(2);
if(buttonState != lastButtonState)
{
lastButtonState = buttonState;
if((buttonState == HIGH) && (ledState == LOW))
{
digitalWrite(10,HIGH);
ledState = HIGH;
ledCameOn = millis();
}
}
}

_30min__LED_ON_then_OFF.ino (1.04 KB)

What happens if you use   if(millis()-ledCameOn > 1800000)

gabytm: My project is : I want to turn ON and OFF a LED from a push Button, but if I turn it ON and NOT OFF, the LED to switch OFF after 30 min by itself.

unsigned long previousMillis = millis();

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

void loop() {
  previousMillis = digitalRead(2) == LOW ? millis() : previousMillis;
  digitalWrite(10, millis() - previousMillis > 30*60*1000 ? LOW : HIGH);
}

Showing off your understanding of the ternary operator is a !(real good way) of helping a total beginner

buttonState = digitalRead(2);
  if(buttonState != lastButtonState)
  {
    lastButtonState = buttonState;

You do this twice per loop. Tidy up so you're only updating the button state once per loop and that should sort things.

manor_royal: Showing off your understanding of the ternary operator is a !(real good way) of helping a total beginner

I'm a Newbie like he is, don't you see my classification? Anyway, Ternary Operation isn't rocket science, for sure easier than all the code he is dealing with!

Please add my Karma, I deserve it :)

Thank you all. I tested every suggestion. Each one work, but after the half hour passed, I can`t reinitiate . The LED flash shortly and nothing else. I have to reset some how. Also I have to debounce the input. I will get back to you as soon I made some progress.

btw, the ternary operand is an elegant solution

Thanks again!