User:Kmcgiver0970/colorLEDupdate

From Wikibooks, open books for an open world
Jump to navigation Jump to search

// Constant vatiables const int buttonPin = 7; const int ledPin = 13; const int whitePin = 12; const int redPin = 11; const int greenPin = 10;

// To keep track of the button state // And to keep track of the number of button pushes int buttonState = 0; int buttonPushCounter = 0;

void setup() {

 // Initialize button to INPUT
 pinMode(buttonPin, INPUT);
 
 // Initialize LEDs to OUTPUT
 pinMode(ledPin, OUTPUT);
 pinMode(whitePin, OUTPUT);
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 
 Serial.begin(9600);

}


void loop() {

 // Added to determine when the buttonPin is being read
 buttonState = LOW;
 digitalWrite(ledPin, HIGH);
 delay(3000);
 digitalWrite(ledPin, LOW);
 delay(3000);
 
 // Read the buttonPin
 buttonState = digitalRead(buttonPin);
 
 // Added to determine when the buttonPin is being read
 digitalWrite(ledPin, HIGH);
 delay(3000);
 digitalWrite(ledPin, LOW);
 delay(5000);
   // If button was pushed, increment buttonPushCounter
   if (buttonState == HIGH)
   {
     buttonPushCounter++;
   }
 
 // Return buttonState to LOW again for next reading
 buttonState = LOW;
 
   // DigitalWrite to specific LEDs according to buttonPushCounter
   if (buttonPushCounter == 1) 
   {
     digitalWrite(whitePin, HIGH);
   }
   else if (buttonPushCounter == 2)
   {
     digitalWrite(redPin, HIGH);
   } 
   else if (buttonPushCounter == 3)
   {
     digitalWrite(greenPin, HIGH);
   } 
 }