Program works only with USB and not direct connection

Hi everyone i’m new to arduino, I’ve dumped this program to my Arduino and then removed the USB connection and then used a 7.5 V adapter to power the board , but it seems that the program doesnt work when i power through adapter or even using USB adapter(not the system connection)
Program works only when board is connected to system, not even if the USB is connected to an USB adapter or a 7.5V adapter to pin

This is the program:

#include <Servo.h>
Servo frontServo;
Servo rearServo;

/* Servo motors - global variables */

int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;

/* Ping distance measurement - global variables */

int pingPin = 4;
long int duration, distanceInches;
long distanceFront=0; //cm
int startAvoidanceDistance=20; //cm
long microsecondsToInches(long microseconds)
{
return microseconds / 148 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 58 / 2;
}
long distanceCm(){
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
distanceInches = microsecondsToInches(duration);
return microsecondsToCentimeters(duration);
}
void center()
{
frontServo.write(centerPos);
rearServo.write(centerPos);
}
void moveForward()
{
frontServo.write(frontRightUp);
rearServo.write(backLeftForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
frontServo.write(frontLeftUp);
rearServo.write(backRightForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveBackRight()
{
frontServo.write(frontRightUp);
rearServo.write(backRightForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos-6);
delay(65);
frontServo.write(frontLeftUp+9);
rearServo.write(backLeftForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveTurnLeft()
{
frontServo.write(frontTurnRightUp);
rearServo.write(backTurnLeftForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
frontServo.write(frontTurnLeftUp);
rearServo.write(backTurnRightForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
}
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
pinMode(pingPin, OUTPUT);
}
void loop()
{
distanceFront=distanceCm();
if (distanceFront > 1){ // Filters out any stray 0.00 error readings 7
if (distanceFront<startAvoidanceDistance) {
for(int i=0; i<=8; i++) {
moveBackRight();
delay(walkSpeed);
}
for(int i=0; i<=10; i++) {
moveTurnLeft();
delay(walkSpeed);
}
} else {
moveForward();
delay(walkSpeed);
}
}
}

Is your tab key broken?

Program works only when board is connected to system, not even if the USB is connected to an USB adapter or a 7.5V adapter to pin

So how can that be anything to do with your code.

You have a hardware issue with your external supplies.
Does an LED blink sketch work?
If so then it is an issue of current.
If not then you haven't connected it like you think you have or have a problem with the current capacity of your external supplies.

Yes, i've checked it, when i program the BLINK code in it and run using an external source it works, but not the program which i wanted to run. And again yes, when i connect two servos and one Ultrasonic sensor it gets disconnected every second from the system,still the bot moves, but when i use it using Adapter, it slightly turns and doesn't move at all.
My project is a Robot Insect.
Very similar to this project:
Imgur

What is the current rating of your power supply?

Yes you defiantly have a power supply issue:-

when i connect two servos and one Ultrasonic sensor it gets disconnected every second from the system,

That is supply a decoupling issue, the servos generate interference that resets the processor. You need to add some isolation to the servo feed like is described here:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

but when i use it using Adapter, it slightly turns and doesn't move at all.

The adapter can not supply enough current for you bot.

Thanks for the idea of De coupling, but i still have a doubt. If Adapter is not enough to power it, how do they use Batteries to power the bot, that too an ordinary 9v battery in that video above?

DibinDixit:
If Adapter is not enough to power it, how do they use Batteries to power the bot,

Because the batteries are able to provide more current than the adapter.

How much current does your power adapter provide?

Its output voltage is 7.5 v and current is 1A at max. But when i connect the board with batteries the board’s LED light glows very dim and it seems it needs more power…

Without knowing what and how you connected to the Arduino it is hard to tell what the problem might be. Looks like you drawing a lot of power of the Arduino to power something else!?

Its output voltage is 7.5 v and current is 1A at max.

That is an odd specification for an adaptor. Measure the voltage under load, that is when it is powering your system, I bet it drops below the 7V you need to power the regulator.
A 9V small square battery is useless in this situations as there is not enough current capacity.

Hi,

Read this

Then watch this

Then get the batteries.

If there is anything that you don't understand, I would be interested to know what it is, I think I have everything covered, but maybe not.

Duane B

rcarduino.blogspot.com

Thanks much Duane, It solved my problems :slight_smile: