Here is my code, to run a traffic simulation in Atmel Studio 6 on an ATmega328p,
ID: 3768119 • Letter: H
Question
Here is my code, to run a traffic simulation in Atmel Studio 6 on an ATmega328p, however I'm having trouble adding more inputs (like traffic or pedestrian switches) and/or states (include walk signals or left turn lights), as it's a bit too simple to qualify as it stand. Please help me. The LED lights will operate on a set timer and delay based off the different pin it is assigned. I want to incorporate the push button on the microcontroller in which, once it is pressed once the light should light up red. If pressed twice, the light should be yellow. Lastly if pressed three times, the light should display green.
#include <avr/io.h>
#include <avr/delay.h>
void sleep(uint8_t_millisec)
{
while(millisec)
{
delay_ms(1000);
millsec--;
}
}
main()
{
DDRC |=1<<PC5;
DDRC |=1<<PC4;
DDRC |=1<<PC3;
while(1)
{
PORTC &= ~(1<<PC5);
PORTC |=(1<<PC4);
PORTC |=(1<<PC3);
sleep(5);
PORTC &= ~(1<<PC4);
PORTC |=(1<<PC5);
PORTC |=(1<<PC3);
sleep(2);
PORTC &= ~(1<<PC3);
PORTC |=(1<<PC5);
PORTC |=(1<<PC4);
sleep(5);
}
}
Explanation / Answer
Program:
#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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.