# include //#include Servo myservo; // create servo object to control a servo int rightDC1 = 8; int rightDC2 = 9; int leftDC1 = 0; int leftDC2 = 1; int pos = 90; // variable to store the servo position const int rightSensorPin = 15; const int leftSensorPin = 16; float right_sound = 0; // added float left_sound = 0; // added float rightVal = 0; // changed bool to float float leftVal = 0; // changed boot to float float threshold = 0.75; void movetail() { for (pos = 90; pos <= 180; pos += 1) { // goes from 90 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } void moveForward() { digitalWrite(rightDC1, HIGH); digitalWrite(rightDC2, LOW); digitalWrite(leftDC1, HIGH); digitalWrite(leftDC2, LOW); } void moveStop() { digitalWrite(rightDC1, LOW); digitalWrite(rightDC2, LOW); digitalWrite(leftDC1, LOW); digitalWrite(leftDC2, LOW); } void turnLeft() { digitalWrite(rightDC1, HIGH); digitalWrite(rightDC2, LOW); digitalWrite(leftDC1, LOW); digitalWrite(leftDC2, HIGH); } void turnRight() { digitalWrite(rightDC1, LOW); digitalWrite(rightDC2, HIGH); digitalWrite(leftDC1, HIGH); digitalWrite(leftDC2, LOW); } void setup() { myservo.attach(10); //attaches the servo on pin 10 to the servo object pinMode(leftSensorPin, INPUT); //pin 16 an input pin. pinMode(rightSensorPin, INPUT); //pin 15 an input pin. Serial.begin(9600);// initialize the serial port: } void loop() { // analog sound input right_sound = analogRead(rightSensorPin); //changed digital to analog left_sound = analogRead(leftSensorPin); //value is between 0 and 1023 // mic level ajustment in serial plotter rightVal = (right_sound ) / 1024; // added leftVal = (left_sound ) / 1024; // value is between 0.5 and 1 // when the sensor detects a signal above the threshold value set on sensor, turn finder to the direction of sound Serial.println("rightVal=" + String(rightVal) + " ,leftVal=" + String(leftVal)); if (leftVal > threshold && rightVal > threshold) { Serial.println("move forward"); moveForward(); delay(500); } if (leftVal < threshold && rightVal > threshold) { Serial.println("turn right"); turnRight(); delay(500); } if (leftVal > threshold && rightVal < threshold) { Serial.println("turn left"); turnLeft(); delay(500); } moveStop(); }