Advertisement
skizziks_53

Arduino Morse Button Tester v1.0

Apr 2nd, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. /*
  2.   Reddit morse button tester
  3.   April 2, 2019
  4.   Uses serial monitor and one NO button.
  5.  
  6.   Instructions:
  7.   View the serial monitor, press the button down and then release it. The board should send a message saying how long the press was.
  8. */
  9.  
  10. int buttonPin = 7; // Attach the button high side here.
  11.  
  12. bool button_pressed = false;
  13.  
  14. int button_press_long = 600; // This is the 'long' press time, in milliseconds.
  15.  
  16. unsigned long button_press_time = 0; // This is the stored time that the button was pressed down.
  17. unsigned long button_release_time = 0; // This is the stored time that the button was released.
  18.  
  19. // The button still needs to be debounced to work properly.
  20. // 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.
  21. int button_debounce_time = 100; // This is the debounce time in milliseconds. Some buttons need longer times than others.
  22. // 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.
  23. bool button_enabled = true;
  24. unsigned long button_db_start_time = 0;
  25. unsigned long button_db_current_time = 0;
  26.  
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.  
  31.   Serial.println("Exiting setup()"); // This is an opening message, just to let you see when the board is up and running.
  32.   // Also it can reveal if there are any errors or bugs that have occurred before this point.
  33. }
  34.  
  35. void loop() {
  36.  
  37.  
  38.   // You can add your own code here.
  39.  
  40.  
  41.  
  42.   // ##############################################################
  43.   // The condition below can only run if the button is not de-bouncing.
  44.   if (button_enabled == true) {
  45.     // The condition below checks the button for presses if it is not already being pressed down.
  46.     if (button_pressed == false) {
  47.       check_button_for_press();
  48.     }
  49.  
  50.     // If the button is being held down, the condition below will catch when it is released.
  51.     if (button_pressed == true) {
  52.       check_button_for_release();
  53.     }
  54.   }
  55.   // ##############################################################
  56.   // The code below is for re-enabling the button if it is disabled because it is de-bouncing.
  57.   if (button_enabled == false) {
  58.     button_db_current_time = millis();
  59.     if (button_db_current_time > button_db_start_time) {
  60.       if (button_db_current_time >= (button_db_start_time + button_debounce_time)) {
  61.         button_enabled = true;
  62.       }
  63.     }
  64.     else {
  65.       button_db_start_time = millis(); // millis() rollover condition.
  66.     }
  67.   }
  68.   // ##############################################################
  69.  
  70.  
  71.   // You can add your own code here.
  72.  
  73.  
  74. }
  75.  
  76. void check_button_for_press() {
  77.   if (digitalRead(buttonPin) == HIGH) {
  78.     start_button_debounce();
  79.     button_pressed_down();
  80.   }
  81. }
  82.  
  83. void start_button_debounce() {
  84.   button_enabled = false;
  85.   button_db_start_time = millis();
  86. }
  87.  
  88. void check_button_for_release() {
  89.   if (digitalRead(buttonPin) == LOW) {
  90.     start_button_debounce();
  91.     button_released();
  92.   }
  93. }
  94.  
  95. void button_pressed_down() {
  96.   button_pressed = true;
  97.   button_press_time = millis();
  98. }
  99.  
  100. void button_released() {
  101.   button_pressed = false;
  102.   button_release_time = millis();
  103.   check_button_press_time();
  104. }
  105.  
  106. void check_button_press_time() {
  107.   long pressTime = button_release_time - button_press_time;
  108.   if (pressTime <= button_press_long) {
  109.     show_short_press(pressTime); // This gets called if the press time is short.
  110.   }
  111.   else {
  112.     show_long_press(pressTime); // This gets called if the press time is long.
  113.   }
  114. }
  115.  
  116. void show_short_press(long timeValue) {
  117.   Serial.print("Short button press = ");
  118.   Serial.print(timeValue);
  119.   Serial.println(" milliseconds");
  120. }
  121.  
  122. void show_long_press(long timeValue) {
  123.   Serial.print("Long button press = ");
  124.   Serial.print(timeValue);
  125.   Serial.println(" milliseconds");
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement