FaceRecognition-python/SensorsArd/SensorsArd.ino

99 lines
1.8 KiB
Arduino
Raw Permalink 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;
2018-12-06 16:01:53 +01:00
boolean IROK = false;
boolean UltraOK = false;
char pi = 'N';
2018-11-19 14:12:30 +01:00
void setup() {
2018-11-24 15:39:49 +01:00
pinMode(trigPin, OUTPUT);
2018-12-06 16:01:53 +01:00
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);
2018-12-06 16:01:53 +01:00
pinMode(ProxSensor, INPUT);
2018-11-24 15:39:49 +01:00
digitalWrite(buzzerPin, LOW);
2018-11-19 14:12:30 +01:00
digitalWrite(relayPin, LOW);
2018-12-06 16:01:53 +01:00
Serial.begin(9600);
2018-11-19 14:12:30 +01:00
}
void loop() {
2018-12-06 16:01:53 +01:00
IROK = false;
UltraOK = false;
pi = 'N';
2018-11-24 15:39:49 +01:00
2018-12-06 16:01:53 +01:00
distance = readDistance();
if (distance < 70) UltraOK = true;
2018-11-19 14:12:30 +01:00
else UltraOK = false;
2018-12-06 16:01:53 +01:00
if (digitalRead(ProxSensor) == LOW ) IROK = true;
2018-11-19 14:12:30 +01:00
else IROK = false;
2018-12-06 16:01:53 +01:00
if (Serial.available() > 0) {
pi = Serial.read();
}
2018-11-24 17:58:35 +01:00
delay(500);
2018-12-06 16:01:53 +01:00
2018-11-24 17:58:35 +01:00
Serial.println(pi);
Serial.println(IROK);
Serial.println(UltraOK);
2018-12-06 16:01:53 +01:00
if (IROK && UltraOK && pi == 'o') {
openShifter();
2018-11-19 14:12:30 +01:00
}
IROK = false;
UltraOK = false;
}
2018-12-06 16:01:53 +01:00
void openShifter() {
digitalWrite(relayPin, HIGH);
playBuzzer(300, true);
delay(7000);
digitalWrite(relayPin, LOW);
IROK = false;
UltraOK = false;
pi = 'N';
serialFlush();
}
void serialFlush() {
while (Serial.available() > 0) {
2018-11-19 14:12:30 +01:00
char t = Serial.read();
}
2018-11-24 15:39:49 +01:00
}
2018-12-06 16:01:53 +01:00
void playBuzzer(int time, boolean ok) {
2018-11-24 17:58:35 +01:00
tone(buzzerPin, 3000);
2018-12-06 16:01:53 +01:00
delay(time / 2);
2018-11-24 17:58:35 +01:00
noTone(buzzerPin);
delay(100);
tone(buzzerPin, 3000);
2018-12-06 16:01:53 +01:00
delay(time / 2);
2018-11-24 17:58:35 +01:00
noTone(buzzerPin);
2018-11-19 14:12:30 +01:00
2018-12-06 16:01:53 +01:00
}
2018-11-24 17:58:35 +01:00
2018-12-06 16:01:53 +01:00
int readDistance() {
// 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
return duration * 0.034 / 2;
2018-11-24 17:58:35 +01:00
2018-12-06 16:01:53 +01:00
}