NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Simple Traffic Light Souce Code In C

August 08, 2009
by Nerdful_com
Nerdful_com's Avatar

This code is written in C using no libnerdkits include files.

/* 
www.nerdful.com - I get called a nerd now and then,
but I am only a bit nerdful...

This code will do what a set of 2 traffic lights will do green,
orange, and then red for each direction, but other direction does 
different actions
*/

#include <avr/io.h>
#include <avr/delay.h>
void sleep(uint8_t millisec)
{
while(millisec)
{
delay_ms(1000);
millisec--;
}
}
main()
{
DDRC |=1<<PC5; //enable pin 28 red1
DDRC |=1<<PC4; //enable pin 27 yellow1
DDRC |=1<<PC3; //enable pin 26 green1
DDRB |=1<<PB3; //enable pin 17 red2
DDRB |=1<<PB2; //enable pin 16 yellow2
DDRB |=1<<PB1; //enable pin 15 green2
while(1)
{
PORTC &= ~(1<<PC5); //on red1 pin28
PORTB &= ~(1<<PB1); //on green2 pin 15
PORTB |=(1<<PB3); //off red2
PORTC |=(1<<PC4); //off yellow1
PORTB |=(1<<PB2); //off yellow2
PORTC |=(1<<PC3); //off green1
sleep(5);   
PORTB &= ~(1<<PB2); //on yellow2
PORTB |=(1<<PB1); //off green2
sleep(2);
PORTC |=(1<<PC5); //off red1
PORTB |=(1<<PB2); //off yellow2
PORTC &= ~(1<<PC3); //on green1
PORTB &= ~(1<<PB3); //on red2
sleep(5);
PORTC &= ~(1<<PC4); //on yellow1
PORTC |=(1<<PC3); //off green1
sleep(2);
}
}
August 08, 2009
by Nerdful_com
Nerdful_com's Avatar

I know there is another traffic light sample that NerdKit staff provide. I made this one up (wihout source code from someone else) so it uses less of the MCUs processing power. No LCD and no mathematics to use up limited resources. I could infact replace the Atmega168 chip with an Atiny chip (smaller, cheaper and less features) if I was to market say a signal light set for a train collector. The functions are just timed events that get looped infinitely.

Here is an exciting video showing the traffic lights application running with our embedded microcontroller (MCU), few wires, LEDs etc (less than 5 dollars in parts, could be cheaper with smaller MCU).

alt image text

The resistor near bottom center of breadboard (see photograph above) was left in by accident from LCD I had hooked up earlier (not needed in this layout).

My next task is to figure out PMW for flashing lights and slowing/speeding up normal DC motors. I have some servos and stepper motors on order (will be saving old electronics for parts in the future).

August 09, 2009
by Nerdful_com
Nerdful_com's Avatar

Here's code for a 1 way traffic light that is much cleaner and simpler for me to understand:

#include <avr/io.h>
#include <avr/delay.h>

main()
{
DDRC = 0b0000111; // enable pins 23-25
while(1)
{
PORTC = 0b110;  // turn on PC0 pin 23 // 110, 0 means on and 1 mean off
delay_ms(1000);
PORTC = 0b101;  // turn on PC1 pin 24
delay_ms(500);
PORTC = 0b011;  // turn on PC2 pin 25
delay_ms(1000);
}
}
August 09, 2009
by Nerdful_com
Nerdful_com's Avatar

Oops, tiny error (but it still works), code redo:

#include <avr/io.h>
#include <avr/delay.h>

main()
{
DDRC = 0b000111; // THIS WAS CHANGED! enable pins 23-25
while(1)
{
PORTC = 0b110;  // turn on PC0 pin 23 // 110, 0 means on and 1 mean off
delay_ms(1000);
PORTC = 0b101;  // turn on PC1 pin 24
delay_ms(1000);
PORTC = 0b011;  // turn on PC2 pin 25
delay_ms(1000);
}
}

Post a Reply

Please log in to post a reply.

Did you know that you can connect a computer keyboard to your microcontroller? Learn more...