Week 9 – Traffic Signal

For the Week 9 assignment, we were supposed to get information from at least one analog sensor and at least one switch, and use this information to control at least two LEDs, one in an analog fashion and one in a digital fashion, in a creative way.

I’ll be very honest, I started the assignment on Saturday but I wasn’t very comfortable with working with the circuit. I knew I could experiment with the coding part, but the fear of damaging any equipment meant that trial-and-error wasn’t the best choice when it came to setting up the circuit. But after going through some websites and class examples and after the Monday class (29th March), I became a lot more confident about setting up my circuit.

I tried to create a traffic signal using Arduino. I set up my circuit with 3 LEDs, corresponding to the red, yellow and green lights, one switch (digital sensor) and one potentiometer (analog sensor).

The red LED can be turned on and off using the switch. It is controlled digitally and its brightness can only be set as HIGH or LOW. When it is on, the potentiometer can then be used to take input and the input generated can be used to make the traffic signal work. The input generated can be mapped onto a variable called mappedValue, which only stores values between 0 to 255 (just like in the class examples).

When the value of ‘mappedValue’ crosses 100, the red light is turned off and the yellow LED is turned on. This yellow LED is controlled in an analog fashion and its brightness is equal to the mappedValue at all instances.

When the value of mappedValue crosses 150, the yellow LED is turned off and the green LED is turned on. The green LED is controlled digitally and its brightness can once again only be set as HIGH or LOW. The green LED stays on as long as mappedValue takes values between 150 and 255. 

The entire process can then be repeated in the reverse order, starting from the green LED and finishing at the red LED, which can once again be turned on and off using the switch/button.

A video of the working circuit is attached below.

The code:

int buttonPin = 2;
int ledPin = 3;
int led2Pin = 9;
int led3Pin = 11;
int knobPin = A0;
int ledState = LOW;
int prevButtonState = LOW;
 
void setup() {
  // set pin modes for the 3 LEDs and the button
  pinMode(ledPin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(buttonPin, INPUT);
  // needed in order to start serial communication
  Serial.begin(9600);
}
 
 void loop() {
  // check to see what state our button is in, and store that information
  int currentButtonState = digitalRead(buttonPin);
 
  // if the button is currently being prssed down, AND during the last frame is wasn't pressed down
  if (currentButtonState == HIGH && prevButtonState == LOW) {
      // flip the LED state
      if (ledState == HIGH){
        ledState = LOW;
      } else if (ledState == LOW){
        ledState = HIGH;
      }
  }
 
  // set our LED to turn on and off according to our variable that we flip above
//  digitalWrite(ledPin, ledState);
 
  //we need to remember the state of our button for the next time through LOOP
  prevButtonState = currentButtonState;

  // reading the input generated by the potentiometer and then mapping it to a variable called 'mappedValue'
  int knobValue = analogRead(knobPin);
  int mappedValue = map(knobValue, 0, 1023, 0, 255);

//  if (mappedValue >= 120 && ledState == LOW){
//    digitalWrite(ledPin, LOW);
//    analogWrite(led2Pin, 0);
//    digitalWrite(led3Pin, HIGH);
//  }
//  else if (mappedValue < 120 && ledState == LOW){
//    digitalWrite(ledPin, LOW);
//    analogWrite(led2Pin, mappedValue);
//    digitalWrite(led3Pin, LOW);
//  }
//  else if (mappedValue >= 120 && ledState == HIGH){
//    digitalWrite(ledPin, HIGH);
//    analogWrite(led2Pin, 0);
//    digitalWrite(led3Pin, LOW);
//  }
//  else if (mappedValue < 120 && ledState == HIGH){
//    digitalWrite(ledPin, HIGH);
//    analogWrite(led2Pin, mappedValue);
//    digitalWrite(led3Pin, LOW);
//  }


  // setting conditions for turning the 3 different LEDs on and off based on the input generated by the potentiometer
  if (mappedValue < 100 && ledState == HIGH){
    digitalWrite(ledPin, HIGH);
    analogWrite(led2Pin, 0);
    digitalWrite(led3Pin, 0);
  }

  if (mappedValue >= 100 && mappedValue <= 150 && ledState == HIGH){
    digitalWrite(ledPin, LOW);
    analogWrite(led2Pin, mappedValue);
    digitalWrite(led3Pin, LOW);
  }

  if (mappedValue > 150 && ledState == HIGH){
    digitalWrite(ledPin, LOW);
    analogWrite(led2Pin, 0);
    digitalWrite(led3Pin, HIGH);
  }

  if (mappedValue >= 0 && mappedValue <= 5 && ledState == LOW){
    digitalWrite(ledPin, LOW);
  }
}

 

Leave a Reply