I have a medium Springrc full rotation servo. The normal servo serial library doesn't seem to working properly. I downloaded a ContinuousServo.h library that sort of acts as a step motor library on my servo, but I am only able to make it spin in one direction. I am trying to connect the servo to a gear drive and rack system and would like to be able to have the servo spin in one direction for a certain amount of time and then stop and reverse. Does anyone have some advise? How can I control the full/continuous rotation servos accurately in both directions over time parameters?
this is the code I have so far.
#define sensorIR A0 //Must be an analog pin
#include <ContinuousServo.h> // servo library
ContinuousServo servo1(11); // servo control object
float sensorValue; //inches; // or cm; Must be of type float for pow()
int presenceDetected;
float UV, Bright;
const int threshold = 10;
const unsigned long interval = 20*1000UL; // Twenty seconds
//const int motorPin = 13;
unsigned long startTime = 0;
void setup() {
Serial.begin(57600);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
// servo1.attach(11);
}
void loop() {
// if the analog value is low enough, turn on the motor:
// if (analogRead(sensorValue) < threshold) {
// startTime = millis();
// Serial.println(startTime); // Serial Print When the sensor is sensed.
// Serial.print("on");
// servo1.write(0);
//
// //digitalWrite(motorPin, HIGH);
// }
//
// // If time has expired, turn the motor off
// if(millis() - startTime > interval) {
// Serial.println("off");
// servo1.write(95);
// }
IR();
if (sensorValue > 200) {
//if (inches < 10) {
LED();
}
//if (inches < 10) {
if (sensorValue > 200) {
presenceDetected = 1;}
else {
presenceDetected = 0;
}
if(presenceDetected == 1)
{ servo1.step(0);
Serial.println("Servo180:");
}
else {
servo1.step(500);
Serial.println("Servo0:");
}
delay(100);
Serial.println(sensorValue);
//Serial.print("Inches: ");
//Serial.print("centimeters;");
//Serial.println(inches);
Serial.print("presenceDetected:");
Serial.println(presenceDetected);
//Serial.println(cm);
//int position;
//servo1.write(180);
//delay(100);
}
void IR()
{ sensorValue = analogRead(sensorIR);
//inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
//cm = 10650.08 * pow(sensorValue,-0.935) - 10;
delay(100);}
void LED()
{ digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);}