Pages: [1]   Go Down
Author Topic: Re: Another multiples LEDs blinking question  (Read 27 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also have a question..and I am a newbie...
I would like to modify the code below to use the 'blink without delay' or "millis()" type code to do the same thing as the code below.

I've been racking my brain for a week on this, any and all help would be appreciated.


int led = 13; // blink 'digital' pin 13 - AKA the built in red LED

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
    digitalWrite(led, HIGH);
    delay(10);
    digitalWrite(led, LOW);
    delay(990);
}

Please note the delay times... for a strobe affect.

thanks Rocco
Logged

Offline Offline
God Member
*****
Karma: 37
Posts: 759
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

like this:

Code:
int led = 13;
unsigned long startTime;
unsigned long pulse = 10UL;
unsigned long interval = 1000UL;

void setup()
{
  pinMode(led, OUTPUT);
}
//
void loop()
{
  flash();
}

void flash()
{
  if (millis() - startTime < pulse)
  {
    digitalWrite(led, HIGH);
  }
  else if (millis() - startTime < interval)
  {
    digitalWrite(led, LOW);
  }
  else startTime += interval;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Bulldog!!! you make it look ez.
Now I need to learn what it all does, and I will.

Thanks again.
Logged

Offline Offline
God Member
*****
Karma: 37
Posts: 759
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Bulldog!!! you make it look ez.
Now I need to learn what it all does, and I will.

Thanks again.


moderator: please split this thread

it is easy if you think about millis() as a timestamp:

assign the current time to startTime:

Code:
startTime = millis();

how much time has elapsed?

Code:
millis() - startTime = elapsedTime;

has my timer expired?

Code:
if millis()- startTime > interval;

then start the timer all over by adding the interval to the startTime:

Code:
else startTime += interval;

above is equivalent to :

Code:
else startTime = startTime + interval;





Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


it is easy if you think about millis() as a timestamp:


Thanks for laying it out, I get it now...
I needed to get the math right, and that "else if (millis() - startTime < interval)"
line... I was way off with my code.

Thanks

Rocco
Logged

Pages: [1]   Go Up
Jump to: