Advertisement
shimniok

Arduino Task Scheduling

Jul 18th, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. /* run different tasks at different intervals on Arduino without
  2.  * interrupts or blocking/delay
  3.  */
  4. void loop()
  5. {
  6.     int now = millis(); /* no sense in calling millis over and over */
  7.  
  8.     /* this runs at 1/10ms = 100Hz */
  9.     if ( (now % 10) == 0) { /* short, frequent tasks at top */
  10.         readGyro();
  11.     }
  12.        
  13.     /* 1/50ms = 20Hz */
  14.     if ( (now % 50) == 0) {
  15.         readCompass();
  16.     }
  17.    
  18.     /* this runs whenever there's a character available */
  19.     /* possibly parsing GPS as well ala TinyGPS */
  20.     if ( Serial.available() ) {
  21.         readGPS();
  22.     }
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement