Hello I want to make a walking robot using micro servos and I don't know what code to use for the void loop. What works best?
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(0);
delay(1000);
or
// Sweep from 0 to 180 degrees:
for (angle = 0; angle <= 180; angle += 1) {
myservo.write(angle);
delay(15);
}
// And back from 180 to 0 degrees:
for (angle = 180; angle >= 0; angle -= 1) {
myservo.write(angle);
delay(30);
}
m4nthony:
What works best?
Whichever of those two causes the movement you want.
m4nthony:
What works best?
How fast do you want the servos to move ?
Instantly from one angle to the other (first code) or slowly from one angle to the other (second code) ?
I want the robot to just walk so I don't know what works better
Try both. Then you will know.
Have you tried both of them ?
Which is more likely to be applicable do you think ?
When you walk do your legs snap from one position to another or do they move slowly from one position to another ?
They move slowly but how can I get that motion and I did try both.
Double the delay()s in the second code. Does the servo movement slow down ?
gcjr
September 5, 2022, 2:25pm
9
if you need to coordinate the positions of various servos, doesn't it make sense to determine the intermediate positions for each servo during a complete "walk" cycle and perform a write() for each servo at each intermediate position
for (int n = 0; n < Npos; n++) {
for (int servo = 0; servo < Nservo; servo++)
myservo[servo].write (pos [servo][n]);
delay(30);
}
Depends on whether the coffee is ready, no?
Ok I know what to do but now when my ultrasonic sensor kicks in I just want one leg to move but the right still moves but in a delay.
#include <Servo.h>
const int TrigPin = A0;
const int EchoPin = A1;
Servo myservo1;
Servo myservo2;
const int DISTANCE_THRESHOLD = 50;
int pos = 0;
int pos1 = 90;
int pos2 = 80;
int pos3 = 100;
float duration_us, distance_cm;
void setup() {
Serial.begin (9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);
duration_us = pulseIn(EchoPin,HIGH);
distance_cm = 0.017 * duration_us;
myservo1.write(pos1);
myservo2.write(pos1);
delay(400);
myservo1.write(pos1 - pos2);
delay(40);
myservo2.write(pos1 + pos2);
delay(400);
myservo1.write(pos1 - pos2);
delay(400);
myservo2.write(pos1 + pos2);
delay(400);
if(distance_cm < DISTANCE_THRESHOLD) {
for (pos = 0; pos <= pos3; pos += 1) {
myservo1.write(pos);
delay(15);
}
for (pos = pos3; pos >= 0; pos -= 1) {
myservo1.write(pos);
delay(15);
}
}
}
Since your servos are named "myservo1" and "myservo2" it is hard to be sure what you mean by "one leg" and "the right". Do you mean "myservo1" is "LeftLegServo" and "myservo2" is "RightLegServo"? The sketch would be easier to understand if you used meaningful names.
Now you can see where you told the right leg to move when you didn't want the right leg to move:
#include <Servo.h>
const int TrigPin = A0;
const int EchoPin = A1;
Servo LeftLegServo;
Servo RightLegServo;
const int DISTANCE_THRESHOLD = 50;
int pos = 0;
int pos1 = 90;
int pos2 = 80;
int pos3 = 100;
float duration_us, distance_cm;
void setup()
{
Serial.begin (9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
LeftLegServo.attach(9);
RightLegServo.attach(10);
}
void loop()
{
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
duration_us = pulseIn(EchoPin, HIGH);
distance_cm = 0.017 * duration_us;
LeftLegServo.write(pos1);
RightLegServo.write(pos1);
delay(400);
LeftLegServo.write(pos1 - pos2);
delay(40);
RightLegServo.write(pos1 + pos2);
delay(400);
LeftLegServo.write(pos1 - pos2);
delay(400);
RightLegServo.write(pos1 + pos2);
delay(400);
if (distance_cm < DISTANCE_THRESHOLD)
{
for (pos = 0; pos <= pos3; pos += 1)
{
LeftLegServo.write(pos);
delay(15);
}
for (pos = pos3; pos >= 0; pos -= 1)
{
LeftLegServo.write(pos);
delay(15);
}
}
}
myservo1 is left and myservo2 is right but I still don't see the mistake.
If you don't want the Right servo to move, why are you moving the Right servo?
LeftLegServo.write(pos1 - pos2);
delay(40);
RightLegServo.write(pos1 + pos2);
delay(400);
LeftLegServo.write(pos1 - pos2);
delay(400);
RightLegServo.write(pos1 + pos2);
delay(400);
That's not the code im having problems with. Its my if statement im having problems with.
gcjr
September 8, 2022, 9:19am
17
what is the problem?
looks like when your if condition is true, myservo1 is moved from 0 to 100 and then back from 100 to 0
if(distance_cm < DISTANCE_THRESHOLD) {
for (pos = 0; pos <= pos3; pos += 1) {
myservo1.write(pos);
delay(15);
}
for (pos = pos3; pos >= 0; pos -= 1) {
myservo1.write(pos);
delay(15);
}
}
Thats what should happen but myservo2 moves with just with a delay
gcjr
September 8, 2022, 10:47am
19
are you sure each servo is being controlled correctly using myservo1 and myservo2?
Honestly i really don't know this is my first project
gcjr
September 8, 2022, 12:34pm
21
try running this
it should move one servo and then the other back and forth with a 1 sec delay between each movement
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos1 = 10;
int pos3 = 100;
unsigned long Pause = 1000;
void loop ()
{
myservo1.write (pos3);
delay (Pause);
myservo2.write (pos3);
delay (Pause);
myservo1.write (pos1);
delay (Pause);
myservo2.write (pos1);
delay (Pause);
}
void setup () {
Serial.begin (9600);
myservo1.attach (9);
myservo2.attach (10);
myservo1.write (pos3);
myservo1.write (pos3);
}