sorry for stupid post, im pretty knew to the whole electronics/robotics scene. I've built a bot which has 3 hxt900 servo's 2 hacked for continous rotation. Also a HC-SR04 ping sensor. when i plug in a 9v battery into it, it doesnt really do anything, the servos twitch and thats about it.
I thought this was due to lack of power, but everything works as it should when plugged into usb..
all my code is wrote(using what you linked), it works when on usb just not via a brandnew 9v battery.
code below....
#include <Ultrasonic.h>
#include <Servo.h>
Ultrasonic ultrasonic(13,12);
const int RForward = 0;
const int RBackward = 180;
const int LForward = 180;
const int LBackward = 0;
const int RNeutral = 93;
const int LNeutral = 92; //constants for motor speed
const int TrigPin = 13;
const int EchoPin = 12; //Echo pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance = 0;
int rightDistance = 0; //distances on either side
Servo HeadServo;
Servo LeftServo;
Servo RightServo; //declare servos
long duration; //time it takes to recieve PING))) signal
void setup()
{
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
RightServo.attach(11);
LeftServo.attach(10);
HeadServo.attach(6); //attach servo to proper pins
HeadServo.write(90); //centre head servo
}
void loop()
{
int distanceFwd = (ultrasonic.Ranging(CM));
Serial.begin( 9600 );
Serial.print( ultrasonic.Ranging(CM) );
Serial.println( "cm" );
delay(500);
if (distanceFwd>dangerThresh) //if path is clear
{
LeftServo.write(LForward);
RightServo.write(RForward); //move forward
}
else //if path is blocked
{
LeftServo.write(LNeutral);
RightServo.write(RNeutral);
HeadServo.write(0);
delay(500);
rightDistance = ultrasonic.Ranging(CM); //scan to the right
delay(500);
HeadServo.write(180);
delay(700);
leftDistance = ultrasonic.Ranging(CM); //scan to the left
delay(500);
HeadServo.write(90); //return to center
delay(100);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
LeftServo.write(LBackward);
RightServo.write(RForward); //turn left
delay(2000);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
LeftServo.write(LForward);
RightServo.write(RBackward); //turn right
delay(2000);
}
else //if they are equally obstructed
{
LeftServo.write(LForward);
RightServo.write(RBackward); //turn 180 degrees
delay(2000);
}
}
long ping()
{
// Send out PING))) signal pulse
pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(TrigPin, LOW);
//Get duration it takes to receive echo
pinMode(TrigPin, INPUT);
duration = pulseIn(TrigPin, HIGH);
//Convert duration into distance
return duration / 29 / 2;
}
What is your power consumption? Measure it with a multimeter on usb and compare to on a 9v, if its too high the onboard regulator of the uno will not be able to provide enough power
tmcraig:
all my code is wrote(using what you linked), it works when on usb just not via a brandnew 9v battery.
9V batteries are not designed for high current use. Running any kind of motor off of them is going to result in problems. You need to get a real source of current like a LiPo or a bank of 6 AA's.
thanks for the responce guys. ill buy a multimeter over the weekend to test.
and 6 pack of AA's.
as i said totally new to electronics, so it confused me when usb powered the arduino and the 3 servos and ping sensor. any tutorials on basics on working out power needed