random servo programming

Hi. Finally found how to add a topic. Ok so I need a little help with programming 4 servos. I have used the sweep servo example to move one servo from 0 to 90 degrees.

I then copies and pasted this for the other three servos to run in sequence. However, whe. It gets to the last servo it just keeps repeating the movement and does not go back to the beginning of the loop, so there is something missing somewhere.

Secondly I would like to have the servos open and close together in twos, threes or all together in a random sequence and at varying times. I.E. 1&2. 1&4. 3&4. 1,2,4 etc with varying delays between the movements.

I have a sketch on another Arduino that does this for six servos so I know it can be done but I don't know if I can copy this sketch across or need to write a new one

I hope somebody can help me out with this frustration.

Got some code? We can’t help fix what we can’t see.
Use the code tags button, </> on the menu

so it looks like this

juss1972:
I have a sketch on another Arduino that does this for six servos so I know it can be done but I don't know if I can copy this sketch across or need to write a new one

yes you can copy sketches. you may have to change the name to match the directory name

Or open the existing, do a Save As, and let the IDE create the directory.

juss1972:
I have a sketch on another Arduino that does this for six servos so I know it can be done but I don't know if I can copy this sketch across or need to write a new one

If you mean that you have an Arduino that does what you want but you don't have the sketch that was uploaded to that Arduino it becomes much harder. You CAN read the binary of the program out of the Arduino's memory... but the binary is NOT the sketch and all you could do with it is upload the binary to another Arduino of basically the same model. That would give you ANOTHER Arduino that does the exact same thing. You would not be able to edit the binary to modify the behavior in any way.

Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Thanks for the replies. I have changed the code I was using. I have now put the stock adafruit pwm servo library in to move the servos. All four are doing what I want now, opening and closing one at a time.

What I now need to figure out is how to get them moving together, either two, three or all of them. As in servo 1-3, 1-4, 1-2-4 etc.

Once that's sorted I need to add an auto function to pick which servo/servos to move instead of the same loop all of the time.

If you want help then post the code you have. Otherwise you're welcome to carry on sorting it out on your own.

If you're using the "Adafruit pwm servo library" then I imagine you're using a PCA9685 servo card, something you didn't mention. That needs completely different code from the standard Servo library that most of us would naturally think of.

Steve

Oh ok yes I'm using a nano with an extension board and the pwm board. I know nothing about Arduino coding that's why I put the library sketch in. The sketch that has the six servos in that I'm using for something else only has them twitching at certain times randomly.

Well here we are at reply #9 with no code and no schematic.

I'm out at the minute so can't send anything. But I have done nothing to the code of the pwm library. I'll see if I can post the other sketch with the twitch settings in

Think I’ve done it lol this is the sketch with the servo section in it

FlthyHPs_v1.8_Ben.ino (91.8 KB)

Hi,
Why all that code?
I can see it is toooo big to post in code tags.
Didn’t you just write code for the servos and get them performing how you liked before adding it to thew rest?

Can I suggest you write code JUST for the servos and get that code running.

A schematic would also help.

Thanks… Tom… :slight_smile:

That code was not written by me. Is has been made for a specific task to move six servos and light LEDs randomly. What I'm looking for is how to get four servos to move randomly on a different sketch randomly.

You started out by saying "I have used the sweep servo example to move one servo from 0 to 90 degrees.

I then copies and pasted this for the other three servos to run in sequence. However, whe. It gets to the last servo it just keeps repeating the movement and does not go back to the beginning of the loop, so there is something missing somewhere."

So where is that code? If you post THAT code we may be able to see what's missing and how you can improve it. The six servo code is far too confusing for anyone with very limited programming experience to be messing with.

Steve

Yeah I'm not going to mess with that code I just hope someone could find in it what I needed in the new code to make the four servos random. The initial code I used was the basic servo example in Arduino then just copied it. But that's was being used without the pwm board. So thats why I wired up the pwm board to the nano extension board and used the adafruit pwm library. They move quicker doing it this way so I wanted to add a randomiser to get them moving independently and various times. I have not added anything to the pwm code. I will see if I can get the original code back to out on hear but all I did was copy and paste the sketch to make four servos instead of just the one

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo1.attach(4); // attaches the servo on pin 9 to the servo object

myservo1.attach(5); // attaches the servo on pin 9 to the servo object

myservo1.attach(7); // attaches the servo on pin 9 to the servo object

myservo1.attach(7); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 100; pos += 3) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos -= 3) { // goes from 180 degrees to 0 degrees
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
{
for (pos = 0; pos <= 100; pos += 3) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos -= 3) { // goes from 180 degrees to 0 degrees
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
{
for (pos = 0; pos <= 100; pos += 3) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos -= 3) { // goes from 180 degrees to 0 degrees
myservo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position

{
for (pos = 0; pos <= 100; pos += 3) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos -= 3) { // goes from 180 degrees to 0 degrees
myservo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

}

}}}}

  myservo1.attach(4);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(5);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(7);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(7);  // attaches the servo on pin 9 to the servo object

TYPO: You should generally not attach the same servo multiple times.

What you MEANT was:

  myservo1.attach(4);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(5);  // attaches the servo on pin 9 to the servo object
  myservo3.attach(7);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(7);  // attaches the servo on pin 9 to the servo object

But then you have the problem:

  myservo3.attach(7);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(7);  // attaches the servo on pin 9 to the servo object

TYPO: You should not attach two servos to the same pin.

Fix those and then look for other simple mistakes before you ask for more help.

REMEMBER TO SELECT Tools->Auto Format before you post code. If you then use Edit->Copy for Forum you can paste into the forum with the code tags included.

I just wrote that code just now to show what I'd done. The code I did toast night was all correct but I didn't save it

Ok so that code is now workin and looping. So now I need to add a random setting for when and how many servos are activated.