Board Running, Servo Not. Analog vs Digital Servo?

Hi there,

I am trying to do a DIY robot fish for work, from this website: http://www.open-electronics.org/robofish-create-your-robot-fish-with-arduino/ (the website has the code for the fish too). I have an arduino nano, two IR sensors, magnet switch, 4 AA batteries with holders and three servo motors. The whole circuit has power, sensors are running fine, too, but whenever the board is turned on or reset the 3 servos turn a few quick notches and stop.

The DIY fish had an Uno and digital servos. Problem is, I accidentally ordered analog servos (fitec fs90 micro servo 4.8V, http://www.robotshop.com/ca/en/9g-micro-servo-motor-4-8v.html?gclid=CIi87bTG1c4CFQaJaQodNvUCzA).

I tried switching the signal pins in the code and on the board to analog pins, no luck. I tried to attach the signal wires to digital pins with PWM (pins 5, 6, and 9) and use analogWrite() in place of the Write() command in the code and I get “‘class Servo’ has no member named ‘analogWrite’”.

Can someone please tell me what I’m doing wrong? I would greatly appreciate it.
Thank you so much!

This is the code with the changes I made, the original is on the link for the DIY fish at the top

// ROBOFISH 
// di Segatello Mirco
#include <Servo.h>
Servo Servo1, Servo2, Servo3; // create servo object to control a servo
 
int i, time, obstacle;
int pos1, pos2, pos3; 
int pos1R, pos2R, pos3R; 
int phase=45;
int velocity=2000; 
int maxDeflexion=20; 
int maxDefobs=20; 
int actualTime;
float shift;
const int center1=98; 
const int center2=90;
const int center3=105;
const int sens_SX=7; 
const int sens_DX=8;
const int lostTime=3000;
 
void setup()
{
Servo1.attach(5);    
Servo2.attach(6); 
Servo3.attach(9); 
pinMode(sens_SX, INPUT);
pinMode(sens_DX, INPUT);
pinMode(13, OUTPUT);
time=velocity/360;
shift=0;
 
}
 
void loop()
{
for (i=0; i<360; i++) {
 
pos1 = i+2*phase;
pos2 = i+phase;
pos3 = i;
 
if (pos1>359) pos1-=360;
if (pos2>359) pos2-=360;
if (pos3>359) pos3-=360;
 
if (pos1>179) pos1=360-pos1; 
if (pos2>179) pos2=360-pos2;
if (pos3>179) pos3=360-pos3; 
 
pos1R=map(pos1,0,180,center1-maxDeflexion-obstacle,center1+maxDeflexion-obstacle);
pos2R=map(pos2,0,180,center2-maxDeflexion-obstacle,center2+maxDeflexion-obstacle);
pos3R=map(pos3,0,180,center3-maxDeflexion-obstacle,center3+maxDeflexion-obstacle);
 
Servo1.analogWrite(pos1R); 
Servo2.analogWrite(pos2R); 
Servo3.analogWrite(pos3R); 
delay(time);
 
obstacle=int(shift);
 
if (digitalRead(sens_DX)==0) { 
if (obstacle<maxDefobs) shift=shift+0.05; 
actualTime=millis();
}
if (digitalRead(sens_SX)==0) { 
if (obstacle > (-maxDefobs)) shift=shift-0.05;
actualTime=millis();
}
 
if (digitalRead(sens_SX)==1 && digitalRead(sens_SX)==1 && obstacle!=0)
if (millis()>actualTime+lostTime) {
if (shift>0) shift=shift-0.05;
if (shift<0) shift=shift+0.05;
}
}
}

Servo1.analogWrite(pos1R);

as the error said, analogWrite is not the command of the lib. Also, the servos don't exactly use PWM so the pins don't matter, and the analogWrite you were trying wouldn't work anyway.

Digital servos do high/low pulses, analog servos afaik use varying voltages.
Arduino doesn't put out varying voltages. Even during analogWrite, it's still high/low, just a matter of ratio.
No amount of coding can make the Arduino vary voltage. To get that, you need some trickery
https://arduino-info.wikispaces.com/Analog-Output

Or just buy the right servos.

AFAIK digital servos work with the Servo library just like analog servos. The "digital" stuff is inside the servo.

You can use any of the Arduino I/O pins to control a servo with the Servo library. Use the Servo sweep example that comes with the Arduino IDE for a test program.

analogWrite() is used with a suitable h-bridge driver for controlling the speed of DC motors - it is not used with servos.

...R

There is no difference in how you use an analog v. a digital servo, just use the Servo library.

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How are you powering your servos?
If you are using the same 4AA's that power everything else you will having power supply problems due to the power requirements of the servo's.

Thanks.. Tom.... :slight_smile: