/*
2.3.8 Distance Warning Lights
Write a program that will solve the following task:
No LEDs should be work if an object is detected less than 100cm from
the ultrasonic range .
If an object is detected greater than 100 cm, light only a green LED.
If an object is detected greater than 200 cm, light only a yellow LED.
If an object is detected greater than 300 cm, light only a red LED.
*/
int yellow=8; int green=9; int red=10;
int trigger= 2;
int echo=3;
float duration=0;
float distance=0;
void setup()
{
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
//generate the ultrasound signal
digitalWrite(trigger, LOW);
delayMicroseconds(10);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
duration=pulseIn(echo,HIGH);
distance=(duration/2)*0.0345;
if(distance>=100)
{
digitalWrite(yellow, HIGH);
}
else
{
digitalWrite(yellow, LOW);
}
if(distance>=200)
{
digitalWrite(green, HIGH);
}
else
{
digitalWrite(green, LOW);
}
if(distance>=300)
{
digitalWrite(red, HIGH);
}
else
{
digitalWrite(red, LOW);
}
}