FaceRecognition-python/SensorsArd/SensorsArd.ino

97 lines
1.8 KiB
Arduino
Raw Normal View History

2018-11-19 14:12:30 +01:00
const int trigPin = 9;
const int echoPin = 10;
const int ProxSensor = 2;
const int relayPin = 12;
2018-11-24 15:39:49 +01:00
const int buzzerPin = 3;
2018-11-19 14:12:30 +01:00
int inputVal = 0;
long duration;
int distance;
char piInput = 'N';
void setup() {
2018-11-24 15:39:49 +01:00
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
2018-11-19 14:12:30 +01:00
pinMode(LED_BUILTIN, OUTPUT);
pinMode(relayPin, OUTPUT);
2018-11-24 15:39:49 +01:00
pinMode(buzzerPin, OUTPUT);
pinMode(ProxSensor,INPUT);
digitalWrite(buzzerPin, LOW);
2018-11-19 14:12:30 +01:00
digitalWrite(relayPin, LOW);
2018-11-24 15:39:49 +01:00
Serial.begin(9600);
2018-11-19 14:12:30 +01:00
}
void loop() {
2018-11-24 15:39:49 +01:00
2018-11-19 14:12:30 +01:00
boolean IROK = false;
boolean UltraOK = false;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
if (distance < 70) UltraOK = true;
else UltraOK = false;
if(digitalRead(ProxSensor) == LOW ) IROK = true;
else IROK = false;
//if (IROK && UltraOK) Serial.println("A");
if (IROK && UltraOK && Serial.available() > 0 ){
if (Serial.read() == 'O') {
digitalWrite(relayPin, HIGH);
2018-11-24 15:39:49 +01:00
playBuzzer(2,false);
2018-11-19 14:12:30 +01:00
delay(5000);
digitalWrite(relayPin, LOW);
IROK = false;
UltraOK = false;
serialFlush();
}
}
IROK = false;
UltraOK = false;
//Read from Pi here
}
void serialFlush(){
while(Serial.available() > 0) {
char t = Serial.read();
}
2018-11-24 15:39:49 +01:00
}
void playBuzzer(int time, boolean ok){
if (ok) {
tone(buzzerPin, 1000);
delay(time/3);
noTone(buzzerPin);
delay(time/5);
tone(buzzerPin,1000);
delay(time/3);
noTone(buzzerPin);
}
else {
tone(buzzerPin, 1000);
delay(time);
noTone(buzzerPin);
}
}
2018-11-19 14:12:30 +01:00