arduino-touchless-projects/touchless_basic/touchless_basic.ino
2020-06-02 19:47:34 +02:00

53 lines
1.4 KiB
C++

const int US_sensorPin = 7;
const int relayPin = 10;
const int waveBackWait = 5000; //adjust this to set the min speed of wave gesture
const int range = 8; //Range of detection from the sensor in cm
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Default relay should be OFF!
}
void loop() {
long duration, cm;
pinMode(US_sensorPin, OUTPUT);
digitalWrite(US_sensorPin, LOW);
delayMicroseconds(2);
digitalWrite(US_sensorPin, HIGH);
delayMicroseconds(5);
digitalWrite(US_sensorPin, LOW);
pinMode(US_sensorPin, INPUT);
duration = pulseIn(US_sensorPin, HIGH);
cm = microsecondsToCentimeters(duration);
if (cm < range && cm > 1) {
for (int i = waveBackWait; i > 0; i--) {
long new_duration, new_cm;
pinMode(US_sensorPin, OUTPUT);
digitalWrite(US_sensorPin, LOW);
delayMicroseconds(2);
digitalWrite(US_sensorPin, HIGH);
delayMicroseconds(5);
digitalWrite(US_sensorPin, LOW);
pinMode(US_sensorPin, INPUT);
new_duration = pulseIn(US_sensorPin, HIGH);
new_cm = microsecondsToCentimeters(new_duration);
if (new_cm < range && new_cm > 1) {
digitalWrite(relayPin, LOW); // Turn Ralay ON
delay(500); // Miliseconds which it waits before turn off again
digitalWrite(relayPin,HIGH); // Trun Ralay OFF
}
}
}
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}