User:Kmcgiver0970/colorLED

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 blackPin = 6;

const int whitePin = 5;

const int redPin = 4;

const int greenPin = 3;


// 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(blackPin, 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(blackPin, HIGH);
    }
    if (buttonPushCounter == 2)
    {
      digitalWrite(whitePin, HIGH);
    } 
    if (buttonPushCounter == 3)
    {
      digitalWrite(redPin, HIGH);
    } 
    if (buttonPushCounter == 4) 
    {
      digitalWrite(greenPin, HIGH);
    } 
  }