Advertisement
octo_cat

button press corresponding lights

Jun 9th, 2020
2,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin = 2;    
  2. const int ledPin0 =  13;      //green light
  3. const int ledPin1 =  12;      //yellow light
  4. const int ledPin2 =  11;      //red light
  5.  
  6. int count = 0;              
  7. // variables will change:
  8. int buttonState = 0;        
  9.  
  10. void setup() {
  11.   pinMode(ledPin0, OUTPUT);
  12.    pinMode(ledPin1, OUTPUT);
  13.     pinMode(ledPin2, OUTPUT);
  14.   pinMode(buttonPin, INPUT);    
  15. }
  16.  
  17. void loop(){
  18.  buttonState = digitalRead(buttonPin);
  19.    if (buttonState == HIGH) {    
  20.     count++;              
  21.     if (count >= 4) {
  22.       count = 0;
  23.     }
  24.   }
  25.     else {
  26.     digitalWrite(ledPin0, LOW);
  27.     digitalWrite(ledPin1, LOW);
  28.     digitalWrite(ledPin2, LOW);
  29.     }
  30.      
  31.      
  32.       if (count == 1) {
  33.      digitalWrite(ledPin0, HIGH);
  34.     }    
  35.     else {
  36.     digitalWrite(ledPin0, LOW);
  37.     }
  38.      
  39.      
  40.       if (count == 2) {
  41.      digitalWrite(ledPin1, HIGH);
  42.     }    
  43.     else {
  44.     digitalWrite(ledPin1, LOW);
  45.     }
  46.      
  47.      
  48.       if (count == 3) {
  49.      digitalWrite(ledPin2, HIGH);
  50.     }
  51.     else {
  52.     digitalWrite(ledPin2, LOW);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement