Go Down

Topic: really need help with 4 servos with the arduino (Read 4 times) previous topic - next topic

afap

Quote
ou're missing a closing brace on "setup ()".


i know im just so sleepy im having a hard time putting it in right. im off to bed.

DaveK

Quote

wow you really put some work into it, Thank you.


Happy to help, or try to anyway :)

Quote

The code you wrote and told me to mod is a little over my head.  


Sorry, I forgot that you're still pretty new to coding. The modification I suggested is just to replace the 'val = 70;' lines with an analogRead() and map(), so that what ends up in val is from one of your pots instead of a constant. It would be something like this:

Code: [Select]

#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;

void setup()
{
 Serial.begin(115200);
 servo1.attach(8);  
 servo2.attach(9);  
 servo3.attach(10);  
 servo4.attach(11);
}

void loop()
{
 int a = analogRead(0);
 Serial.print("Value: ");
 Serial.println(a);
 int val = map(a, 0, 1023, 0, 179);    
 Serial.print("S1: ");
 Serial.print(val);
 servo1.write(val);
 Serial.print(", ");
 Serial.println(servo1.read());
 
 a = analogRead(1);
 Serial.print("Value: ");
 Serial.println(a);
 val = map(a, 0, 1023, 0, 179);    
 Serial.print("S2: ");
 Serial.print(val);
 servo2.write(val);
 Serial.print(", ");
 Serial.println(servo2.read());
 
 a = analogRead(2);
 Serial.print("Value: ");
 Serial.println(a);
 val = map(a, 0, 1023, 0, 179);    
 Serial.print("S3: ");
 Serial.print(val);
 servo3.write(val);
 Serial.print(", ");
 Serial.println(servo3.read());
 
 a = analogRead(3);
 Serial.print("Value: ");
 Serial.println(a);
 val = map(a, 0, 1023, 0, 179);    
 Serial.print("S4: ");
 Serial.print(val);
 servo4.write(val);
 Serial.print(", ");
 Serial.println(servo4.read());

 Serial.println();  
 delay(100);
}


This reads from analog pins 0-3 and writes to digital pins 8-11, so be very careful to make sure that the servos are wired to the correct inputs.

Quote

Now if i were to run this program would i have to have it plugged  into the usb? My set up is i can keep it plugged into the usb for more then testing.


Ideally, yes, keep it connected to the USB port and after you upload the sketch click on the serial monitor button. When the sketch starts it'll start dumping the information about what it is reading and writing to the serial port. The output will looks something like this for the first sketch I posted:

Quote

Input: A0
Value: 512
S1: 89, 89
S2: 70, 70
S3: 90, 90
S4: 120, 120

When you type a '1' into the serial monitor and send it the Input field will change to 'A1', indicating that it is now reading from analog pin 1 instead of 0.

The second sketch (the one in this post) doesn't take serial input, but it will print out what it is reading from each analog input and what it is writing to each servo. Should look something like this:

Quote

Value: 512
S1: 90, 90
Value: 512
S2: 70, 70
Value: 512
S3: 90, 90
Value: 512
S4: 120, 120


As you turn one of the pots you should see one and only one of the 'Value' fields change, and only the 'S' line right after that value should change. Only one servo should respond.

If you get multiple values changing or multiple servos moving when you adjust any single pot then something is screwed up :)

Also, in that first sketch I posted I left a constrain() call in after the analog read. You might want to remove it, that was just to keep my servo in-range (it's a steering servo on a car, not full 180 sweep).

afap

Yes just to be sure i re downloaded it last night. let me know if im doing it right i got the zip file downloaded, then i went the the arduino folder opened it then opened hardware folder then libraries folder i deleted the old MegaServo file and unzipped the new one there. dose that sound right?

DaveK

Yep, that's right. You should get a .o file in the MegaServo folder after you compile.

afap

http://i380.photobucket.com/albums/oo241/Googlefishchris/NewBitmapImage.jpg

here is a pic of what im seeing when i try that last code that DaveK posted. it did not work. it made to of the pots do this same thing and it was very shaky. and take a look at the pic whats up with the Serial?

beige

Set the baud rate in the serial monitor to match the program 8-)

afap

ok here is an up date. I replaced 2 of the pots. and then i tryed this code again
Code: [Select]
#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;  
int potpin4 = 3;

int val;    // variable to read the value from the analog pin

void setup()
{
 servo1.attach(9);  // attaches the servo on pin 9 to the servo object
 servo2.attach(10);  // attaches the servo on pin 10 to the servo object
 servo3.attach(11);  // attaches the servo on pin 9 to the servo object
 servo4.attach(8);  // attaches the servo on pin 10 to the servo object
}

void loop()
{
 val = analogRead(potpin1);
 val = map(val, 0, 1023, 0, 179);    
 servo1.write(val);
             
 val = analogRead(potpin2);
 val = map(val, 0, 1023, 179, 0);    
servo2.write(val);
 
 val = analogRead(potpin3);
 val = map(val, 0, 1023, 0, 179);    
 servo3.write(val);
 
 val = analogRead(potpin4);
 val = map(val, 0, 1023, 0, 179);    
 servo4.write(val);
 
 delay(15);
}




it works.........................but i dont know why but 2 of the servos sake a little for then i would like. but there is no bleeding over as far as i can tell. The only different on the pots is that the ones i was using before were big and the ones im using now are little. i dont know if that maters or what, but something is different.  any thoughts on this?

mem

You may need to add decoupling capacitors.  Try some 0.1uf ceramic capacitors across the servo power rail. If you have long wires connected the servos put capacitors at both the servos and power supply  ends.

DaveK

Did you try the sketch I posted again with the baud rates corrected? I'm curious to see what the serial output is when the problem is happening.

afap

#69
Aug 08, 2009, 09:59 pm Last Edit: Aug 08, 2009, 10:04 pm by Chris_Herrera Reason: 1
yes i did DaveK and sorry for not posting my findings. thats what led me to chainge the pots. even when i was not moving the pots it would chaing just a little. the program it self did not work thow 1 of the pots would more 2 servos and one of the other pots would more 2 servos and the last 2 pots did nothing. lots of shaking. something was wrong with the code because i was never getting that much shaking and i have always had 3 servos working right.

when i started this project i was only using 2 servos for 1 cam and that all worked fine now i added to more servos for cam number 2 and thats when the trubble started. so the pots that i replaced were the 2 new ones i had added.

MeM
Quote
You may need to add decoupling capacitors.  Try some 0.1uf ceramic capacitors across the servo power rail. If you have long wires connected the servos put capacitors at both the servos and power supply  ends.


is this what your talking about? http://cgi.ebay.com/50-pcs-Ceramic-Axial-Capacitors-0-1uF-100nF-20-50V_W0QQitemZ290328838783QQcmdZViewItemQQptZLH_DefaultDomain_210?hash=item4398f2827f&_trksid=p3286.c0.m14

is there any wrong way to install them?

could you please tell me more about this?

jezuz

Quote
is there any wrong way to install them?


Nope, they are non-polar, so either way is fine... And I am pretty sure your link has the correct item.

like mem said, these are used to get rid of electrical noise, which is probably what is causing the twitchy-ness of the servos...

mem

Yes, that's what I am talking about. They are a very common component so if you have a local electronics shop you can probably pick them up there.
They are connected between the positive power line and ground, the polarity doesn't matter.

There is an article here that goes into the technical details: http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

BTW, how long are the wires connecting your arduino board to each of the servos?  and to the pots?

DaveK

Quote

yes i did DaveK and sorry for not posting my findings. thats what led me to chainge the pots. even when i was not moving the pots it would chaing just a little.


A little bit of jitter is normal until you add a bit of hysteresis to the control system. Imagine the thermostat that measures the temperature in a home and turns the heater on or off to control the heat. If the thermostat is set for a comfortable room temperature of 62F it must allow the actual temperature to vary somewhat from the setting. If it were to turn on the heater every time there were a small change in the measurement the heating system would be cycled on and off on very short cycles. This would be inefficient and would probably eventually damage the system.

The same thing applies with servo controls. If you want to eliminate small amounts of jitter resulting from unstable input readings you can do so by adding some memory of the previous input set point and only changing the output when the input deviates by larger amounts.

If you are getting large amounts of jitter something else might be wrong.


It appears that you have the problem solved, excellent!

So take the below as an analysis of how I would go about solving the problem as guided by the general principles I've learned.

Quote

the program it self did not work thow 1 of the pots would more 2 servos


I believe that the program is fine. That was why I tested it on my hardware here and then sent it to test on your hardware, to isolate problems. On my hardware a single pot does not affect two servos. To help to track down why it does this on your system the program prints out the inputs and outputs for each servo.

If when you turn one pot you have corresponding movement in two servos you should watch the output from the sketch to see where the problem is starting. There are four obvious possibilities:

First; the input hardware. The input of one potentiometer is connected to two analog input pins. If this is the case then when you turn the problem potentiometer you will see changes in more than one of the four 'Value' fields.

Second; software assignment of the position. If there is a problem in calculating the target servo position from the analog input, such as if the value from a single analog input were being assigned to two 'servo' objects then while the 'Value' field for a servo remained unchanged, the first value in the coordinate pair of the 'Sn:' field would change. This would indicate a problem in the sketch software.

Third; software problem in the servo library. If a problem in the servo library caused the input to one servo object to affect another than this /might/ appear in the value read from the servo. This is displayed as the second value in the coordinate pair of the 'Sn:' field (where 'n' is the index of the servo). The second value should always match the first value. Problems in the servo library could be difficult to track down in this way, however, by testing on my hardware and by virtue of being used by many other people, it is quite unlikely that the servo library is the problem, though certainly not impossible.

Fourth; hardware output problem. If two servos move when one pot is moved and all of the fields in the serial output indicate that only one analog value is changing, and that only one servo is being commanded to move, then the most likely problem is that two more more outputs are cross connected. In this case the cross connected servos would respond to changes in multiple pots, and they would likely exhibit substantial jitter as the attempted to read the overlapping command signals.

Quote

and one of the other pots would more 2 servos and the last 2 pots did nothing.


If turning two of the pots does not result in the two corresponding  'Value' field displaying corresponding changes then it is likely that one of the following is the problem, roughly in order of how likely: the pots are bad, the pots are incorrectly connected, that there is something wrong with the Arduino hardware, or that the software is reading from the wrong analog input.

Quote

lots of shaking. something was wrong with the code because i was never getting that much shaking and i have always had 3 servos working right.


Hardware that acts in the wrong way in a variety of ways does not necessarily indicate that the software is wrong. It would not be surprising if good software combined with bad hardware worked worse than bad software combined with bad hardware. (by 'bad software' I mean software that would not work correctly with good hardware).

I suggest you might be able to track down the problem by assuming that the sketch I posted above and tested on my simple bread-boarded hardware is probably OK (though I will review it again to be sure), and then look carefully at the serial output it produces to see if the chain of events between turning a single pot appears to be in order and that the correct outputs are generated by the software.

Break the problem down a step at a time, starting with the pot's (such as you did by replacing what may have been faulty hardware). Verify that the output of each step matches the specifications (particularly with respect to not influencing other outputs). Continually throwing different software at the system might eventually work, but it won't tell you what the issue is, and it will be confusing if the problem is an intermittent hardware issue.

afap

#73
Aug 09, 2009, 07:01 am Last Edit: Aug 09, 2009, 07:10 am by Chris_Herrera Reason: 1
DaveK YOU THE MAN!!! after reading what you said i went back and tryed your program with the new pots and really took the time to read the read out. O and now with the new pots the program is working like a CHAMP!!!! some of the pots thow on the read out on the val will not go all the way to 0 one will go no less then 18 I think ones at 5 and the other to are at 0. dose this mean the ones that will not go to 0 are bad? I only ask because there is a very little shaking still on cam number 2. but its not very bad at all.

mem
Quote
BTW, how long are the wires connecting your arduino board to each of the servos?  and to the pots?


The wires from the pots are about 6" from the arduino. the outputs going to the servos and 5 volts and ground ......... 2 are 20' and the other 2 are about 30', yes feet! I em only able to add stuff to the arduino side like the decoupling capacitors. I found this wire that has 8 wires inside (i think its some kind of phone line stuff) and was able to Power my cams 12 volts power my servos 5 volts send the servo signals AND stll have 2 left over for the video. it worked out very well.  

I'm gong to upload some vidos i made of this project so you all can take a look. ill post links in about 15 mins ;D

jezuz

Glad you have it working... this must have been the most commited troubleshooting discussion that actually led to a result that I have seen since my joining of the forum last winter...

Go Up