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?
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.