Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUTTONPIN 12
- #define LEDPIN 11
- #define LEDFLASHTIME 200 //the flash interval for the LED
- #define CYCLELIMIT 4 //the amount of different cycles before it loops back to 0
- #define LOCKOUTTIME 500 //this is the time the button is ignore after a press in milliseconds
- #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?
- boolean button_state=false; //holds the current buttons value
- boolean button_lockout=false; //used to prevent the state from changing if button is just always activated
- uint32_t button_lockout_timer=0; //this is used to ignore the button after an initial trigger, it's a lockout timer.
- int state_cycle=0; //increments everytime the button is pressed until it reaches CYCLELIMIT then resets
- 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
- int loopcounter=0; //just a general purpose loop counter
- 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
- uint32_t ledtimer=0; //timer used to flash the led
- void setup() {
- Serial.begin(9600);
- pinMode(BUTTONPIN,INPUT); //this isn't actually required at all, all pins are inputs by default but it's good practice
- 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.
- pinMode(LEDPIN,OUTPUT); //set led pin as output
- digitalWrite(LEDPIN,LOW); //set the initial state to low, not required but again good practice
- }
- void loop(){
- if(millis()>button_lockout_timer){ //check to make sure the timer has expired, if so continue with processing button
- button_state=digitalRead(BUTTONPIN); //read the pin state and assign into variable
- 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.
- button_lockout_timer=millis()+LOCKOUTTIME; //set the lockout timer to prevent re-activation from happening too soon
- if(!button_lockout){ //make sure the button isn't locked out
- if(!ENABLEAUTOREPEAT) //check to see if we want the cycle to keep changing or not
- button_lockout=true;
- state_cycle=state_cycle+1; //increment the cycle variable
- state_cycle=state_cycle%CYCLELIMIT; //this is the modulo operator which makes the value reset back to 0 when it reaches the CYCLELIMIT
- Serial.println("Button was pressed!");
- }
- }
- else{ //the button was released
- button_lockout=false; //if the button was released we can re-enable by disabling the lockout
- }
- }
- if(state_cycle!=state_cycle_pre){ //this becomes true if the cycle just changed and will execute only once on every change
- Serial.print("Cycle update: ");
- Serial.println(state_cycle);
- state_cycle_pre=state_cycle; //update the previous value with current which will prevent repetative executions
- }
- switch(state_cycle){ //This will run the appropriate code every loop itteration
- case 0:
- //do nothing? sometimes it's useful to have a state which does nothing maybe?
- break;
- case 1:
- runTask1();
- break;
- case 2:
- runTask2();
- break;
- case 3:
- //just extra defenition, can add or remove cases as desired
- break;
- 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)
- Serial.print("No defenition for cycle: ");
- Serial.println(state_cycle);
- break;
- }
- }
- void runTask1(){
- Serial.print("Running Task 1");
- for(int i=0;i<loopcounter;i++)
- Serial.print(".");
- Serial.println();
- loopcounter=(loopcounter+1)%3;//increment with modulo to keep it within limit
- }
- void runTask2(){
- if(millis()>ledtimer){
- ledtimer=millis()+LEDFLASHTIME; //set the timer for next update
- ledstate=!ledstate; //set the state to the inverted state
- digitalWrite(LEDPIN,ledstate);
- Serial.print("LED: ");
- Serial.println( ((ledstate)? "On":"Off") ); //this is a ternary operator, handy for doing stuff like this
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement