1

So I am learning arduino code by going through examples and playing with them, and I came across something interesting. I am not fluent in any coding language but I am fully familiarized with computer logic and function so I can read what you suggest just fine.

What I want to know is, why are these two code pieces different?

const int ledPin =  13;     

int ledState = LOW;            

long interval = 1000;          

void setup() {

pinMode(ledPin, OUTPUT);      
}

void loop()
{

  unsigned long currentMillis = millis();

  if(currentMillis > interval) {

    currentMillis = 0;  


    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;


    digitalWrite(ledPin, ledState);
  }
}

This is my code, or rather modification to this code:

const int ledPin =  13;     


int ledState = LOW;           
long previousMillis = 0;      


long interval = 1000;      

void setup() {

  pinMode(ledPin, OUTPUT);      
}

void loop()
{

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;   

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    digitalWrite(ledPin, ledState);
  }

}

How is it that the first code works, and the second code turns the led on and either terminates or never turns the LED off? The code is designed to blink the LED once every 1 second.

2

Ah yes, this problem.

Note the declaration of currentMillis:

unsigned long currentMillis = ...;

And now note the declaration of previousMillis:

long previousMillis = ...;

You wouldn't think that one little word could make that much difference, but it does. It completely changes how C performs mathematics. previousMillis must also be unsigned or you will get undesired results.

  • How so? That is what confuses me. I /can/ fix it in this scenario, but should this happen with my own code I would be stumped. – Sciiiiience Jul 6 '14 at 1:47
  • Answer this question: What is the value of a negative number when stored in an unsigned variable? – Ignacio Vazquez-Abrams Jul 6 '14 at 1:59
  • Unsigned is essentially absolute value, no? So, positive. – Sciiiiience Jul 6 '14 at 2:15
  • It is not the absolute value. You need to be more precise than "positive" to understand. – Ignacio Vazquez-Abrams Jul 6 '14 at 2:16
  • Then what is it? – Sciiiiience Jul 6 '14 at 2:17
0

http://en.wikipedia.org/wiki/Two%27s_complement - good read regarding signed numbers in binary format. It really isn't "complex"; the highest bit being "1" denotes a negative number. As there is no "-0" the negative numbers also have an offset of one.

If you mix unsigned and signed - you simply get unexpected results.

You need to be very precise, i.e. check if the signed number is negative and handle it with care to make it "compatible" with the unsigned number before you use them together. What "care" ends up be in reality depends on what you expect to get out of it, as result.

  • Thanks man I would up vote but I don't have the rep lol – Sciiiiience Jul 6 '14 at 21:19
  • Noted an error there, now it should be ok. – user3043 Jul 6 '14 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.