0% found this document useful (0 votes)
10 views6 pages

Lab 4 - EENG312

The document outlines a laboratory experiment for EENG312 focusing on non-blocking code using the millis() function to control traffic lights and pedestrian signals. Students will edit and debug a provided program for Arduino Uno that manages traffic light sequences and incorporates a pedestrian crossing button. Additionally, a homework assignment is given to enhance the program by adding a sensor to detect incoming cars before allowing pedestrian crossing.

Uploaded by

Zeynep Bayram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Lab 4 - EENG312

The document outlines a laboratory experiment for EENG312 focusing on non-blocking code using the millis() function to control traffic lights and pedestrian signals. Students will edit and debug a provided program for Arduino Uno that manages traffic light sequences and incorporates a pedestrian crossing button. Additionally, a homework assignment is given to enhance the program by adding a sensor to detect incoming cars before allowing pedestrian crossing.

Uploaded by

Zeynep Bayram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EENG312: Microcontroller Applications

Laboratory Study: Experiment 4


Flow Control and Non-Blocking Code

Objective: The students will be introduced to the concept of non-blocking code using the
millis() function for time control. The students will get to practice the usage of flags to check
the progress of the code.

Lab Work: Each group of students will edit and run the following Program in the IDE and
Tinkercad and debug to make it ready to be uploaded to Arduino Uno. The program controls
one set of traffic lights for cars and another for pedestrians. A pedestrian button can be used
to allow pedestrians to cross.
//traffic light code
//millis for time control allowing non-blocking code
//pedestrian button

#define car_red 4
#define car_yellow 3
#define car_green 2

#define ped_green 5
#define ped_red 6

#define ped_button 8

int start, button_pressed = 0;


unsigned long start_time;

//function to control light sequence


void normal_lights(unsigned long s_time)
{
//current_time is time passed since the start of sequence
unsigned long current_time = millis() - s_time;
//Serial.println(current_time);

if (current_time < 500)


{
digitalWrite(car_green, LOW);
digitalWrite(car_yellow, LOW);
digitalWrite(car_red, HIGH);
}
else if (current_time > 500 && current_time < 4500)
{
digitalWrite(ped_red,LOW);
digitalWrite(ped_green,HIGH);

}
else if (current_time > 4500 && current_time < 5000)
{
digitalWrite(ped_red,HIGH);
digitalWrite(ped_green,LOW);

}
else if (current_time > 5000 && current_time < 6000)
{
digitalWrite(car_yellow, HIGH);

}
else if (current_time > 6000 && current_time < 9000)
{
digitalWrite(car_red, LOW);
digitalWrite(car_yellow, LOW);
digitalWrite(car_green,HIGH);

}
else if (current_time > 9000 && current_time < 10000)
{
digitalWrite(car_green,LOW);
digitalWrite(car_yellow, HIGH);
}
//the sequence takes 10 seconds. After 10 seconds it must
//restart, thus we turn the start flag to 1
else if (current_time > 10000)
{
start = 1;
}
}

void setup()
{
//Serial.begin(9600);
pinMode(car_red, OUTPUT);
pinMode(car_yellow, OUTPUT);
pinMode(car_green, OUTPUT);
pinMode(ped_green, OUTPUT);
pinMode(ped_red, OUTPUT);
pinMode(ped_button, INPUT);
//start = 1 -> light sequence will reset
//start = 0 -> light sequence will continue from
// where it was
start = 1;
}

void loop()
{
button_pressed = digitalRead(ped_button);
if (button_pressed == HIGH)
{
//Pedestrians are trying to cross
//when button is pressed, the light sequence is
//reset, turning lights red
start_time = millis();
start = 0;

else
{
//Routine
if (start == 1)
{
//reseting start time to now and start flag to 0
//starts the light sequence from red
start_time = millis();
start = 0;
}

normal_lights(start_time);

delay(10);

}
HW 4. (Due Friday after Midterm Exams)
Write a program with the respective Tinkercad circuit design to control traffic lights like the
example above. In addition to the example setup, you must have a sensor to check for
incoming cars before allowing pedestrian crossing. You could use PIR or Ultrasonic distance
sensors.
An example design would be:
 No alteration to light sequence if cars are detected.
 Reset light sequence if there are no cars (resets to ‘red for cars’ allowing crossing)
HINT: PIR sensor detects motion.
HINT: Ultrasonic distance sensor measures distance in terms of time.

You might also like