Sunday, 6 December 2015

Arduino Challenge 3: Button Control (Make Your Own Challenge)

Challenge Name: Button Control LEDs

Author: Christina He

Credit: New Innovators.ca Lab 3

Difficulty Level: Level 2

Time to Complete: Appro. 30 minute

Challenge Description: Make two LEDs to blink on and off according to the button. When the button is pressed, one LED will turn on while the other will stay off. When the button is released, the LED that was on will turn off, while the other that was off will turn on for one second and then switch back. The program will repeat.

Answer:

Code 

int button = 2;
int led1 = 8;
int led2 = 9;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(button, INPUT);
}

void loop()
{
  if (digitalRead(button) == HIGH)
  {
   digitalWrite(led2, LOW);
   digitalWrite(led1, HIGH);
   delay(1000);
  }
  else
  {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    delay(1000);
  }
}

Visual Answer








No comments:

Post a Comment