Code Working in Reverse?

Hi All,

I too a very long time to grasp the Millis() concept.

Now that I THINK I understand it i have writen my own little program to blink the onboard LED on the Arduino.

The only problem i Have is that it works in reverse.

The on time is actually turning the LED off for that set of time. And the off time is turning it on for that period of time.

Can someone please tell me what is happening here?

int led = 13;
int on = 1000;
int off = 100;
int toggle = 0;
unsigned long previosMillis = 0;

void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}

void loop()
{
unsigned long currentMillis = millis();

if (currentMillis - previosMillis >= on && toggle == 0)
   {
    digitalWrite(led, HIGH);
    toggle = 1;
    previosMillis = currentMillis;
   }

if (currentMillis - previosMillis >= off && toggle == 1)
   {
    digitalWrite(led, LOW);
    toggle = 0;
    previosMillis = currentMillis;
   }
}

so my LED is now off for 1000 milliseconds and on for 100 milliseconds. WHY????
It should be to this program on for 1000 millisecond and off for 100 milliseconds.

How is the LED wired ?

Im using the onboard LED.

Your digtalWrite() of the led is backwards.

When the on time is exceeded, you want to turn the led off, and when the off time is exceeded you turn it on.

int led = 13;
int on = 1000;
int off = 100;
int toggle = 0;
unsigned long previosMillis = 0;

void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}

void loop()
{
unsigned long currentMillis = millis();

if (currentMillis - previosMillis >= on && toggle == 0)
   {
    digitalWrite(led, LOW);
    toggle = 1;
    previosMillis = currentMillis;
   }

if (currentMillis - previosMillis >= off && toggle == 1)
   {
    digitalWrite(led, HIGH);
    toggle = 0;
    previosMillis = currentMillis;
   }
}

This

if (currentMillis - previosMillis >= on && toggle == 0)

translates to:
“if on-time has elapsed AND the LED is off”. You want it the other way around. Just change one of the conditions