Swong46
February 28, 2016, 1:46am
#1
Hi, below is my code for running three servos. I am only getting a signal on the first servo and doesn’t seem to change if I move things around. Looks like “myservoa.attach(7);” is good but the other servo attach lines are thrown out. Help is greatly appreciated. Thanks
#include <Servo.h>
Servo myservoa, myservob, myservoc;
void setup() {
myservoa.attach(7); // attaches the servo on pin 9 to the servo object
myservob.attach(8);
myservoc.attach(9);
}
void loop() {
for (int pos = 1400; pos <= 1700; pos += 1) {
myservoa.writeMicroseconds(pos);
delay(5);
}
for (int pos = 1700; pos >= 1400; pos -= 1) {
myservoa.writeMicroseconds(pos);
delay(5);
}
for (int pos = 1000; pos >= 1500; pos -= 1) {
myservob.writeMicroseconds(pos);
delay(5);
}
for (int pos = 1500; pos <= 1000; pos += 1) {
myservob.writeMicroseconds(pos);
delay(5);
}
for (int pos = 1000; pos >= 1500; pos -= 1) {
myservoc.writeMicroseconds(pos);
delay(5);
}
for (int pos = 1500; pos <= 1000; pos += 1) {
myservoc.writeMicroseconds(pos);
delay(5);
}
}
JimboZA
February 28, 2016, 5:50am
#2
I'm no C expert but is this kosher?
Servo myservoa, myservob, myservoc;
Try:
Servo myservoa;
Servo myservob;
Servo myservoc;
Swong46
February 28, 2016, 7:20am
#3
That's what I used before, no go. I should say that I am using a Sparkfun Redboard...
MarkT
February 28, 2016, 12:05pm
#4
JimboZA:
I'm no C expert but is this kosher?
If you are not an expert, don't muddy the waters!
MarkT
February 28, 2016, 12:10pm
#5
Nothing to do with the attach() calls.
Your first error is on this line - can you spot it?
for (int pos = 1000; pos >= 1500; pos -= 1) {
Swong46
February 28, 2016, 7:49pm
#6
MarkT, I assume you are talking about calling the var pos inside the loop. I had it outside the for loop as a global var before and no dice.
However, it works now that I initialized the var in the void loop().
#include <Servo.h>
Servo myservoa;
Servo myservob;
Servo myservoc;
void setup() {
myservoa.attach(7);
myservob.attach(8);
myservoc.attach(9);
}
void loop() {
int position;
for(position = 70; position < 120; position += 1)
{
myservoa.write(position);
delay(20);
}
for(position = 120; position >= 70; position -= 1)
{
myservoa.write(position);
delay(20);
}
for(position = 70; position < 120; position += 1)
{
myservob.write(position);
delay(20);
}
for(position = 120; position >= 70; position -= 1)
{
myservob.write(position);
delay(20);
}
for(position = 70; position < 120; position += 1)
{
myservoc.write(position);
delay(20);
}
for(position = 120; position >= 70; position -= 1)
{
myservoc.write(position);
delay(20);
}
}
system
February 29, 2016, 8:13pm
#7
MarkT, I assume you are talking about calling the var pos inside the loop.
You can't call a variable. You can only call functions.
The for statement that MarkT showed says "Starting with pos = 1000, while pos is greater than or equal 1500, do stuff and then decrement pos by 1." How many times will that loop iterate? Does the number zero ring a bell?
Ah! I see, had my arrows backwards.