const int trigPin = 9; const int echoPin = 10; const int ProxSensor = 2; const int relayPin = 12; const int buzzerPin = 3; int inputVal = 0; long duration; int distance; boolean IROK = false; boolean UltraOK = false; char pi = 'N'; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LED_BUILTIN, OUTPUT); pinMode(relayPin, OUTPUT); pinMode(buzzerPin, OUTPUT); pinMode(ProxSensor, INPUT); digitalWrite(buzzerPin, LOW); digitalWrite(relayPin, LOW); Serial.begin(9600); } void loop() { IROK = false; UltraOK = false; pi = 'N'; distance = readDistance(); if (distance < 70) UltraOK = true; else UltraOK = false; if (digitalRead(ProxSensor) == LOW ) IROK = true; else IROK = false; if (Serial.available() > 0) { pi = Serial.read(); } delay(500); Serial.println(pi); Serial.println(IROK); Serial.println(UltraOK); if (IROK && UltraOK && pi == 'o') { openShifter(); } IROK = false; UltraOK = false; } 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) { char t = Serial.read(); } } void playBuzzer(int time, boolean ok) { tone(buzzerPin, 3000); delay(time / 2); noTone(buzzerPin); delay(100); tone(buzzerPin, 3000); delay(time / 2); noTone(buzzerPin); } 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; }