//We introduce button constants int buttonPin = 7; int buttonState = 0; int cont = 0; //We introduce constants of the leds that will make eyes int cm = 0; int eyesLEDone = 3; int eyesLEDtwo = 5; //insert the 2 pins of the distance sensor int triggerPin = 2; int echoPin = A0; //we introduce the constant of the converter int transformador = 13; //code to enter the distance sensor long readUltrasonicDistance(int triggerPin, int echoPin) { pinMode(triggerPin, OUTPUT); digitalWrite(triggerPin, LOW); delayMicroseconds(2); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); pinMode(echoPin, INPUT); return pulseIn(echoPin, HIGH); } void setup() { cont = 0; //initial button status Serial.begin(9600); pinMode(buttonPin, OUTPUT); int buttonState = digitalRead(buttonPin); pinMode(transformador, OUTPUT); pinMode(transformador, INPUT); } void loop() { //button action Serial.print("count :"); Serial.println(cont); if (digitalRead(buttonPin) == HIGH) { cont++; while (digitalRead(buttonPin) == HIGH) {} } //we define that according to the times that you click the button, it makes different functions if (cont == 0) { digitalWrite(transformador, HIGH); } else if (cont == 1) { //transformer code pinMode(transformador, OUTPUT); digitalWrite(transformador, LOW); delay(100); pinMode(transformador, INPUT); } else if (cont == 2) { digitalWrite(transformador, LOW); delay(100); digitalWrite(transformador, HIGH); delay(100); } else if (cont >= 3) { cont = 0; } //we define that if an object is less than the desired distance, the leds that act as eyes are turned on cm = 0.01723 * readUltrasonicDistance(2, A0); Serial.print ("--------------------"); Serial.println(digitalRead(cm)); if (cm <= 100) { digitalWrite(eyesLEDone, HIGH); digitalWrite(eyesLEDtwo, HIGH); } if (cm > 100) { digitalWrite(eyesLEDone, LOW); digitalWrite(eyesLEDtwo, LOW); } }