Pages: [1]   Go Down
Author Topic: millis madness  (Read 455 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to build a simple on delay timer. I would like to press and hold a pushbutton for 5 seconds and then a LED would come on.
Code:
int ledPin =  13;     
 int buttonPin = 2;
int ledState = LOW;     
int state=0;
long previousMillis = 0;     
long interval = 5000;

void setup() {
    pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); 
}

void loop()
{
    unsigned long currentMillis = millis();
  state=digitalRead(buttonPin);
 
  if (state==HIGH) 
  {
    if(currentMillis - previousMillis > interval )
    previousMillis = currentMillis;     
   
    if (ledState == LOW)
      ledState = HIGH;}
      else{
      ledState= LOW;
 
    digitalWrite(ledPin, ledState);
  }
}
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7268
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to build a simple on delay timer. I would like to press and hold a pushbutton for 5 seconds and then a LED would come on.

And? (hint: anything wrong?)
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The LED never comes on.
Logged

Southern Ontario
Offline Offline
Sr. Member
****
Karma: 2
Posts: 279
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I fixed the indenting to make the code a little clearer...
Code:
void loop()
{
unsigned long currentMillis = millis();
state=digitalRead(buttonPin);

if (state==HIGH) 
{
if(currentMillis - previousMillis > interval )
previousMillis = currentMillis;     

if (ledState == LOW)
ledState = HIGH;
}
else
{
ledState= LOW;
digitalWrite(ledPin, ledState);
}
}

The only time you call digitalwrite on the led is when it's low.  You then set it low... 
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7268
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You did most of the OP's work smiley

OP, please re-do your logic now that nickvd has properly indented your code. Just give it a try.
Logged


Pages: [1]   Go Up
Jump to: