Hello everyone my problem for this topic is i cant use delay() function with my servos. First of all i am using an external power supply (ground connected to arduino) and 8 micro servos with an arduino uno.
My problem is that when i run the Servo sweep example code, it works just fine. But when i write own code with different functions that contain different delays, the servos just simply don't do what i tell them. When it comes to a delay function all the code just stops functioning. But... if i use the serial monitor and write a character each time a delay is executed, i can see the characters with the right amount of delay but servos still dont work.
What am i supposed to do at this point? Can i use a different delay library (if there is one)?
Here's the code im using.
#include "Servo.h"
Servo BRF, BLL, FRF, FLL, FLF, FRL, BRL, BLF;
void setup() {
// put your setup code here, to run once:
FRF.attach(4);
FRL.attach(7);
Serial.begin(9600);
}
void loop() {
WalkForward();
}
void WalkForward(){
FRL.write(90);
FRF.write(155);
delay(1000);
Serial.write("a"); // This functions perfectly
FRL.write(50);
FRF.write(155);
}
When it comes to a delay function all the code just stops functioning
That's what delay() does
Can i use a different delay library (if there is one)?
Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.
UKHeliBob:
That's what delay() does
yeah but it also should stop it for the amount of time i give it, it just stops the code forever.
So ive tried the millis() function instead of delay and it still doesnt work. Heres the code...
#include "Servo.h"
Servo BRF, BLL, FRF, FLL, FLF, FRL, BRL, BLF;
unsigned long currentMillis;
unsigned long startMillis;
void setup() {
// put your setup code here, to run once:
BRF.attach(2);
BLL.attach(3);
FRF.attach(4);
FLL.attach(5);
FLF.attach(6);
FRL.attach(7);
BRL.attach(8);
BLF.attach(9);
Serial.begin(9600);
startMillis = millis();
}
void loop() {
currentMillis = millis();
WalkForward();
}
void WalkForward(){
FRL.write(90);
FRF.write(155);
if(currentMillis - startMillis >= 10000){
Serial.println("a");
FRL.write(50);
FRF.write(155);
startMillis = currentMillis;
}
}
piskopat39:
yeah but it also should stop it for the amount of time i give it, it just stops the code forever.
Except it obviously doesn't if the Serial.print() works. If you put another Serial.print() after the second servo writes() do it ever get to that one?
So the question may be...what goes wrong when when you try to write to your servos for the second time? So exactly what servos are they and what is the specification of the "external power supply".
Steve
So my servos are Tower Pro MG90S metal gear micro servos. And my power supply is an old 12v 30a supply which i run a t 5.3volts for the servos.
And i have tried putting Serial.print every time i write to the servo. And it works just fine every single time. Just the servo.write isnt executing.
Maybe i should try another servo library.
Yeah guys tried out the "varSpeedServo" library and it has its different functions that did the job.
void loop() {
WalkForward();
}
void WalkForward(){
FRL.write(90);
FRF.write(155);
delay(1000);
Serial.write("a"); // This functions perfectly
FRL.write(50);
FRF.write(155);
}
Why not try READING YOUR CODE! Your telling the servo not to move
Mark