Hello,
I am working on a design project for a college class. I have a golf ball rolling down a ramp. The ball pushes a lever attached to the potentiometer. After a delay, the servo is supposed to spin. This works, however, the delay does not seem to work properly. The potentiometer moves the same amount each time the ball hits it, and the ball always moves the potentiometer at the same exact time. The servo, however, is often off 2-5 seconds (does not spin). Is there a way to make my code more precise in order to minimize this error?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo to pin nine (9) on the servo object
}
void loop() {
delay(100);
val = analogRead(potpin); // reads the value of the pentiometer (value between 0 and 1023)
delay(100);
val = map(val, 0, 80, 0, 180); // scales it to use its with the servo (value between 0 and 180)
delay(15000);
myservo.write(val); // sets the servo position according to the scaled value
delay(100); // waits for the servo to get there
}
Student_Design_Project.ino (800 Bytes)