Hello, I have been working on a project that requires four servos to continuously rotate fully clockwise and fully counterclockwise. I am using an arduino Mega, and Hitec HS-425BB servos. I took away the gears and case they were in so that the potentiometer and motor were exposed. I realized that i could make them continuously rotate fully clockwise and fully counterclockwise if I set the potentiometer at the middle point. I am using 1000 - 2000 Microseconds 1500 being my zero point. If I input 1000-1500 it rotates counter and 1500- 2000 it rotates clockwise. This is what I want; however, I wrote a code so that I input RPM (revolutions per minute) which range from -9600 to 9600 RPM (i got these values by setting the servo to 2000microseconds and measuring the RPM with the tachometer) and these would be mapped to something the servo recognizes 1000-2000.
I decided to test if i was getting the right RPM with a tachometer (device that measures RPM) but the readings were off by a wide margin. In other words if i input 1000 RPM i should expect a reading of something close to 1000 RPM from the tachometer, but its not giving me that reading it gives me 4000+ I thought it was not mapping it right, that is why i added Serial.print(map2) to see the output of the map2 function and its working for both of them. At this point i am not sure what i am doing wrong if anyone can help, it will be greatly appreciated.
Here is my Code
#include <Servo.h>
Servo myservox;
Servo myservoy;
Servo myservoz;
int xs = 9;
int ys = 10;
int zs = 11;
int rpmx = 0;
int rpmy = 1000;
int rpmz = 0;
void setup()
{
Serial.begin(9600);
myservox.attach(xs);
myservoy.attach(ys);
myservoz.attach(zs);
myservox.writeMicroseconds(1500);
myservoy.writeMicroseconds(1500);
myservoz.writeMicroseconds(1500);
}
void loop ()
{
rotate(rpmx, xs);
rotate(rpmy, ys);
rotate(rpmz, zs);
}
void rotate(float rpm, int servo)
{
if(rpm > 0)
{
float in_low2 = 0.0;
float in_high2 = 9500;
float out_low2 = 1500; //127.5;
float out_high2 = 2300; //255.0;
float map2;
map2 = (rpm-in_low2)*(out_high2-out_low2)/(in_high2-in_low2)+out_low2;
Serial.println(map2);
if(servo==9)
{
myservox.writeMicroseconds(map2);
}
if(servo==10)
{
myservoy.writeMicroseconds(map2);
}
if(servo==11)
{
myservoz.writeMicroseconds(map2);
}
delay(15);
}
if(rpm < 0)
{
float in_low1 = 9600.0;
float in_high1 = 0.0;
float out_low1 = 1500; //127.5;
float out_high1 = 700; //255.0;
float map1;
map1 = (rpm-in_low1)*(out_high1-out_low1)/(in_high1-in_low1)+out_low1;
Serial.println(map1);
if(servo==9)
{
myservox.writeMicroseconds(map1);
}
if(servo==10)
{
myservoy.writeMicroseconds(map1);
}
if(servo==11)
{
myservoz.writeMicroseconds(map1);
}
delay(15);
}
}
PS i decided to do the math for the map function because map(..) by itself truncates.