Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define Variables
- const int output = 5;
- const int output2 = 6;
- const int button = 3;
- const int button2 = 2;
- boolean lastButton = LOW; // Contains last button state
- boolean currentButton = LOW; // Contains current button state
- boolean output_state = LOW; // Present state of output true/on false/off
- boolean lastButton2 = LOW; // Contains last button2 state
- boolean currentButton2 = LOW; // Contains current button2 state
- boolean output_state2 = LOW; // Present state of ouput2 true/on false/off
- void setup()
- {
- // Define pin types
- pinMode(output, OUTPUT);
- pinMode(output2, OUTPUT);
- pinMode(button, INPUT);
- pinMode(button2, INPUT);
- }
- void loop()
- {
- // Set the current button state with the debounced value
- currentButton = debounce(lastButton, button);
- if (lastButton == LOW && currentButton == HIGH) // If button was pressed
- {
- output_state = !output_state; // toggle the output value
- }
- lastButton = currentButton; // Change the state of the button
- digitalWrite(output, output_state); // Set the state of the output
- // Set the current button state with the debounced value
- currentButton2 = debounce(lastButton2, button2);
- if (lastButton2 == LOW && currentButton2 == HIGH) // If button2 was pressed
- {
- output_state2 = !output_state2; // toggle the output2 value
- }
- lastButton2 = currentButton2; // Change the state of the button
- digitalWrite(output2, output_state2); // Set the state of the output
- }
- //-------------------------------------------------------------------------------------
- /* Debouncing Function.
- * Pass in the button number and previous state of the button as an input argumnets.
- * Return as an output argument the current debounced state of the button.
- */
- boolean debounce(boolean last, byte sw)
- {
- boolean current = digitalRead(sw); // Read the state of the button
- if (last != current) // If values are different
- {
- delay(5);
- current = digitalRead(sw); // Read the state of the button again
- }
- return current;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement