Tuesday 24 November 2015

Challenge 3 - Button Control

Challenge NameChristmas Time
AuthorKyle Chan
CreditI referenced the code off newinnovators.ca
Difficulty Level (1-5)3
Time To Complete5 minutes
Challenge DescriptionPressing the button turns on off the green and turns on the red


Hintcopy and paste really helps
Answerconst int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin1 = 10;
const int ledPin2 = 9;
const int ledPin4 = 11;
const int ledPin3 = 8;

int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState1 == HIGH)
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin4, HIGH);
    delay(1000);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin4, LOW);
   
  }
  else if (buttonState2 == HIGH)
  {
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    delay(1000);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
}

No comments:

Post a Comment