Advertisement
jmyean

Using 1 Button to Switch Between 2 LEDs

Mar 26th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Jung Min Yean
  3.  * 03/25/19
  4.  * Program about turning on and off one LED with a button
  5.  *
  6.   */
  7.  
  8.  
  9.  // Declare Constants and variables
  10. const byte Button=9;
  11. const byte LedPin=17;
  12. byte ReadVal; // variable to read button
  13. const byte LedPin2 = 19;
  14. int count=0;
  15.  
  16. void setup() {
  17.   pinMode(LedPin, OUTPUT);
  18.   pinMode(LedPin2, OUTPUT);
  19.   pinMode(Button, INPUT);
  20.   Serial.begin(9600);
  21. }
  22. void loop() {
  23.   ReadVal = digitalRead(Button); //when on the ReadVal should be 1 and off should be 0, otherwise it is not being read
  24.   delay(10);
  25.   //Serial.println(ReadVal);
  26.   if(ReadVal ==1) // == is asking if the variable has a value of 1
  27.   {
  28.     count ++;
  29.   }
  30.   if (count == 1)
  31.   {
  32.     digitalWrite(LedPin, HIGH);
  33.     digitalWrite(LedPin2, LOW);
  34.   }
  35.   else if(count == 2)
  36.   {
  37.     digitalWrite(LedPin2, HIGH);
  38.     digitalWrite(LedPin, LOW);
  39.     count = 0;
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement