Pedestrian Crossing Traffic Light System Tasks 4 To 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

4) Finite State Machine

Cars GREEN / ped RED

Waiting for input (button)

Cars YELLOW / ped RED

Waiting for yellow delay to pass

Cars RED / ped GREEN

Waiting for crossing time delay to pass

Cars RED / ped RED

5) Timers

Arduino’s microcontroller is an ATmega328 which has 3 timers, timer0 (8 bit), timer1 (16 bit)
and timer2 (8 bit), each of the timers has a counter that is incremented on each tick of the timer's
clock. The Arduino clock runs at 16MHz, this is the fastest speed that the timers can increment
their counters. For example Timer0 and timer2 are 8 bit timers, meaning they can store a
maximum counter value of 255.wich mean it would take 256/16,000,000 seconds (~16us) for the
counter to reach its maximum (overflow) and go back to zero. It is impossible for humans to
notice a blinking LED for example at that frequency so the solution is to use a pre-scale which
divides the pulses coming from the internal clock by a determined amount. This input is sent to
the control circuit which increments the TCNT register.

Let’s say we gonna use timer1, we can pre-scale the timer by settings the Clock Select bits
(CS12/CS11/CS10) in Timer/Counter1 Control Register B (TCCR1B) (from
ATmega48PA/88PA/168PA/328P datasheet)
Timer Prescaler
CPU CLOCK TCNT1
Divide by 1024=
16 MHZ The Counter
15625

TCNT1 is a 16-bit counter it has 65535 for maximum timer counter value and after we configured the
register TCCR1B, the counter will be incrementing with a frequency of 15625 HZ counting to 65535 at
this speed would take = 4.194 seconds, then 1 second will take =15622.16~15622 increments.

So if we wanna a delay of 1 second the timer should count to 15622.

// A SIMPLE CODE TO DEMONSTRATE HOW TO MAKE A DELAY WITH TIMER

//**** TURN A LED ON AFTER A DELAY OF 1 SECOND ****

/* Oussama Lakhneche */

#define yellowLed 2

void setup() {

pinMode(yellowLed, OUTPUT);

//set timer1 at 1Hz

TCCR1B = 0; // set entire TCCR1B register to 0

// Set CS10 and CS12 bits for 1024 prescaler

TCCR1B |= (1 << CS12) | (1 << CS10);

void loop() {

if (TCNT1 >= 15622) {

digitalWrite(yellowLed, HIGH);

TCNT1 = 0;

}
6) Interrupt

The program running on a controller is normally running sequentially instruction by instruction. An


interrupt is an external event that interrupts the running program and runs a special interrupt service
routine (ISR). After the ISR has been finished, the running program is continued with the next instruction.

For our case the main task is to wait for button to be pressed and we can make an interrupt that can be
launched once the state of the input has changed for example we choose pin 2 as an input (or an output is
doesn’t matter cause in the datasheet even if the pin is set as output it will trigger an interrupt) next step is
to setup EIMSK (external interrupt mask register). Then write the interrupt service routine .

You might also like