Timer question

Is it Possible to use a timer like this? This doesn't turn the led off but the serial monitor show the time count up correctly when the switch is activated.

#define LED 12
#define SW 3
unsigned long runningT;
unsigned long currentT;
int SWstate;
int LEDstate;
void setup()
{
  pinMode(SW,INPUT);
  pinMode(LED,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  currentT= millis();
  SWstate=digitalRead(SW); 
  LEDstate= digitalRead(LED);
  if (SWstate==HIGH)
  {
    LEDstate =HIGH;
    runningT=runningT++;
  }
  if ((runningT==5000))
  {
    LEDstate=LOW;
  }  

  Serial.println(runningT);
  //Serial.println(SWstate);

  digitalWrite(LED,LEDstate);
}

That's a piss poor way of using a "timer". First off, you're assuming that loop() takes a specific amount of time. Unless you measure it every time you make changes to the code, the time it takes for loop() to run won't likely be consistent. Secondly, all that code does is turn the LED off 5 seconds after reset/power-on. Once the switch is pressed again, it turns it on permanently. How about describing what you are trying to do, not how you are trying to do it?

You need to do the timer like in the example:

Michael

    runningT=runningT++;

You want to read up on the ++ operator, so you don't look like a doofus.

n++ is equivalent to n = n + 1, so your code is equivalent to n = n = n + 1. Looks silly that way, doesn't it?

easterly81:

    runningT=runningT++;

That statement has undefined results.

In general this is undefined in C:

i = i++;

http://www.stroustrup.com/bs_faq2.html#evaluation-order

Also see: Sequence point - Wikipedia

The value of i++ between sequence points is undefined (that is, it might, or might not, be incremented yet).

easterly81:
This doesn't turn the led off

It does, but it will do turn it back on again as soon as loop comes back around if the switch is high. This will all happen so fast that it appears to have never turned off.