Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is a sample sketch to show a solution to Simons 4x LED challenge
- * see: https://www.facebook.com/groups/1437500423193520/permalink/1705520479724845/
- *
- * The challenge is that any one of three events can occur
- * Event A : the user can press a button
- * Event B : time has passed
- * Event C : the user releases the button
- *
- * Event A & C can only happen sequentally. That is one event has to happen before the other can happen
- * Ecent B can only happen after event A
- * Event C can happen at any time after event A has happened
- *
- *
- * Test circuit:
- *
- * Connect a push-button to pin 10 (ButtonPin), connect other side of button to ground.
- * Connect LEDs (including resistors) to pins 3, 4, 5 & 6
- *
- * Pin 13 (StatusPin) is used as a I-am-alive-blinking-signal
- *
- * This skect uses the ButtoOne library from https://github.com/MrAlvin/ButtonOne
- *
- */
- #include "ButtonOne.h"
- // Setup a ButtonOne instance on pin 10.
- ButtonOne button(10);
- #define LED1_PIN 3
- #define LED2_PIN 4
- #define LED3_PIN 5
- #define LED4_PIN 6
- #define DEBUG_LED_PIN 13
- boolean button_pressed = false; // flag used to evaluate on event B - the passing of time between activation of LEDs
- unsigned long previousMillis = 0; // used to track the passing of "interval" time
- unsigned long interval = 0; // used to track time to occur between LED activations. Default action is; that LED 1 turns on, as the button is pressed
- int idx = 1; // used keep track of the current state in the state-machine
- //*********************************************
- // setup function - to run once:
- //*********************************************
- void setup() {
- // enable the standard led on pin 13.
- pinMode(DEBUG_LED_PIN, OUTPUT); // sets the digital pin as output
- pinMode(LED1_PIN, OUTPUT); // sets the digital pin as output
- pinMode(LED2_PIN, OUTPUT); // sets the digital pin as output
- pinMode(LED3_PIN, OUTPUT); // sets the digital pin as output
- pinMode(LED4_PIN, OUTPUT); // sets the digital pin as output
- //initiate internal button management values
- button.begin();
- // link the Press function to be called on a button Press event.
- button.attachPress(btnPress);
- // link the Release function to be called on a button Release event.
- button.attachRelease(btnRelease);
- } // setup
- //*********************************************
- // main code - to run repeatedly:
- //*********************************************
- void loop() {
- // check for event A (button press)
- button.check();
- // if event A has occured, check for event B
- if(button_pressed) is_it_time();
- // something I usually use when dealing with event-style programming
- debug_blink(); // lets me know that the program is up and running
- } // loop
- //*********************************************
- // button events:
- //*********************************************
- // this function will be called when the button is pressed
- void btnPress() {
- button_pressed = true; // raise flag. Response to flag happens in the main loop()
- } // btnPress
- // this function will be called when the button is released - which is also event C
- void btnRelease() {
- button_pressed = false; // button is no longer pressed
- LedsOff(); // turn off all LEDs
- idx = 1; // reset the pointer in the state machine, so ready for next press of the button
- previousMillis = 0; // reset the time tracker
- interval = 0; // set time interval for time to pass between pressing of button to turn on LED 1
- } // btnRelease
- //*********************************************
- // timer function:
- //*********************************************
- void is_it_time() {
- if (millis() - previousMillis > interval) {
- previousMillis = millis(); // store the current time, so we can waite "interval" amount of time, before we take next action
- // do you timed event
- timed_action();
- }
- } //is_it_time
- //*********************************************
- // state machine:
- //*********************************************
- void timed_action() {
- switch(idx){
- case 1: //turn on one led
- led_1_on();
- interval = 1000; // time until LED 2 will be turned on
- idx++; // ready to activate LED 2
- break;
- case 2: //turn on two leds
- led_2_on();
- interval = 1000; // time until LED 3 will be turned on
- idx++; // ready to activate LED 3
- break;
- case 3: //turn on three leds
- led_3_on();
- interval = 1000; // time until LED 4 will be turned on
- idx++; // ready to activate LED 4
- break;
- case 4: //turn on four leds
- led_4_on();
- idx++; // ready to simply waite
- break;
- case 5: //no changes
- break;
- }
- } //timed_action
- //*********************************************
- // LED functions:
- //*********************************************
- void LedsOff(){
- led_1_off();
- led_2_off();
- led_3_off();
- led_4_off();
- }
- void led_1_on(){
- digitalWrite(LED1_PIN, HIGH);
- }
- void led_1_off(){
- digitalWrite(LED1_PIN, LOW);
- }
- void led_2_on(){
- digitalWrite(LED1_PIN, HIGH);
- }
- void led_2_off(){
- digitalWrite(LED1_PIN, LOW);
- }
- void led_3_on(){
- digitalWrite(LED1_PIN, HIGH);
- }
- void led_3_off(){
- digitalWrite(LED1_PIN, LOW);
- }
- void led_4_on(){
- digitalWrite(LED1_PIN, HIGH);
- }
- void led_4_off(){
- digitalWrite(LED1_PIN, LOW);
- }
- //*********************************************
- // I-am-alive debug LED blink function:
- //*********************************************
- #define ON_DELAY 500
- #define OFF_DELAY 500
- void debug_blink() {
- static unsigned long blink_millis = 0;
- static unsigned long blink_interval = 0;
- static boolean do_on = true;
- if ( millis() - blink_millis > blink_interval ) { // if its time to change the blink
- if (do_on) { //use a flag to determine wether to turn on or off the Blink LED
- digitalWrite(DEBUG_LED_PIN, HIGH); // set the LED on, if okay to use power for it
- blink_millis = millis();
- blink_interval = ON_DELAY; // wait for a second
- do_on = false;
- }else{
- digitalWrite(DEBUG_LED_PIN, LOW); // set the LED off
- // set the time to do next blink
- blink_millis = millis();
- blink_interval = OFF_DELAY; // wait for a second
- do_on = true;
- }
- }
- } //debug_blink
- // End
Advertisement
Add Comment
Please, Sign In to add comment