Advertisement
learnelectronics

Arduino toggle

Jul 1st, 2020
3,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. //*********************************************
  2. //*           Arduino Toggle Switch           *
  3. //*        learnelectronics 28 June 2020      *
  4. //*                                           *
  5. //*       www.youtube.com/learnelectronics    *
  6. //*         email: arduino0169@gmail.com      *
  7. //*                                           *
  8. //*********************************************
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.     int switchPin = 2;                        // Button Pin2 one side 5V, opposite to GND through 10K resistor
  18.     int led1Pin = 8;                          // 1st LED on pin8
  19.     int led2pin = 9;                          // 2nd LED on pin9
  20.  
  21.     int val;                                  // Read instantanious pin value
  22.     int val2;                                 // Read delayed value
  23.     int buttonState;                          // Hold the button Value
  24.     int Mode = 0;                             // keep track of which LED is on
  25.  
  26.     void setup() {
  27.       pinMode(switchPin, INPUT);              // Set the switch pin as input
  28.       pinMode(led1Pin, OUTPUT);               // Set LED1 as output
  29.       pinMode(led2pin, OUTPUT);               // Set LED2 as output
  30.       buttonState = digitalRead(switchPin);   // read the initial state
  31.     }
  32.  
  33.     void loop(){
  34.       val = digitalRead(switchPin);           // read button value and store it in val
  35.       delay(10);                              // wait 10mS and see if button is still on
  36.       val2 = digitalRead(switchPin);          // read the input again to check for bounces
  37.       if (val == val2) {                      // make sure we got 2 consistant readings!
  38.         if (val != buttonState) {             // the button state has changed!
  39.           if (val == LOW) {                   // check if the button is pressed
  40.             if (Mode == 0) {        
  41.               Mode = 1;              
  42.             } else {
  43.                 if (Mode == 1) {      
  44.                 Mode = 0;          
  45.             }
  46.           }
  47.          }
  48.         }
  49.         buttonState = val;                    // save the new state in our variable
  50.       }
  51.  
  52.                                               // Switch LED according to mode
  53.       if (Mode == 0) {
  54.         digitalWrite(led1Pin, HIGH);          //LED1 On
  55.         digitalWrite(led2pin, LOW);           //LED2 Off
  56.       }
  57.  
  58.       if (Mode == 1) {
  59.         digitalWrite(led1Pin, LOW);           //LED1 Off
  60.         digitalWrite(led2pin, HIGH);          //LED2 On
  61.       }
  62.  
  63.      
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement