Pages: 1 [2]   Go Down
Author Topic: Serial.write and Serial.read Xbee also port manipulation  (Read 1987 times)
0 Members and 1 Guest are viewing this topic.
Iowa
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: