arduino not executing code properly when connected to 9v battery

my arduino code is executed perfectly when plugged into the computer, but when i use a 9v battery it executes a completely different code.

Well, maybe a little more info? What pin are you plugging the 9v to? Is this 9v battery a old fashion one that was used in old radios, or a 20 pound 9v battery. There is lots of problems trying to use the little 9v batteries. What load does your arduino/9v battery have to source? What does your volt meter read on that battery after 20 seconds of operation?

ok i have my 9v connected into the barrel connector and the voltage my arduino can accept is 7-12 v i believe.

Yes, the barrel connector can accept 9v. Can you take a reading with a voltmeter? Can you answer my other questions?

unfortunately i do not have a voltmeter

cunningcoder: my arduino code is executed perfectly when plugged into the computer, but when i use a 9v battery it executes a completely different code.

I hoped to not have to ask about this, but I guess I will. What are the two different codes that you refer to?

Please show us your script (in code tags ). What happens differently?

so the code i have uploaded to the board is this

<#include

Servo wheel1; // create a servo object to control a servo Servo wheel2; Servo sensorservo; int pos = 0;

void setup() {

wheel1.attach(8); // attaches the servo on pin 8 wheel2.attach(5); sensorservo.attach(10);

} void loop() { for(pos = 0; pos <= 150; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree sensorservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 150; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees { sensorservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } wheel1.write(0); wheel2.write(180);

} >

but for some reason when i use a 9v to it runs a program that would probably look this

<#include Servo wheel1; // create a servo object to control a servo Servo wheel2;

void setup() {

wheel1.attach(8); // attaches the servo on pin 8 wheel2.attach(5); void loop(){

wheel1.write(0); wheel2.write(0) }

Ok, two servos do require a lot of power. If you are not using a 2 pound or larger battery (9v), then you can expect problems.

Suggestion: 1. Get a volt meter. 2. Get a bigger battery.

its actually 3 servos and hc=sr04 sensor what battery do u recomend

cunningcoder: its actually 3 servos and hc=sr04 sensor what battery do u recomend

That's a tough question. It depends on a lot of variables. What size/weight is acceptable? How deep is your pockets? With info at hand, I would suggest a 12v battery since it is more standard. It may be good to place a couple silicon diodes in series to drop the 12v down a couple volts.

Is this a robot that moves around on it's own power source, or what?

what battery do u recomend

A battery pack aimed at a 3-servo remote-controlled airplane or car. 7.2V NiMH or ā€œ11.2Vā€ LiPo pack.
Or 6 AA batteries (preferably rechargeables, preferably Eneloops or similar) in a battery holder.

well my pockets are as deep as a cheap kiddie pool my robot weighs nothing because its built out of poster board and its 49 squared inches of posterboard so the base is light

So, you need a light weight battery, with more power than a little 9v.

Another thought, you may be able to save some power surges, with software. If you try to move the servo a long way, it uses a lot of power. If you do it slowly in code, it will use less power.

so after buying a new 9v battery i still have the same problem, but before buying a 12 battery do u think it will solve my first issue.

I think a battery that is small (less than 2 pounds) will not work well. A battery that is large, more than 10 pounds will work well. In between, it will be iffy, I think.

Get yourself a 6xAA battery back. 9V output and 2500mAH capacity, vs 200-300mAH capacity from little square 9V battery. Rechargable too.

9V battery ? Seriously ? Are you trying to tell us you are trying to run servos with a smoke alarm battery ? Are you serious ?