I am working on a project for my school. The project is a pet feeder which has servo motor and ultrasonic sensor in it. When the pet walks near the ultrasonic sensor, the servo will rotate and drop food for for 5 seconds and close again. Unfortunately I am getting error in my code as I cant slot in the delay in the code. This is what my code loom like.
#include <Servo.h>
// constants won't change
const int TRIG_PIN = 6;
const int ECHO_PIN = 7;
const int SERVO_PIN = 9;
const int DISTANCE_THRESHOLD = 50;
Servo servo;
float duration_us, distance_cm;
void setup() {
Serial.begin (9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(SERVO_PIN);
servo.write(0);
}
void loop() {
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017 * duration_us;
if(distance_cm < DISTANCE_THRESHOLD)
servo.write(90);
delay(1000);
servo.write(0);
}
And this is the error warning:
void loop() {
^~~~
exit status 1
redefinition of 'void setup()'
Any help would be appreciated.