Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Jung Min Yean
- * 03/27/19
- * Program about turning on and off one LED with a button
- *
- */
- // Declare Constants and variables
- const byte Button=9;
- const byte LedPin=17;
- const byte LedPin2 = 19;
- int count=0;
- boolean currentVal;
- boolean lastVal; //currentVal and lastVal treats the button as a switch. It evaluates the value of the current and previous press. If different, then in is a new press.
- void setup() {
- pinMode(LedPin, OUTPUT); //pinMode assign mode of the pin (input or output)
- pinMode(LedPin2, OUTPUT);
- pinMode(Button, INPUT);
- Serial.begin(9600); //activate the monitor
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(Button);
- if (last != current)
- {
- delay(5);
- current = digitalRead(Button);
- }
- return current;
- }
- void loop()
- {
- currentVal = debounce(lastVal); // will go and read the Button
- if (lastVal == LOW && currentVal == HIGH) // Checks if the button has been pressed
- {
- count++;
- }
- if (count == 1)
- {
- digitalWrite(LedPin, HIGH);
- digitalWrite(LedPin2, LOW);
- }
- else if(count == 2)
- {
- digitalWrite(LedPin2, HIGH);
- digitalWrite(LedPin, LOW);
- }
- else if(count == 3)
- {
- digitalWrite(LedPin, HIGH);
- digitalWrite(LedPin2, HIGH);
- }
- else if (count ==4)
- {
- digitalWrite(LedPin, LOW);
- digitalWrite(LedPin2, LOW);
- count = 0;
- }
- lastVal=currentVal;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement