Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit button press timer v1.0
- 17 March 2019
- This sketch just uses a NO button and lights up the pin# 13 LED, so it works on any board.
- Note: this sketch suffers from the button bouncing when it is released, since there is no code just for de-bouncing the button itself... :P
- */
- int ledPin = 13;
- int led_state = 1; // Since you want the LED to blink on and off, a separate value is used to track that.
- int switchPin = 3;
- unsigned long longDuration = 5000;
- int button_debounce_time = 300; // de-bounce time, in milliseconds
- unsigned long pressBeginTime = 0; // This is the time that the button was pressed down.
- unsigned long pressCurrentTime = 0; // This is the current time, for comparing to the above value.
- int buttonState = 0; // There are two different button states used: zero = not pressed, 1 = pressed.
- bool led_blinking = false;
- int led_blink_time = 500; // This is a milliseconds value to blink the LED on and off.
- unsigned long led_blink_beginTime = 0; // This variable is just for timing the LED blinking on and off.
- void setup() {
- Serial.begin(9600);
- pinMode(switchPin, INPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, led_state);
- Serial.println("Exiting setup."); // This is a message that setup() has completed.
- // This is useful because if you make an error somewhere in setup() that the compiler does not catch, the processor will often crash and just skip all the remaining lines of setup() and go on to the main loop.
- // So if this line doesn't print out, you would know that something was wrong inside setup().
- }
- void loop() {
- if (buttonState == 0) {
- if (digitalRead(switchPin) == HIGH) {
- // These three conditions above only happen all at once when the button is first pressed down.
- buttonPress_begin();
- }
- }
- if (buttonState == 1) {
- if (digitalRead(switchPin) == HIGH) {
- // These two conditions above only happen at once when the button is being held down.
- check_if_led_is_running();
- }
- }
- if (buttonState == 1) {
- if (digitalRead(switchPin) == LOW) {
- // These two conditions above only happen at once when the button has been released.
- buttonPress_end();
- }
- }
- } // end of main loop
- void buttonPress_begin() {
- // This should only be called one time when the button is first pressed down.
- Serial.println("button pressed");
- pressBeginTime = millis();
- flip_buttonState();
- }
- void flip_buttonState() {
- if (buttonState == 0) {
- buttonState = 1;
- }
- else {
- buttonState = 0;
- }
- }
- void check_if_led_is_running() {
- // This function is just to check if the LED needs to be started blinking, or if it is already started, if it needs to be changed.
- pressCurrentTime = millis();
- if ((pressCurrentTime - pressBeginTime) <= longDuration) {
- if (led_blinking == false) {
- led_blinking = true; // It is necessary to set led_blink_beginTime to get the LED to blink
- start_led_blinker(); // so this is where that is done
- }
- else {
- check_led_blinker(); // Once led_blinking is true, this can just flip the LED on and off.
- }
- }
- else {
- led_blinking = false;
- led_state = 0;
- digitalWrite(ledPin, led_state);
- }
- }
- void start_led_blinker() {
- // This is only called when the led first begins to blink.
- led_blink_beginTime = millis();
- flip_led_state();
- }
- void check_led_blinker() {
- // This is only called after the led has already been started blinking.
- if (pressCurrentTime >= (led_blink_beginTime + led_blink_time)) {
- led_blink_beginTime = millis(); // Reset the led blink time to the current time.
- flip_led_state();
- show_button_press_time();
- }
- }
- void flip_led_state() {
- // This just flips the LED pin state between zero and 1, and then writes it to the pin.
- if (led_state == 1) {
- led_state = 0;
- }
- else {
- led_state = 1;
- }
- digitalWrite(ledPin, led_state);
- }
- void show_button_press_time() {
- long totalTime = pressCurrentTime - pressBeginTime;
- Serial.print("ms = ");
- Serial.println(totalTime);
- }
- void buttonPress_end() {
- // This function only gets called when the button is released.
- Serial.println("button released");
- flip_buttonState();
- pressCurrentTime = millis();
- show_button_press_time();
- led_state = 1;
- digitalWrite(ledPin, led_state);
- }
Add Comment
Please, Sign In to add comment