#include <ServoTimeTimer1.h>
#define servoPin1 9
#define servoPin2 10
#define potPin1 0
#define potPin2 1
ServoTimeTimer1 servo1;
ServoTimeTimer1 servo2;
int val = 0;
int dir = 0;
int pot;
int ster = 0;
void setup()
{
servo1.attach(servoPin1);
servo2.attach(servoPin2);
Serial.begin(38400);
}
void setServos(int ldir , int rdir)
{
Serial.print(1500+rdir*50);
Serial.print(" | ");
Serial.println(1500-rdir*50);
// Linkes Servo (left servo)
servo1.write(1500-ldir*50+35);
// Rechtes Servo (right servo)
servo2.write(1500+rdir*55+40);
}
void loop()
{
val = 0;
dir = 0;
for (int i=0; i<11; i++){
val = val + analogRead(potPin1);
dir = dir + analogRead(potPin2);
}
pot = (val-5120)/512;
Serial.print(pot);
Serial.print(" | ");
ster = (dir-5120)/512 ;
Serial.print(ster);
Serial.print(" | ");
setServos(pot+ster, pot-ster);
//delay(5); // just enabling this line makes the servos not working anymore (random(?) initial velocity)
}
This is the actual (working) code of my little light following "robot" (a half-dead cockroach is much more intelligent ;-)).
Uncommenting the last line with "delay()" will make it fail.
Carsten