Guest User

Untitled

a guest
May 29th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum States {
  2.   STARTUP,
  3.   WAITING,
  4.   MEASURING,
  5.   SHUTDOWN
  6. };
  7.  
  8. enum States currentState;
  9.  
  10. unsigned int timerCounter;
  11.  
  12. void setup() {
  13.   currentState = STARTUP;
  14. }
  15.  
  16. void loop() {
  17.   switch (currentState) {
  18.     case STARTUP:
  19.       init();
  20.       break;
  21.  
  22.     case WAITING:
  23.       //do nothing
  24.       break;
  25.  
  26.     case MEASURING:
  27.       measure();
  28.       break;
  29.  
  30.     case SHUTDOWN:
  31.       //shutdown();
  32.       break;
  33.  
  34.     default:
  35.       currentState = WAITING;
  36.       break;
  37.   }
  38. }
  39.  
  40. void init() {
  41.   TCCR0B |= (1 << CS02) | (1 << CS00);//prescaler set to Fosc/1024 (see page 142)
  42.   TIMSK0 |= (1 << TOV0);//enable Timer0 Overflow Interrupt (see page 143)
  43.   currentState = WAITING;//<--Change the state!
  44.   sei();//enable interrupts
  45. }
  46.  
  47. void measure()
  48. {
  49.   /* All your measurement and averaging stuff happens here */
  50.   currentState = WAITING;//<--Change the state!
  51. }
  52.  
  53. ISR(TIMER0_OVF_vect)
  54. {
  55.   timerCounter++;
  56.   if (timerCounter >= 54931)
  57.   {
  58.     timerCounter = 0;
  59.     currentState = MEASURING;//<--Change the state!
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment