Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 18 July 2018
- Four buttons four LEDs
- Problem 1: LED1 needs to be on only when button1 is held down.
- Right now it takes several presses to turn on and off and stays on.
- Problem 1: LEDs 2,3,4 do not turn off with buttons 2,3,4.
- They are all turned off by button1, which is what I want, but I also
- need them to turn of with their respective button presses.
- */
- // set pins
- const int buttonPin1 = 2; // Pin1's pushbutton
- const int buttonPin2 = 4; // Pin2's pushbutton
- const int buttonPin3 = 6; // Pin3's pushbutton
- const int buttonPin4 = 8; // Pin4's pushbutton
- const int ledPin1 = 3; // Pin1's LED
- const int ledPin2 = 5; // Pin2's LED
- const int ledPin3 = 7; // Pin3's LED
- const int ledPin4 = 9; // Pin4's LED
- // set variables
- int ledPin1_status = 0;
- 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.
- // 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.
- int ledPin2_status = 0;
- int ledPin3_status = 0;
- int ledPin4_status = 0;
- void setup() { //initialize pins
- pinMode(buttonPin1, INPUT); // set PINn as INPUT
- pinMode(buttonPin2, INPUT);
- pinMode(buttonPin3, INPUT);
- pinMode(buttonPin4, INPUT);
- pinMode(ledPin1, OUTPUT); // set LEDn as OUTPUT
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- pinMode(ledPin4, OUTPUT);
- }
- /*
- There is different ways of debouncing buttons, below is just the way I prefer to do it.
- */
- unsigned long previousButtonPressTime = 0; // the last time the output pin was toggled
- unsigned long currentButtonPressTime = 0; // the debounce time; increase if the output flickers
- unsigned long buttonDebounceDelay = 100; // the button debounce time in milliseconds
- bool buttonsEnabled = true; // This is used for disabling all the buttons during debouncing
- // function prototypes -- not strictly needed here but declaring functions is a good habit to aquire
- void turnOffLEDs234();
- void switchLED(int, int);
- void disableButtons();
- void loop() {
- ledPin1_status = digitalRead(buttonPin1);
- if (ledPin1_status != ledPin1_previousStatus) {
- if (ledPin1_status == 1) {
- if (buttonsEnabled == true) {
- switchLED(1, ledPin1_status);
- ledPin1_previousStatus = ledPin1_status; // This line is here is to make sure that this code section only runs when the ledPin1_status changes.
- disableButtons(); // This starts the button debounce timer and disables the buttons.
- // 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:
- turnOffLEDs234();
- // You can also have LEDs 2-3-4 turn off when button 1 is released. That line is #76 further below.
- }
- }
- else { // if (ledPin1_status == 0)
- switchLED(1, ledPin1_status);
- ledPin1_previousStatus = ledPin1_status; // This line is here is to make sure that this code section only runs when the ledPin1_status changes.
- // If you wanted LEDs 2,3 and 4 to turn off when button 1 was released, then you would use the line below:
- //turnOffLEDs234();
- }
- }
- if (buttonsEnabled == true) {
- ledPin2_status = digitalRead(buttonPin2);
- if (ledPin2_status == HIGH) {
- switchLED(2, ledPin2_status);
- disableButtons();
- }
- ledPin3_status = digitalRead(buttonPin3);
- if (ledPin3_status == HIGH) {
- switchLED(3, ledPin3_status);
- disableButtons();
- }
- ledPin4_status = digitalRead(buttonPin4);
- if (ledPin4_status == HIGH) {
- switchLED(4, ledPin4_status);
- disableButtons();
- }
- }
- else { // if (buttonsEnabled == false)
- // Below is checking for the de-bounce time and re-enabling the buttons:
- currentButtonPressTime = millis();
- if (currentButtonPressTime >= previousButtonPressTime) {
- if (currentButtonPressTime >= (previousButtonPressTime + buttonDebounceDelay)) {
- buttonsEnabled = true; // This re-enables the buttons
- }
- }
- else {
- previousButtonPressTime = millis(); // rollover condition
- }
- }
- } // ----------- end of main program loop
- void turnOffLEDs234() {
- // This is just a convenient way to turn off all three LEDs at once.
- // This only turns LEDs 2-3-4 off, as they get turned on individually by their own buttons.
- ledPin2_status = 0;
- switchLED(2, ledPin2_status);
- ledPin3_status = 0;
- switchLED(3, ledPin3_status);
- ledPin4_status = 0;
- switchLED(4, ledPin4_status);
- }
- void switchLED(int LEDnum, int LEDstate) {
- // This function writes the given LED to the given state.
- // All the LEDs are always changed using this one function.
- switch (LEDnum) {
- case 1:
- digitalWrite(ledPin1, LEDstate);
- break;
- case 2:
- digitalWrite(ledPin2, LEDstate);
- break;
- case 3:
- digitalWrite(ledPin3, LEDstate);
- break;
- case 4:
- digitalWrite(ledPin4, LEDstate);
- break;
- //default:
- // statements
- }
- }
- void disableButtons() {
- // This disables all the buttons until the debounce time passes.
- previousButtonPressTime = millis();
- buttonsEnabled = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement