Components and supplies
Arduino UNO
Project description
Code
Sketch
arduino
1 2// constants won't change. They're used here to 3// set pin numbers: 4const int button1 = 1; // the number of the pushbutton pin 5const int button2 = 2; 6const int ledPin1 = 3; // the number of the LED pin 7const int ledPin2 = 4; 8// Variables will change: 9int ledState1 = HIGH; // the current state of the output pin 10int ledState2 = HIGH; 11int buttonState1; // the current reading from the input pin 12int buttonState2; 13int lastButtonState1 = LOW; // the previous reading from the input pin 14int lastButtonState2 = LOW; 15 16// the following variables are unsigned long's because the time, measured in miliseconds, 17// will quickly become a bigger number than can be stored in an int. 18unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 19unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers 20 21void setup() { 22 pinMode(button1, INPUT); 23 pinMode(button2, INPUT); 24 pinMode(ledPin1, OUTPUT); 25 pinMode(ledPin2, OUTPUT); 26 27 // set initial LED state 28 digitalWrite(ledPin1, ledState1); 29 digitalWrite(ledPin2, ledState2); 30} 31 32void loop() { 33 // read the state of the switch into a local variable: 34 int reading1 = digitalRead(button1); 35 int reading2 = digitalRead(button2); 36 37 // check to see if you just pressed the button 38 // (i.e. the input went from LOW to HIGH), and you've waited 39 // long enough since the last press to ignore any noise: 40 41 // If the switch changed, due to noise or pressing: 42 if (reading1 != lastButtonState1) { 43 // reset the debouncing timer 44 lastDebounceTime = millis(); 45 } 46 47 if ((millis() - lastDebounceTime) > debounceDelay) { 48 // whatever the reading is at, it's been there for longer 49 // than the debounce delay, so take it as the actual current state: 50 51 // if the button state has changed: 52 if (reading1 != buttonState1) { 53 buttonState1 = reading1; 54 55 // only toggle the LED if the new button state is HIGH 56 if (buttonState1 == HIGH) { 57 ledState1 = !ledState1; 58 } 59 } 60 } 61 62 // set the LED: 63 digitalWrite(ledPin1, ledState1); 64 65 // save the reading. Next time through the loop, 66 // it'll be the lastButtonState: 67 lastButtonState1 = reading1; 68 69 70 // If the switch changed, due to noise or pressing: 71 if (reading2 != lastButtonState2) { 72 // reset the debouncing timer 73 lastDebounceTime = millis(); 74 } 75 76 if ((millis() - lastDebounceTime) > debounceDelay) { 77 // whatever the reading is at, it's been there for longer 78 // than the debounce delay, so take it as the actual current state: 79 80 // if the button state has changed: 81 if (reading2 != buttonState2) { 82 buttonState2 = reading2; 83 84 // only toggle the LED if the new button state is HIGH 85 if (buttonState2 == HIGH) { 86 ledState2 = !ledState2; 87 } 88 } 89 } 90 91 // set the LED: 92 digitalWrite(ledPin2, ledState2); 93 94 // save the reading. Next time through the loop, 95 // it'll be the lastButtonState: 96 lastButtonState2 = reading2; 97}
Sketch
arduino
1 2// constants won't change. They're used here to 3// set pin numbers: 4const int button1 = 1; // the number of the pushbutton pin 5const int button2 = 2; 6const int ledPin1 = 3; // the number of the LED pin 7const int ledPin2 = 4; 8// Variables will change: 9int ledState1 = HIGH; // the current state of the output pin 10int ledState2 = HIGH; 11int buttonState1; // the current reading from the input pin 12int buttonState2; 13int lastButtonState1 = LOW; // the previous reading from the input pin 14int lastButtonState2 = LOW; 15 16// the following variables are unsigned long's because the time, measured in miliseconds, 17// will quickly become a bigger number than can be stored in an int. 18unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 19unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers 20 21void setup() { 22 pinMode(button1, INPUT); 23 pinMode(button2, INPUT); 24 pinMode(ledPin1, OUTPUT); 25 pinMode(ledPin2, OUTPUT); 26 27 // set initial LED state 28 digitalWrite(ledPin1, ledState1); 29 digitalWrite(ledPin2, ledState2); 30} 31 32void loop() { 33 // read the state of the switch into a local variable: 34 int reading1 = digitalRead(button1); 35 int reading2 = digitalRead(button2); 36 37 // check to see if you just pressed the button 38 // (i.e. the input went from LOW to HIGH), and you've waited 39 // long enough since the last press to ignore any noise: 40 41 // If the switch changed, due to noise or pressing: 42 if (reading1 != lastButtonState1) { 43 // reset the debouncing timer 44 lastDebounceTime = millis(); 45 } 46 47 if ((millis() - lastDebounceTime) > debounceDelay) { 48 // whatever the reading is at, it's been there for longer 49 // than the debounce delay, so take it as the actual current state: 50 51 // if the button state has changed: 52 if (reading1 != buttonState1) { 53 buttonState1 = reading1; 54 55 // only toggle the LED if the new button state is HIGH 56 if (buttonState1 == HIGH) { 57 ledState1 = !ledState1; 58 } 59 } 60 } 61 62 // set the LED: 63 digitalWrite(ledPin1, ledState1); 64 65 // save the reading. Next time through the loop, 66 // it'll be the lastButtonState: 67 lastButtonState1 = reading1; 68 69 70 // If the switch changed, due to noise or pressing: 71 if (reading2 != lastButtonState2) { 72 // reset the debouncing timer 73 lastDebounceTime = millis(); 74 } 75 76 if ((millis() - lastDebounceTime) > debounceDelay) { 77 // whatever the reading is at, it's been there for longer 78 // than the debounce delay, so take it as the actual current state: 79 80 // if the button state has changed: 81 if (reading2 != buttonState2) { 82 buttonState2 = reading2; 83 84 // only toggle the LED if the new button state is HIGH 85 if (buttonState2 == HIGH) { 86 ledState2 = !ledState2; 87 } 88 } 89 } 90 91 // set the LED: 92 digitalWrite(ledPin2, ledState2); 93 94 // save the reading. Next time through the loop, 95 // it'll be the lastButtonState: 96 lastButtonState2 = reading2; 97}
Downloadable files
Schematic
Schematic
Comments
Only logged in users can leave comments
lightthedreams
15 Followers
•9 Projects
2
0