#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include<avr/interrupt.h>
//the variables
/*for the first traffic light*/
int red_pin1 = PINC0;
int yellow_pin1 = PINC1;
int green_pin1 = PINC2;
/*for the second traffic light*/
int red_pin2 = PINC3;
int yellow_pin2 = PINC4;
int green_pin2 = PINC5;
int button = PIND2; //for the interrupt
int motion_sensor1 = PINB0;
int motion_sensor2 = PINB1;
int distance_sensor1 = PIND3;
int echo1 = PIND4;
int distance_sensor2 = PIND5;
int echo2 = PIND6;
//the timer dealy method using timer 1
void T1Delay();
void T1Delay()
{
TCNT1H = 0x85;
TCNT1L = 0xEE;
TCCR1A = 0x00;
TCCR1B = 0x04;
while((TIFR1 & (0x1 << TOV1)) == 0);
TCCR1B = 0;
TIFR1 = 0x1 << TOV1;
}
//the interrupt method
ISR(INT1_vect)
{
int x = digitalRead(button);
if(x == HIGH)
{
Serial.println("the button is pushed");
if(PORTC & 0x0C)
{
//Serial.println("the button is pushed");
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
PORTC |= 0x21;
PORTC &= 0x21;
T1Delay();
//x = LOW;
}
else if(PORTC & 0x21)
{
//Serial.println("the button is pushed");
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
PORTC |= 0x0C;
PORTC &= 0x0C;
T1Delay();
//x = LOW;
}
}
}//end of the interrupt method
long readDistanceCM(int TRIG_PIN, int ECHO_PIN) {//a method from another example to calculate the distance
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
//the start of the main part of this project
void setup() {
DDRC = 0xFF; //as an output - LEDs
DDRD = 0x00; //as as input - distance sensors & button
DDRB = 0x00; //as an input - motion sensors
Serial.begin(9600); //the serial communication
//assuming that the first traffic light has the priority
PORTC |= 0x0C;
PORTC &= 0x0C;
Serial.println("the beginning");
T1Delay(); //we dealy the time
//configuring the intrrupt
//EIMSK |= (1 << INT1); //enabling the serial interrupt
//EICRA |= (1 << ISC11); // the event is rising edge
//sei();//enabling the global interrupt
}//the end of the setup
void loop() {
int sensor = digitalRead(motion_sensor1);
int sensor2 = digitalRead(motion_sensor2);
distance_sensor1 = readDistanceCM(3,4);
distance_sensor2 = readDistanceCM(5,6);
if(sensor == HIGH)
{
Serial.println("sensor 1 is activated");
Serial.print("distance: ");
Serial.println(distance_sensor1);
if(distance_sensor1 < 250)//continue to go
{
T1Delay();
if(PORTC & 0x21)//should we turn on the yellow light initially?
{
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
}
PORTC |= 0x0C;
PORTC &= 0x0C;
T1Delay();
}
else if(distance_sensor1 >= 250)//give a way for the other traffic light
{
T1Delay();
if(PORTC & 0x0C)
{
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
}
PORTC |= 0x21;
PORTC &= 0x21;
T1Delay();
}
T1Delay();
}//the end of the first if statement
T1Delay();
if(sensor2 == HIGH)
{
Serial.println("sensor 2 is activated");
Serial.print("distance: ");
Serial.println(distance_sensor2);
if(distance_sensor2 < 250)//continue to go
{
T1Delay();
if(PORTC & 0x0C)//should we turn on the yellow light initially?
{
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
}
PORTC |= 0x21;
PORTC &= 0x21;
T1Delay();
}
else if(distance_sensor2 >= 250)//give a way for the other traffic light
{
T1Delay();
if(PORTC & 0x21)
{
PORTC |= 0x12;
PORTC &= 0x12;
T1Delay();
}
PORTC |= 0x0C;
PORTC &= 0x0C;
T1Delay();
}
}//the end of the second if statement
T1Delay();
}// the end of the "loop" method