0% found this document useful (0 votes)
47 views5 pages

MP 2,3,4

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Introduction: In this experiment, we were familiarized about microcontroller by implementing a basic traffic control

system. After that, a timer was used to control the same traffic system and finally, we learned about the method of
taking external inputs in Arduino by implementing a runway approach light. For this experiment Arduino and some
led was used.

Theory and Methodology :

Arduino: Arduino is an open-source platform used for creating interactive electronics projects. Arduino consists of
both a programmable microcontroller and a piece of software, or IDE (Integrated Development Environment) that
runs on your computer, used to write and upload computer code to the microcontroller board. Arduino Uno also
doesn’t need a hardware circuit (programmer/ burner) to load a new code into the board. We can easily load a code
into the board just using a USB cable and the Arduino IDE (that uses an easier version of C++ to write a code).

Timer: Every electronic component of a sequential logic circuit works on a time base. This time base helps to keep all
the work synchronized. Without a time base, devices would have no idea as to when to perform particular actions.
Thus, timers is an important concept in the field of electronics.

A timer / counter is a piece of hardware built in the Arduino controller. It is like a clock, and can be used to measure
time events. Basically, a timer is a register whose value increases/decreases automatically.

Flowchart:

Figure 1 Traffic control system using microcontrollers

Code:

TRAFFIC CONTROL SYSTEM USING MICROCONTROLLERS


#define RED_PIN 8
#define YELLOW_PIN 10
#define GREEN_PIN 12
int red_on = 3000;
int red_yellow_on = 1000; int green_on = 3000;
int green_blink = 500;
int yellow_on = 1000;
void setup() {
// ports for connecting LEDs
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
}
void loop() {
// turning on voltage at output red LED
digitalWrite(RED_PIN, HIGH);
// to make red LED on
delay(red_on);
// to turn yellow LED on
digitalWrite(YELLOW_PIN, HIGH);
delay(red_yellow_on);
// turning off RED_PIN and YELLOW_PIN, and turrning on greenLEd dig italWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
delay(green_on);
digitalWrite(GREEN_PIN, LOW);
// for turning green Led on and off for 3 times
for (int i = 0; i < 3; i = i+1)
{
delay(green_blink);
digitalWrite(GREEN_PIN, HIGH);
delay(green_blink);
digitalWrite(GREEN_PIN, LOW);
}
// for turning on yellow LED
digitalWrite(YELLOW_PIN, HIGH);
delay(yellow_on);
digitalWrite(YELLOW_PIN, LOW);
}
TRAFFIC CONTROL SYSTEM WITH TIMER TIMER

#define RED_PIN 8 //define name of pins used


#define YELLOW_PIN 10
#define GREEN_PIN 12
//define the delays for each traffic light color
int red_on = 3000; //3s delay
int red_yellow_on = 1000; //1s delay
int green_on = 3000; //3s delay
int green_blink = 1500; //.5s delay
int milliseconds = millis();
int yellow_on = 1000; //1s delay
int delay_timer (int miliseconds)
{
int count = 0; while(1)
{
if(TCNT0 >= milliseconds ) // Checking if 1 milisecond has passed
{
TCNT0=0;
count++;
if (count == miliseconds) //checking if required miliseconds delay has passed
{
count=0;
break; // exits the loop
}
}
}
return 0;
}
void setup() { //define pins connected to LEDs as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
//set up timer
TCCR0A = 0b00000000 ;
TCCR0B = 0b11111000; //setting prescaler for timer clock
TCNT0= 0;
}
void loop() {
//to make red LED on
digitalWrite(RED_PIN, HIGH);
delay_timer(red_on);
//to turn yellow LED on
digitalWrite(YELLOW_PIN, HIGH);
delay_timer(red_yellow_on);
//turning off RED_PIN and YELLOW_PIN, and turrning on greenLED
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
delay_timer(green_on);
digitalWrite(GREEN_PIN, LOW);
//for turning green Led on and off for 3 times
for(int i = 0; i < 3; i = i+1)
{
delay_timer(green_blink);
digitalWrite(GREEN_PIN, HIGH);
delay_timer(green_blink);
digitalWrite(GREEN_PIN, LOW);
}
//for turning on yellow LED
digitalWrite(YELLOW_PIN, HIGH);
delay_timer(yellow_on);
digitalWrite(YELLOW_PIN, LOW);
}

TRAFFIC CONTROL SYSTEM EXTERNAL INPUT

#define RED1 4
#define RED2 6
#define RED3 7
#define RED4 8
#define RED5 9
#define RED6 12
#define LEDSWITCH 14

//define the delays for each LED


int LED_blink = 700;
//define variable for switch press
int switch_read; //defining a variable which will read the state of the switch
int LED_sequence=1; //defining which way the LEDs will light up (left to right or right to left)
int delay_timer (int miliseconds)
{
int count = 0; while(1)
{
if(TCNT0 >= __) // Checking if 1 milisecond has passed
{
TCNT0=0;
count++;
if (count == miliseconds) //checking if required miliseconds delay has passed {
count=0;
break; // exits the loop
}
}
}
return 0;
}
void setup() {
//define pins connected to LEDs as outputs and the switch as input
pinMode (RED1, OUTPUT);
pinMode (RED2, OUTPUT);
pinMode (RED3, OUTPUT);
pinMode (RED4, OUTPUT);
pinMode (RED5, OUTPUT);
pinMode (RED6, OUTPUT);
pinMode (LEDSWITCH, INPUT);
//set up timer
TCCR0A = 0b00000000;
TCCR0B = 0b11111000; //setting prescaler for timer clock
TCNT0=0;
}
void loop() {
switch_read=digitalRead(switch1); if (switch_read==LOW){
LED_sequence=!LED_sequence; }
if (LED_sequence==1){

//to make green1 LED blink


digitalWrite(RED1, HIGH);
delay_timer(LED_blink);
digitalWrite(RED1, LOW);
//to turn red1 LED blink
digitalWrite(RED2, HIGH);
delay_timer(LED_blink);
digitalWrite (RED2, LOW);
//green2 blink and so on
digitalWrite(RED3, HIGH);
delay_timer(LED_blink);
digitalWrite(RED3, LOW);

digitalWrite(RED4, HIGH);
delay_timer(LED_blink);
digitalWrite(RED4, LOW);

digitalWrite(RED5, HIGH);
delay_timer(LED_blink);
digitalWrite(RED5, LOW);

//green2 blink and so on


digitalWrite (RED6, HIGH);
delay_timer(LED_blink);
digitalWrite(RED6, LOW);
delay_timer(LED_blink);
}
else {
digitalWrite(RED1, HIGH);
delay_timer (LED_blink);
digitalWrite(RED1, LOW);
digitalWrite(RED2, HIGH);
delay_timer(LED_blink);
digitalWrite(RED2, LOW);

digitalWrite(RED3, HIGH);
delay_timer(LED_blink);
digitalWrite(RED3, LOW);
digitalWrite(RED4, HIGH);
delay_timer(LED_blink);
digitalWrite(RED4, LOW);

digitalWrite(RED5, HIGH);
delay_timer(LED_blink);
digitalWrite(RED5, LOW);

digitalWrite(RED6, HIGH);
delay_timer(LED_blink);
digitalWrite (RED6, LOW);
delay_timer(LED_blink);
}
}
Conclusion: After completing the experiment we learn about microprocessor, how to define pins in arudino and also
about timer function and delay function. We implemented three different circuit to test our skills and the result was
successful.

References:
1) https://www.arduino.cc/.
2) https://www.coursera.org/learn/arduino/lecture/ei4ni/1-10-first-glance-at-a-program
3) Jeremy Blue; Exploring Arduino: Tools and Techniques for Engineering Wizardry
4) AIUB Lab Manual Student Copy

You might also like