Starting to combine codes to make my own. Looking for help.

Just wanting to make a thread that I can come back to and ask questions for this little project I am doing.

So I am working on making it so that I can push a button or flip a switch and have two servos do what ever it is I want them to do then reset and go back to normal. I am just starting with the basic code for one servo and adding bits and pieces to make it do what I want.

Here is what I have.

#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;

void setup(){
myservo1.attach(9);
myservo2.attach(10);
}

void loop(){
for(pos = 0; pos <180; pos += 1) //goes from 0 to 180 degrees
{
myservo1.write(pos);
delay(15);
myservo2.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo1.write(pos);
delay(15);
myservo2.write(pos);
delay(15);
}
}

I am currently just trying to get the servos to rotate back and fourth. The issue I am running into is that the servos are not operating smoothly. They run back and fourth but they seem to be very choppy, I have my circuit built on the breadboard. All I am doing is hooking an external 9v battery up to the +/- rows on my bread board. I then ground the arduino to the bread board. I use jumpers to connect the positive and negatives of the servos to the +/- rows.

Ill get pics of needed I am just trying to fiugre out if the choppy servo is normal or if its another issue.

Thanks

A 9v (pp3) battery is unlikely to supply enough current for servos. Also, servos usually should be supplied by a max of 6volts.

If you are new to the Arduino and programming you may be interested in this demo sketch I wrote
http://forum.arduino.cc/index.php?topic=223286.0

...R

Thanks ill go check it out! Ill also run and pickup a different battery pack and see if that helps me any.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 5; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
Servo servo2;

void setup()
{
pinMode(button1, INPUT);
servo1.attach(10);
servo2.attach(9);
digitalWrite(5, HIGH); //enable pullups to make pin high

}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(179);
servo2.write(90);
delayMicroseconds(10000);
servo1.write(90);
servo2.write(179);
delayMicroseconds(10000);
}
else {
servo1.write(92);
servo2.write(179);

}

}

Here is the code I am working with. It works fine except I have two issues.

#1: The servos wont operate at the exact same time. There is a delay before the 2nd one works. can I make them both operate at the exact same time?

#2: The delay function I am using does not delay long enough. Is there a delay that will allow me to delay for say 2 minutes without delaying the actual operations? just pausing for 2 minutes before starting the next one

flyfisher117:
#1: The servos wont operate at the exact same time. There is a delay before the 2nd one works. can I make them both operate at the exact same time?

There's nothing in the code to cause that. Maybe your power supply doesn't have enough capacity to operate both servos at the same time. How are you powering them?

flyfisher117:
#2: The delay function I am using does not delay long enough. Is there a delay that will allow me to delay for say 2 minutes without delaying the actual operations? just pausing for 2 minutes before starting the next one

Delay what without delaying what operations? If you want your sketch to stop, put a delay in. If you don't want it to stop, don't put a delay in.

Study the concept in the Blink Without Delay example sketch and get rid of all the delay() functions from your code.

...R

I am using an external power pack with 2 AA batteries. Both and brand new Duracell batteries..I wasnt sure if the delay was caused by the wiring or if it was because of my code. Just wanted to check. Ill play around with it and see if I dont have some weird wire causing the delay.

So I want to make it so that
-If the button is not pushed the servos hold at their set angles.
-When the button is pushed and held in, the servos lower to a new position and hold that position for say 2 minutes, then, even though the button is being held the servos will go back to their original position. if I use "delay(...)" that will also delay my servos all I want to happen is as soon as the button is pushed the servos drop to their new position and hold it for 2 minutes before they go back.

EDIT: Ok so for some reason it has started working properly. I switched the delaymicroseconds back to just delay for some reason its doing exactly what I wanted... It did do this before so ill have to see if I can trouble shoot why it doesnt always work consistantly

If you are thinking of doing other Arduino projects or if you are thinking of extending your current project study the Blink Without Delay sketch and learn how to manage without using delay(). The delay() function just paralyzes the Arduino until its finished. Without it the Arduino could do other useful stuff (like checking a button press) during that time.

...R

So I spent about 30 minutes checking it out and it sort of makes sense, but if I try to do it myself with the servos it makes no sense at all. I can see where it would be useful and I may need it because if at all possible I need to figure in 2 DC motors in this sketch.

I am getting lost in all of the if and else loops and then I am not positive how to include my push button and make the gate drop... it makes sense with the LED but there is a reason I didnt go with EE or CS as a major... I am very hands on and visual and this is all too abstract for me to fully visualize what is going on.

flyfisher117:
I am very hands on and visual and this is all too abstract for me to fully visualize what is going on.

I sort of understand where you are coming from ... but .. Computer programs are very unforgiving and I have found that it is essential to be able to write down (in plain language, not computer code) all of the detailed steps that are necessary to cause a project to function. Then it is usually very easy to convert that into computer code.

I find that my brain can comprehend a project in total in what feels like a comprehensive way but the reality is that it only has an overview of the project. Writing it down forces my brain to deal with the essential details.

Another major advantage of writing down the details is that you have information that will help to isolate problems when the code doesn't do exactly what you thought. Most of the time on any project is spent debugging it and its a good idea to plan for that at the design stage so that you can associate the symptoms of an error with your design.

The other side of the coin, when I get a piece of code that I need to understand it often takes me 10 or a dozen readings, working through it with a pencil and paper and thinking about it as I fall asleep at night.

...R

Thanks, I wish I had the time to sit down and understand it all. I am just in a real time crunch. We are doing a project with the arduinos and I have to have it all programmed and ready to rock by the end of next week. My group needs a prototype and I need the arduino and the electronics ready to go by then.

Its frustrating trying to learn the arduino while balancing a bunch of other really challenging classes.

I think the Millis delay is what I need but trying to figure it out is very frustrating and I have yet to even begin with the circuit for my motors and the code to run them.. :drooling_face:

So I really need help with this stupid project... I am getting no where and I can not get anything to run reliably. Went to my professor for assistance and he told me to slow down and work everything step by step.. I have tried and I am getting lost/purely pizzed off with this project.

At this point I don't care if I use a button or not. I need to use 2 servos and 1 toy DC motor.

When I plug the Arduino in the servos need to hold our bridge vertically. They then need to lower the bridge and hold it at a horizontal angle. I then need a DC motor to run. Then need the servos to raise the bridge back to a vertical position.

I need to be able to change the timing of all this to time everything so that it works correctly exactly when I need it too.

What is upsetting me the most is getting the stupid DC motor to run properly. One second it won't even run at all, then the next second it runs perfectly. But when the DC motor runs my servos freak out and start just spinning uncontrollably in any direction they please. They will not hold their position.

Here is my current code.

#include <Servo.h>

Servo servo1;
Servo servo2;
int motorPin = 6;

void setup()
{
digitalWrite(motorPin, OUTPUT);
servo1.attach(10);
servo1.write(90);
servo2.attach(9);
servo2.write(180);
}

void loop()
{
delay(5000);

servo1.write(180);
servo2.write(90);

delay(5000);

digitalWrite(motorPin, HIGH);

delay(2000);

servo1.write(90);
servo2.write(180);

}

Here is my current set up... The green wires are the servos yellow is my digital pin wire to the motor.. I have tried multiple external power packs with no luck.

This is the motors circuit.

This is the whole board... ignore the button

Can anyone just help me get this all working???

The red wire in the bottom power bus is not connected to anything.

Yes I know. Like I said I have been trying many different external power packs... I wanted the arduino, the servos, and the motor to run off its own power so I connected the motor down there. I just made sure they all had a common ground.

I can get all 3 pieces to operate just not properly... If I just attach the motor it runs fine. If I just attach the 2 servos they run fine. If I attach the motor and servos it all goes wrong. Nothing works properly and when all 3 work in order like they are supposed to the servos are out of control.

flyfisher117:
What is upsetting me the most is getting the stupid DC motor to run properly. One second it won't even run at all, then the next second it runs perfectly. But when the DC motor runs my servos freak out and start just spinning uncontrollably in any direction they please. They will not hold their position.

That makes me suspect that either the power supply is not up to the job and the load is causing the voltage to fluctuate enough to affect the other components, or it's generating electrical noise on the power supply lines which are affecting the other components.

If you leave absolutely everything else the same and just disconnect one wire to the motor, does everything else start working sensibly? That would support the theory that it's an electrical issue rather than wiring fault or software.

So What I have been trying is running the servos off the arduino's 5V pin with the Motor running off of a 9V battery pack. Everything gets all weird.

I tried running the arduino off of my laptop, running the servos off of a 2AA battery pack, and running the motor off of a 9v battery pack. Still everything goes crazy.

I tried running the arduino off of my laptop with both the servos and the motor on a single 2AA battery pack. Still acts up..

I finally ran the arduino off of my laptop, with the servos on their own 2AA battery pack and the motor on its own 2AA battery pack.. STILL acting up.

When I unplug say the yellow wire from the motor (wire that closes the gate and lets the motor work) the servos will work good, then they will go weird, then they will work good, then they will go weird. In that pattern..

And actually I had it closer to working WITH the button than without. Without the button I can hardly get anything to work so I don't know whats going on.

2AA battery pack.

??

What batteries (Ok, Cells...) are you using. It seems you have only 2.4 to 3.0 v there (depending on batteries). MAYBE 3AA if they are 1.5V cells, but better is 4AA NiMH which will be 4.8 to 5V for Servos. And decent current capability.

Other: "Combining Codes": This is always a challenge. Sorry I'm late to the party, but I suggest seeing this on the ArduinoInfo.Info WIKI HERE: (This is only CODE suggestions..)

I agree with @terryking228, this sounds like an inadequate power supply.

Get a battery pack with 4 NiMh cells and then follow a rigorous one at a time testing program ...
Test the software with no motors/servos connected
Test the software with each motor/servo one at a time
Test the software with 1 servo and 1 motor, and with 2 servos and no motor
Test the software with all three

...R

These are all Energizer Advanced Lithium batteries. So when I unplug the motor the servos run perfect. When I unplug my Servo's control cables The motor runs perfect but the servos still spin. I feel like I have some current getting loose and running where ever it wants.. I can't isolate what is causing the servos to spin at their free will.

I wish I had my little RC AirPlane battery packs to play with.

Just found out something that may have been an issue. My battery packs had the batteries in parallel... Got two 4AA battery packs in series. Will test them out and see what happens.