The project video: https://www.youtube.com/watch?v=yBHyAFmtH-E&feature=youtu.be
const int greenLightPin = 6;
const int yellowLightPin = 5;
const int redLightPin = 4;
const int ussTrigPin = 2;
const int ussEchoPin = 3;
void setup() {
// put your setup code here, to run once:
pinMode(greenLightPin,OUTPUT);
pinMode(yellowLightPin,OUTPUT);
pinMode(redLightPin,OUTPUT);
pinMode(ussTrigPin,OUTPUT);
pinMode(ussEchoPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, inches;
digitalWrite(ussTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ussTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ussTrigPin, LOW);
pinMode(ussEchoPin, INPUT);
duration = pulseIn(ussEchoPin, HIGH);
inches = microsecondsToInches(duration);
if(inches > 10 && inches < 15){
digitalWrite(greenLightPin, HIGH);
delay(100);
digitalWrite(greenLightPin, LOW);
delay(100);
}
if(inches < 10 && inches > 5){
digitalWrite(yellowLightPin, HIGH);
delay(100);
digitalWrite(yellowLightPin, LOW);
delay(100);
}
if(inches < 5){
digitalWrite(redLightPin, HIGH);
delay(100);
digitalWrite(redLightPin, LOW);
delay(100);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
const int greenLightPin = 6;
const int yellowLightPin = 5;
const int redLightPin = 4;
const int ussTrigPin = 2;
const int ussEchoPin = 3;
void setup() {
// put your setup code here, to run once:
pinMode(greenLightPin,OUTPUT);
pinMode(yellowLightPin,OUTPUT);
pinMode(redLightPin,OUTPUT);
pinMode(ussTrigPin,OUTPUT);
pinMode(ussEchoPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, inches;
digitalWrite(ussTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ussTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ussTrigPin, LOW);
pinMode(ussEchoPin, INPUT);
duration = pulseIn(ussEchoPin, HIGH);
inches = microsecondsToInches(duration);
if(inches > 10 && inches < 15){
digitalWrite(greenLightPin, HIGH);
delay(100);
digitalWrite(greenLightPin, LOW);
delay(100);
}
if(inches < 10 && inches > 5){
digitalWrite(yellowLightPin, HIGH);
delay(100);
digitalWrite(yellowLightPin, LOW);
delay(100);
}
if(inches < 5){
digitalWrite(redLightPin, HIGH);
delay(100);
digitalWrite(redLightPin, LOW);
delay(100);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
No comments:
Post a Comment