code works with arduino connected with cable to the pc, but not with battery

Hi, I have a very strange problem, my robot I built doesn't do what the code says when it is powered by batteries (9v and 1.5v ). checked the values and all the batteries are new. no large loads my 1sheeld doesnt even send messages when powered by the batteries. but all works when connected by USB. has something to do with impedances? I dont know.. heres the code:

#define trigPin1 9
#define echoPin1 8
#define trigPin2 10
#define echoPin2 11
#define pwmOutput 6
#define Vtest A1
#define CUSTOM_SETTINGS
#define INCLUDE_NOTIFICATION_SHIELD
#define INCLUDE_SLIDER_SHIELD
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_MIC_SHIELD
float duration, distance1,distance2;
#include "OneSheeld.h"
int value;
void setup() {
// put your setup code here, to run once:
OneSheeld.begin();
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(pwmOutput,OUTPUT);
// test
Terminal.println("Starting robot test");
delay(3000);
Terminal.println("please turn L298 power on then verify no warning is shown ");
delay(5000);
while(analogRead(A1)<100){
Terminal.println("L298 power low");
Terminal.println(analogRead(A1));
delay(2000);
}
Terminal.println("moving test: robot will move foreward and backwards");
delay(5000);
digitalWrite(pwmOutput, HIGH);
delay(1000);
digitalWrite(pwmOutput, LOW);
delay(1000);
digitalWrite(pwmOutput, HIGH);
delay(2000);
digitalWrite(pwmOutput, LOW);
delay(1000);
//
Terminal.println("testing IR sensor");
delay(3000);
Terminal.println("please place an object in front of the sensor and check for correct readings");
while(analogRead(0)>200);
Terminal.println("IR sensor OK");

}

void loop() {
// put your main code here, to run repeatedly:
// sonar 1
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
duration=pulseIn(echoPin1, HIGH);
distance1=(duration/2)*0.0343;
Terminal.print("Distance1=");
if(distance1>=400||distance1<=2){
Terminal.println("out of range");
analogWrite(pwmOutput,0);
delay(3000);
OneSheeld.delay(1000);

}
else{
Terminal.print(distance1);
Terminal.println(" cm");
}

// sonar 2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
duration=pulseIn(echoPin1, HIGH);
distance2=(duration/2)*0.0343;
Terminal.print("Distance2=");
if(distance2>=400||distance2<=2){
Terminal.println("out of range");
analogWrite(pwmOutput,0);
delay(3000);
OneSheeld.delay(1000);

}
else{
Terminal.print(distance2);
Terminal.println(" cm");
}
if(distance1>11||distance1<9||distance2>11||distance2<9){
Notification.notifyPhone("crack");
analogWrite(pwmOutput,0);
delay(3000);
OneSheeld.delay(1000);

}
value = Slider.getValue();
analogWrite(pwmOutput,value);
while(analogRead(0)<200){
analogWrite(pwmOutput,0);
Notification.notifyPhone("Obstacle");
OneSheeld.delay(1000);
}
}

Not much point in posting the code, you say it works, so it is probably time to admit that you have made a bad choice of battery, thereby incurring a fatal shortage of power. My guess is you are using a 9v PP3 which has virtually no power at all.

Are the 9V batteries the rectangular smoke alarm batteries? If so, it is no wonder that your robot will not work. Those batteries will not supply the current necessary to power motors.

Read the how to use this forum-please read sticky to see how to post code and some good advice on how to get the most from the forum.

Before posting code use the auto format tool in the IDE (ctrl-t or Tools, Auto Format) to indent your code to make it easier to follow.

actually i just added a capacitor to the batteries and now it works.. yes those are the rectangular 9v batteries..