Go Down

Topic: Serial.write and Serial.read Xbee also port manipulation (Read 2 times) previous topic - next topic

tnovak

I finally understand how that will work with a "rollover"! I looked back at some old projects and Ive used it before but never understood why it works.

Unsigned will rollover backwards instead of negatively. So lets say the rollover is 600 just to make it easy...

unsigned int last = 595;
unsigned int current = 25;
current - last = 30        not -570!!

so 5 past before rollover(at 600) and 25 since rollover = 30 total.  Because of the way unsigned works. 25-595 gets to zero at 570 so then count backwards from rollover(600) or (600-570) and get 30.

Hope that mess above can help others understand how they work.


This is the code I used.
Code: [Select]

if(millis() - previousMillis ) >= interval)                     // If time to send
{
   bitWrite(bytetosend, 0, digitalRead(11));              // Read pin 11 and set 0 bit of byte to send.   
   Serial.write(bytetosend);                                     // Send byte
   previousMillis = millis();                                       // Set sent time
}

PaulS

Quote
Unsigned will rollover backwards instead of negatively.

An unsigned variable does not go negative when rollover occurs. It doesn't go backwards, either, though. It simply starts over again at 0, like a car odometer.

One has no trouble determining how far they have traveled if the starting mileage is 99,790 miles and the ending mileage is 420. The computer has no trouble, either.

Go Up