Advertisement
sanelss

Untitled

May 25th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. #define BUTTONPIN 12
  2. #define LEDPIN 11
  3. #define LEDFLASHTIME 200 //the flash interval for the LED
  4. #define CYCLELIMIT 4 //the amount of different cycles before it loops back to 0
  5. #define LOCKOUTTIME 500 //this is the time the button is ignore after a press in milliseconds
  6. #define ENABLEAUTOREPEAT false //if the button is just held down should it only increment once and no more until released and pressed again or auto increment at the LOCKOUTTIME interval?
  7.  
  8. boolean button_state=false; //holds the current buttons value
  9. boolean button_lockout=false; //used to prevent the state from changing if button is just always activated
  10. uint32_t button_lockout_timer=0; //this is used to ignore the button after an initial trigger, it's a lockout timer.
  11.  
  12.  
  13. int state_cycle=0; //increments everytime the button is pressed until it reaches CYCLELIMIT then resets
  14. int state_cycle_pre=0; //used to keep track if the state_cycle variable has updated so we only execute functions when the cycle changes
  15.  
  16. int loopcounter=0;   //just a general purpose loop counter
  17.  
  18. boolean ledstate=false; //used to keep track of the led state, we could read the pin instead but i prefer keeping all states known within code
  19. uint32_t ledtimer=0; //timer used to flash the led
  20.  
  21. void setup()   {                
  22.   Serial.begin(9600);
  23.   pinMode(BUTTONPIN,INPUT); //this isn't actually required at all, all pins are inputs by default but it's good practice
  24.   digitalWrite(BUTTONPIN,HIGH); //when you write a state to an input pin, it activates the internal pulldown/pullup resistors, in this case pullup. Otherwise the pin will be floating and the data will be junk. An external pull down or pull up can be added instead though.
  25.  
  26.   pinMode(LEDPIN,OUTPUT);  //set led pin as output
  27.   digitalWrite(LEDPIN,LOW); //set the initial state to low, not required but again good practice
  28. }  
  29.  
  30. void loop(){
  31.   if(millis()>button_lockout_timer){  //check to make sure the timer has expired, if so continue with processing button
  32.     button_state=digitalRead(BUTTONPIN);    //read the pin state and assign into variable
  33.     if(button_state==LOW){  //check if the pin is low(so the button would go to ground), this may seem opposite but it's often better to check for low level logic with a pull up resitor on the pin. This is most resiliant to noise and interference.
  34.       button_lockout_timer=millis()+LOCKOUTTIME; //set the lockout timer to prevent re-activation from happening too soon
  35.       if(!button_lockout){ //make sure the button isn't locked out
  36.         if(!ENABLEAUTOREPEAT) //check to see if we want the cycle to keep changing or not
  37.            button_lockout=true;
  38.         state_cycle=state_cycle+1; //increment the cycle variable
  39.         state_cycle=state_cycle%CYCLELIMIT; //this is the modulo operator which makes the value reset back to 0 when it reaches the CYCLELIMIT
  40.         Serial.println("Button was pressed!");
  41.       }
  42.     }
  43.     else{ //the button was released
  44.       button_lockout=false; //if the button was released we can re-enable by disabling the lockout
  45.     }
  46.    
  47.   }
  48.  
  49.   if(state_cycle!=state_cycle_pre){    //this becomes true if the cycle just changed and will execute only once on every change
  50.     Serial.print("Cycle update: ");
  51.     Serial.println(state_cycle);
  52.     state_cycle_pre=state_cycle;  //update the previous value with current which will prevent repetative executions
  53.   }
  54.  
  55.   switch(state_cycle){ //This will run the appropriate code every loop itteration
  56.     case 0:
  57.       //do nothing? sometimes it's useful to have a state which does nothing maybe?
  58.       break;
  59.     case 1:
  60.       runTask1();
  61.       break;
  62.     case 2:
  63.       runTask2();
  64.        break;
  65.     case 3:
  66.       //just extra defenition, can add or remove cases as desired
  67.       break;
  68.     default:  //this should not execute, but will if state_cycle is a value that isn't defined (such as if you increase CYCLELIMIT without more defenitions or if you remove one and forget)
  69.       Serial.print("No defenition for cycle: ");
  70.       Serial.println(state_cycle);
  71.       break;
  72.   }
  73.  
  74. }
  75.  
  76. void runTask1(){
  77.   Serial.print("Running Task 1");
  78.   for(int i=0;i<loopcounter;i++)
  79.     Serial.print(".");
  80.   Serial.println();
  81.   loopcounter=(loopcounter+1)%3;//increment with modulo to keep it within limit
  82. }
  83.  
  84. void runTask2(){
  85.   if(millis()>ledtimer){
  86.     ledtimer=millis()+LEDFLASHTIME;  //set the timer for next update
  87.     ledstate=!ledstate;    //set the state to the inverted state
  88.     digitalWrite(LEDPIN,ledstate);
  89.     Serial.print("LED: ");
  90.     Serial.println( ((ledstate)? "On":"Off") );   //this is a ternary operator, handy for doing stuff like this
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement