Advertisement
skizziks_53

Four buttons four LEDs

Jul 18th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. /*
  2.   18 July 2018
  3.  
  4.   Four buttons four LEDs
  5.  
  6.   Problem 1: LED1 needs to be on only when button1 is held down.
  7.   Right now it takes several presses to turn on and off and stays on.
  8.   Problem 1: LEDs 2,3,4 do not turn off with buttons 2,3,4.
  9.   They are all turned off by button1, which is what I want, but I also
  10.   need them to turn of with their respective button presses.
  11. */
  12.  
  13. // set pins
  14. const int buttonPin1 = 2;                 // Pin1's pushbutton
  15. const int buttonPin2 = 4;                 // Pin2's pushbutton
  16. const int buttonPin3 = 6;                 // Pin3's pushbutton
  17. const int buttonPin4 = 8;                 // Pin4's pushbutton
  18. const int ledPin1 = 3;                    // Pin1's LED
  19. const int ledPin2 = 5;                    // Pin2's LED
  20. const int ledPin3 = 7;                    // Pin3's LED
  21. const int ledPin4 = 9;                    // Pin4's LED
  22.  
  23. // set variables
  24. int ledPin1_status = 0;
  25. int ledPin1_previousStatus = 0; // If you want to detect a change of a pin value, then you should store the previous value AND the current value.
  26. // The previous value is used to compare to the current value, and then (if the two are different) the current value is used to change the PED pin status.
  27. int ledPin2_status = 0;
  28. int ledPin3_status = 0;
  29. int ledPin4_status = 0;
  30.  
  31. void setup() {  //initialize pins
  32.   pinMode(buttonPin1, INPUT); // set PINn as INPUT
  33.   pinMode(buttonPin2, INPUT);
  34.   pinMode(buttonPin3, INPUT);
  35.   pinMode(buttonPin4, INPUT);
  36.   pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
  37.   pinMode(ledPin2, OUTPUT);
  38.   pinMode(ledPin3, OUTPUT);
  39.   pinMode(ledPin4, OUTPUT);
  40. }
  41.  
  42. /*
  43.    There is different ways of debouncing buttons, below is just the way I prefer to do it.
  44. */
  45. unsigned long previousButtonPressTime = 0;  // the last time the output pin was toggled
  46. unsigned long currentButtonPressTime = 0;    // the debounce time; increase if the output flickers
  47. unsigned long buttonDebounceDelay = 100; // the button debounce time in milliseconds
  48. bool buttonsEnabled = true; // This is used for disabling all the buttons during debouncing
  49.  
  50.  
  51. // function prototypes -- not strictly needed here but declaring functions is a good habit to aquire
  52. void turnOffLEDs234();
  53. void switchLED(int, int);
  54. void disableButtons();
  55.  
  56.  
  57. void loop() {
  58.  
  59.   ledPin1_status = digitalRead(buttonPin1);
  60.   if (ledPin1_status != ledPin1_previousStatus) {
  61.     if (ledPin1_status == 1) {
  62.       if (buttonsEnabled == true) {
  63.         switchLED(1, ledPin1_status);
  64.         ledPin1_previousStatus = ledPin1_status; // This line is here is to make sure that this code section only runs when the ledPin1_status changes.
  65.         disableButtons(); // This starts the button debounce timer and disables the buttons.
  66.         // If you wanted LEDs 2,3 and 4 to turn off when button 1 was pressed down, then you would un-comment the line below:
  67.         turnOffLEDs234();
  68.         // You can also have LEDs 2-3-4 turn off when button 1 is released. That line is #76 further below.
  69.       }
  70.     }
  71.     else { // if (ledPin1_status == 0)
  72.       switchLED(1, ledPin1_status);
  73.       ledPin1_previousStatus = ledPin1_status; // This line is here is to make sure that this code section only runs when the ledPin1_status changes.
  74.       // If you wanted LEDs 2,3 and 4 to turn off when button 1 was released, then you would use the line below:
  75.       //turnOffLEDs234();
  76.     }
  77.   }
  78.  
  79.  
  80.   if (buttonsEnabled == true) {
  81.     ledPin2_status = digitalRead(buttonPin2);
  82.     if (ledPin2_status == HIGH) {
  83.       switchLED(2, ledPin2_status);
  84.       disableButtons();
  85.     }
  86.  
  87.     ledPin3_status = digitalRead(buttonPin3);
  88.     if (ledPin3_status == HIGH) {
  89.       switchLED(3, ledPin3_status);
  90.       disableButtons();
  91.     }
  92.  
  93.     ledPin4_status = digitalRead(buttonPin4);
  94.     if (ledPin4_status == HIGH) {
  95.       switchLED(4, ledPin4_status);
  96.       disableButtons();
  97.     }
  98.   }
  99.   else { // if (buttonsEnabled == false)
  100.     // Below is checking for the de-bounce time and re-enabling the buttons:
  101.     currentButtonPressTime = millis();
  102.     if (currentButtonPressTime >= previousButtonPressTime) {
  103.       if (currentButtonPressTime >= (previousButtonPressTime + buttonDebounceDelay)) {
  104.         buttonsEnabled = true; // This re-enables the buttons
  105.       }
  106.     }
  107.     else {
  108.       previousButtonPressTime = millis(); // rollover condition
  109.     }
  110.   }
  111.  
  112. } // ----------- end of main program loop
  113.  
  114.  
  115.  
  116. void turnOffLEDs234() {
  117.   // This is just a convenient way to turn off all three LEDs at once.
  118.   // This only turns LEDs 2-3-4 off, as they get turned on individually by their own buttons.
  119.   ledPin2_status = 0;
  120.   switchLED(2, ledPin2_status);
  121.   ledPin3_status = 0;
  122.   switchLED(3, ledPin3_status);
  123.   ledPin4_status = 0;
  124.   switchLED(4, ledPin4_status);
  125. }
  126.  
  127.  
  128.  
  129. void switchLED(int LEDnum, int LEDstate) {
  130.   // This function writes the given LED to the given state.
  131.   // All the LEDs are always changed using this one function.
  132.   switch (LEDnum) {
  133.     case 1:
  134.       digitalWrite(ledPin1, LEDstate);
  135.       break;
  136.     case 2:
  137.       digitalWrite(ledPin2, LEDstate);
  138.       break;
  139.     case 3:
  140.       digitalWrite(ledPin3, LEDstate);
  141.       break;
  142.     case 4:
  143.       digitalWrite(ledPin4, LEDstate);
  144.       break;
  145.       //default:
  146.       // statements
  147.   }
  148. }
  149.  
  150.  
  151.  
  152. void disableButtons() {
  153.   // This disables all the buttons until the debounce time passes.
  154.   previousButtonPressTime = millis();
  155.   buttonsEnabled = false;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement