int Led1 = 13; int Led2 = 12; const int trigPin = 11; const int echoPin = 10; long duration = 0; int distance = 0; void setup() { pinMode(Led1, OUTPUT); pinMode(Led2, OUTPUT); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); } void loop() { if(Serial.available()) { char data = Serial.read(); // Serial.println(data); if(data == 'R'){LedOn1();} if(data == 'G'){LedOn2();} if(data == 'S'){LedStop();} } // 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; // Prints the distance on the Serial Monitor // Serial.print(" "); Serial.println(distance); delay(50); } void LedOn2() { digitalWrite(Led2, HIGH); } void LedOn1() { digitalWrite(Led1, HIGH); } void LedStop() { digitalWrite(Led1, LOW); digitalWrite(Led2, LOW); }