Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit morse button tester
- April 2, 2019
- Uses serial monitor and one NO button.
- Instructions:
- View the serial monitor, press the button down and then release it. The board should send a message saying how long the press was.
- */
- int buttonPin = 7; // Attach the button high side here.
- bool button_pressed = false;
- int button_press_long = 600; // This is the 'long' press time, in milliseconds.
- unsigned long button_press_time = 0; // This is the stored time that the button was pressed down.
- unsigned long button_release_time = 0; // This is the stored time that the button was released.
- // The button still needs to be debounced to work properly.
- // With the typical PCB mount button that I used, I found that it needed to be debounced on the press-down and on the release.
- int button_debounce_time = 100; // This is the debounce time in milliseconds. Some buttons need longer times than others.
- // Note that since the debounce function here gets called on both the pressed and released events, the user cannot have a shorter press time than the debounce time.
- bool button_enabled = true;
- unsigned long button_db_start_time = 0;
- unsigned long button_db_current_time = 0;
- void setup() {
- Serial.begin(9600);
- Serial.println("Exiting setup()"); // This is an opening message, just to let you see when the board is up and running.
- // Also it can reveal if there are any errors or bugs that have occurred before this point.
- }
- void loop() {
- // You can add your own code here.
- // ##############################################################
- // The condition below can only run if the button is not de-bouncing.
- if (button_enabled == true) {
- // The condition below checks the button for presses if it is not already being pressed down.
- if (button_pressed == false) {
- check_button_for_press();
- }
- // If the button is being held down, the condition below will catch when it is released.
- if (button_pressed == true) {
- check_button_for_release();
- }
- }
- // ##############################################################
- // The code below is for re-enabling the button if it is disabled because it is de-bouncing.
- if (button_enabled == false) {
- button_db_current_time = millis();
- if (button_db_current_time > button_db_start_time) {
- if (button_db_current_time >= (button_db_start_time + button_debounce_time)) {
- button_enabled = true;
- }
- }
- else {
- button_db_start_time = millis(); // millis() rollover condition.
- }
- }
- // ##############################################################
- // You can add your own code here.
- }
- void check_button_for_press() {
- if (digitalRead(buttonPin) == HIGH) {
- start_button_debounce();
- button_pressed_down();
- }
- }
- void start_button_debounce() {
- button_enabled = false;
- button_db_start_time = millis();
- }
- void check_button_for_release() {
- if (digitalRead(buttonPin) == LOW) {
- start_button_debounce();
- button_released();
- }
- }
- void button_pressed_down() {
- button_pressed = true;
- button_press_time = millis();
- }
- void button_released() {
- button_pressed = false;
- button_release_time = millis();
- check_button_press_time();
- }
- void check_button_press_time() {
- long pressTime = button_release_time - button_press_time;
- if (pressTime <= button_press_long) {
- show_short_press(pressTime); // This gets called if the press time is short.
- }
- else {
- show_long_press(pressTime); // This gets called if the press time is long.
- }
- }
- void show_short_press(long timeValue) {
- Serial.print("Short button press = ");
- Serial.print(timeValue);
- Serial.println(" milliseconds");
- }
- void show_long_press(long timeValue) {
- Serial.print("Long button press = ");
- Serial.print(timeValue);
- Serial.println(" milliseconds");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement