Skip to main content
add cast comment
Source Link
slash-dev
  • 2k
  • 14
  • 12

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -32768 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also In your if statements, this statement needs parenthesesyou must cast the millis to bethe same (smaller) type: (unsigned int)millis(). Also, parentheses make the expression more clear:

      else if (((unsigned int)millis() - time > 1000U) && ((unsigned int)millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -32768 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also, this statement needs parentheses to be clear:

      else if ((millis() - time > 1000U) && (millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -32768 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U. In your if statements, you must cast the millis to the same (smaller) type: (unsigned int)millis(). Also, parentheses make the expression more clear:

      else if (((unsigned int)millis() - time > 1000U) && ((unsigned int)millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}
rollover number
Source Link
slash-dev
  • 2k
  • 14
  • 12

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -132768 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also, this statement needs parentheses to be clear:

      else if ((millis() - time > 1000U) && (millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -1 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also, this statement needs parentheses to be clear:

      else if ((millis() - time > 1000U) && (millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -32768 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also, this statement needs parentheses to be clear:

      else if ((millis() - time > 1000U) && (millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}
Source Link
slash-dev
  • 2k
  • 14
  • 12

time should be an unsigned int. As soon as millis reaches 32768, it will be stored as -1 in time, and your comparisons will not work. This happens after 32.768 seconds. Here's a short sketch to illustrate:

int time = 0;

void setup() {
  Serial.begin( 9600 );
}

void loop() {
    time = millis();
    if (time % 1000 == 0) {
      Serial.println( time );
      delay( 2 ); // let millis() increment
    }
}

To see the difference, just change the first line to

unsigned int time = 0;

Note how time rolls over to 0 after 65535 (well, only 65000 is printed).

I would recommend using unsigned constants by adding a "U" after the digits: 1000U.

Also, this statement needs parentheses to be clear:

      else if ((millis() - time > 1000U) && (millis() - tempo < 2000U)) {digitalWrite(led_2, HIGH);}