Smart House Light - Ino
Smart House Light - Ino
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
Maximum sensor distance is rated at 400-500cm. [this is an arbitrary number]
#define RELAY_LINE1_PIN 8
#include "NewPing.h"
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins
and maximum distance.
unsigned int critical_distance_cms = 50; // Cutoff distance at which the light will switch
[this is an arbitrary number]
bool state = 0;
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
pinMode(RELAY_LINE1_PIN, OUTPUT);
digitalWrite(RELAY_LINE1_PIN, HIGH); // Turn the light off
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec).
29ms should be the shortest delay between pings.
unsigned int distance = readDistance(); // Current distance of any object facing the
ultrasonic sensor
Serial.print("Ultrasonic: ");
Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set
distance range)
Serial.println("cm");
delay(5); // Do nothing until the person moves away from the door
}
if (state)
{
Serial.println("Door Open!");
digitalWrite(RELAY_LINE1_PIN, LOW); // Turn the light on
}
else
{
Serial.println("Door Closed!");
digitalWrite(RELAY_LINE1_PIN, HIGH); // Turn the light off
}
}
}
// The value 0 indicates that the ultrasonic sensor is reading nothing in front of it
// Set this distance to max distance so the light doesn't switch unnecessarily
if (distance == 0)
{
distance = MAX_DISTANCE;
}
return distance;
}