Lesson 4: Traffic light control

Like in Lesson 2 you'll need to make Traffic light project. But there will be one small difference: we need to add 3 buttons, to control lights (red, yellow, green).

Hardware parts for this lesson:
  • Arduino board
  • Breadboard
  • LED, 3 pieces
  • 220 Ohm Resistors, 3 pieces
  • Jumpers
  • Buttons, 3 pieces
  • 10K Ohm Resistors, 3 pieces


Give time to students to write a code. After construction of the model, you give 10 minutes to finish the code. And in the end show your version of code:


int b1 = 2;
int b2 = 3;
int b3 = 4;
int led1 = 11;
int led2 = 12;
int led3 = 13;

void setup() {
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
    pinMode(b1, INPUT);  
pinMode(b2, INPUT);
  pinMode(b3, INPUT);
}

void loop(){
    if (digitalRead(b1) == HIGH) {
            digitalWrite(led1, HIGH);
    }
    else if(digitalRead(b2) == HIGH){
            digitalWrite(led2, HIGH);
    }
  else if(digitalRead(b3) == HIGH)
    digitalWrite(led3, HIGH);
      else{
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
      }
}

Thanks for this lesson. If you have question please feel free to ask.

Comments

  1. Thank you for posting this excellent information..it is very useful to me..!
    Click here:
    Light Control Sensor in Mumbai

    ReplyDelete

Post a Comment

Popular posts from this blog

Lesson 10: Piezo Buzzer controlled with Potentiometer

Lesson 9: Piezo Buzzer

Lesson 3: Working with Buttons