//Stop Light v3.0
// v3.0 incorporates
// - a DHT temperature/humidity sensor.
// - use of NewPing library for speed of sound calculation
// v2.0 incorporate a calibration button to set the desired distance
// from the car to the end of the garage. Pressing the button for 3 seconds
// will set the red light distance to the current location of the car and
// set the yellow light to be 1/3 of the distance from the red point to the green point.
//Definition Section
#include <DHT.h>
#include <NewPing.h>
#define DHTPIN 3 // Digital pin connected to the DHT sensor
#define greenledpin 10
#define yellowledpin 11
#define redledpin 12
#define buttonpin 2
#define DHTTYPE DHT22 // DHT 22
#define TRIGPIN 9
#define ECHOPIN 8
#define MAX_DISTANCE 400
DHT dht(DHTPIN, DHTTYPE);
NewPing sonar(TRIGPIN, ECHOPIN, MAX_DISTANCE) ;
int counter = 0;
int green = 400;
int yellow = 150;
int red = 30;
int waittime = 50 ; //wait before looping
int switchState = 0 ;
// End Definition Section
float duration ;
float distance ;
float soundsp;
float soundcm;
void setup() {
// Initialization Section
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(greenledpin, OUTPUT);
pinMode(yellowledpin, OUTPUT);
pinMode(redledpin, OUTPUT);
pinMode(buttonpin, INPUT) ;
pinMode (buttonpin, INPUT) ;
pinMode (DHTPIN, INPUT) ;
Serial.begin(9600);
dht.begin () ;
}
// End Initialization Section
void loop() {
// Wait a few seconds between measurements.
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Distance Measurement Section - Library Calculation
duration = sonar.ping() ;
soundsp = 331.4 + (0.606 * t) + (0.0124 * h) ;
soundcm = soundsp/10000 ;
distance = (duration / 2) * soundcm ;
Serial.println (distance) ;
digitalWrite(redledpin, LOW);
digitalWrite(yellowledpin, LOW);
// End Distance Measurement Section - Library Calculation
// Distance Calibration section
switchState = digitalRead (buttonpin);
if (switchState == HIGH) {
digitalWrite (greenledpin, LOW) ; //all leds off to confirm button press
digitalWrite (yellowledpin, LOW);
digitalWrite (redledpin, LOW) ;
delay (3000) ; // wait 3 seconds and see if button is still pressed
switchState = digitalRead (buttonpin) ;
if (switchState == HIGH) {
red = distance ; //reset red to current distance to car
yellow = red + .33 * (green - red) ; // reset yellow to 1/3 of distance between red and green
for (int i = 1; i <= 5; i++) { //flash all leds to confirm changes have been completed.
digitalWrite (greenledpin, HIGH) ;
digitalWrite (yellowledpin, HIGH);
digitalWrite (redledpin, HIGH) ;
delay (200) ;
digitalWrite (greenledpin, LOW) ;
digitalWrite (yellowledpin, LOW);
digitalWrite (redledpin, LOW) ;
delay (200) ;
}
}
}
// End Distance Calibration Section
if (distance > green) {
digitalWrite (greenledpin, LOW) ;
waittime = 2;
}
else if (distance < red / 2) {
digitalWrite (greenledpin, LOW) ;
digitalWrite (redledpin, HIGH);
delay (125) ;
digitalWrite (redledpin, LOW) ;
waittime = 2 ;
}
else if (distance < red) {
digitalWrite(greenledpin, LOW);
digitalWrite (redledpin, HIGH);
waittime = 2 ;
}
else if (distance < yellow) {
digitalWrite(greenledpin, LOW);
digitalWrite(yellowledpin, HIGH);
waittime = 2;
}
else {
digitalWrite(greenledpin, HIGH);
waittime = 2 ;
}
//Serial.print ("Red = ") ;
//Serial.print (red) ;
//Serial.print ("cm, Yellow = ") ;
//Serial.print (yellow) ;
//Serial.print ("cm, Green = ") ;
//Serial.print (green) ;
//Serial.println ("cm.");
// Serial.print ("Two-way travel time = ");
// Serial.print (duration);
// Serial.println (" ms.");
// Serial.print ("Estimated distance = ");
// Serial.print (distance);
// Serial.println (" cm.");
// Serial.println (" ");
// Serial.println (waittime) ;
// Serial.print ("Red distance = ") ;
// Serial.println (red) ;
delay(waittime);
}