Robot Code ONLY running when plugged into USB

I am working on a project for one of the classes in my senior year in college. The code is a start to what will be a “fire fighting robot.” Basically, the robot needs to navigate a maze, find a candle, and turn a fan on to put it out. As I said the code I am posting is a little rough so go easy on me. One of the requirements for the bot is that the code needs to start from hearing a 3800 Hz tone. Right now, the code only does this properly when plugged into the computer’s USB. If I unplug the USB and power the Arduino Uno externally with two rechargeable 3.7V batteries, the code does not function at all. If you plug it back in, the code works as planned. I am at a loss after many hours of trouble shooting.

#include <Servo.h>
#include <arduinoFFT.h>
#define IN1 8 //K1、K2 motor direction
#define IN2 9 //K1、K2 motor direction
#define IN3 10 //K3、K4 motor direction
#define IN4 12 //K3、K4 motor direction
#define ENA 5 // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB 6 // Needs to be a PWM pin to be able to control motor speed ENB
#define LED1 2 //left led connect to D2
#define LED2 3 //right led connect to D3
#define SERVO 11 //servo connect to D11
#define echo A3 // Ultrasonic Echo pin connect to A2
#define trig A2 // Ultrasonic Trig pin connect to A3
#define mictrigger A1 //Digital Microphone Trigger
#define buzzer 2 //buzzer connect to D2
#define RSPEED 255 //right motor speed
#define LSPEED 255 //left motor speed
#define flamepin 13 //flame detect pin 13
#define fanpinpos 3 //fan turn on pin 3
#define fanpinneg 7
#define SAMPLES 128 //SAMPLES-pt FFT. Must be a base 2 number. Max 128 for Arduino Uno.
#define SAMPLING_FREQUENCY 7600 //Ts = Based on Nyquist, must be 2 times the highest expected frequency.
#define mic_trigger A1

arduinoFFT FFT = arduinoFFT();

unsigned int samplingPeriod;
unsigned long microSeconds;

double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values

enum DN
{
START_AVOIDANCE,//start avoidance
STOP_AVOIDANCE,//stop avoidance
DEF
} Drive_Num = DEF;
int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;
const int distancelimit = 35; //distance limit for obstacles in front
const int sidedistancelimit = 18; //minimum distance in cm to obstacles at both sides (the car will allow a shorter distance sideways)
Servo head;

/motor control/
void go_ahead()//go ahead
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
//delay(t);
}
void go_back() //go back
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
//delay(t);
}
void go_stop() //stop
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void turn_left()//turn left
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
//delay(t);
}
void turn_right()//turn right
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
//delay(t);
}
/*set motor speed */
void set_motorspeed(int lspeed, int rspeed)
{
analogWrite(ENA, lspeed);
analogWrite(ENB, rspeed);
}
void buzz_on() //open buzzer
{
digitalWrite(buzzer, LOW);
}
void buzz_off() //close buzzer
{
digitalWrite(buzzer, HIGH);
}
void alarm() {
buzz_on();
delay(100);
buzz_off();
}
/control led/
void open_led(int led_num)
{
if (led_num == 1) digitalWrite(LED1, LOW);
else digitalWrite(LED2, LOW);
}
void close_led(char led_num)
{
if (led_num == 1) digitalWrite(LED1, HIGH);
else digitalWrite(LED2, HIGH);
}
/detection of ultrasonic distance/
int watch() {
long howfar;
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds(15);
digitalWrite(trig, LOW);
howfar = pulseIn(echo, HIGH);
howfar = howfar * 0.01657; //how far away is the object in cm
Serial.println((int)howfar);
return round(howfar);
}

void loop() {

{

for(int i=0; i<SAMPLES; i++)
{
microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script.
vReal = analogRead(mic_trigger); //Reads the value from analog pin 1 (A1), quantize it and save it as a real term.
_ vImag = 0; //Makes imaginary term 0 always_
* }*

_ /Perform FFT on samples/
* FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
_
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);*

_ /Find peak frequency and print peak/_
* double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);*

* while (peak >= 3500 & peak<=3800) {*
* auto_avoidance();
_
}_
_
boolean stat = digitalRead(flamepin);//read the value of D13*_
* if (stat == LOW)*
* turnonfan();*

* else*
* turnofffan();*
}
}
void turnonfan() {
* digitalWrite(fanpinpos, HIGH); //turn on the fan*
* digitalWrite(fanpinneg, LOW); // turn on the fan*
* go_stop();
_
}_
void turnofffan() {
_
digitalWrite(fanpinpos, LOW);_
_
digitalWrite(fanpinneg, LOW);_
_
}_
void auto_avoidance() {
_
head.write(90);_
_
delay(100);_
_
centerscanval = watch();_
_
if (centerscanval >= distancelimit) {_
set_motorspeed(LSPEED, RSPEED);
go_ahead();
_
}_
_
else {_
go_stop();
_
alarm();_
_
head.write(120);_
_
delay(150);_
_
ldiagonalscanval = watch();_
_
head.write(180);_
_
delay(150);_
_
leftscanval = watch();_
_
head.write(90);_
_
delay(150);_
_
head.write(60);_
_
delay(150);_
_
rdiagonalscanval = watch();_
_
head.write(0);_
_
delay(150);_
_
rightscanval = watch();_
_
head.write(90);_
_
if (ldiagonalscanval >= sidedistancelimit && leftscanval >= sidedistancelimit) {_
set_motorspeed(LSPEED, RSPEED);
go_back();
_
delay(400);_
turn_left();
_
delay(400);_
_
}_
_
else if (rdiagonalscanval >= sidedistancelimit && rightscanval >= sidedistancelimit) {_
set_motorspeed(LSPEED, RSPEED);
go_back();
_
delay(400);_
turn_right();
_
delay(400);_
_
}_
_
}_
_
}_
void setup() {
_ /L298N/_
_
pinMode(IN1, OUTPUT);_
_
pinMode(IN2, OUTPUT);_
_
pinMode(IN3, OUTPUT);_
_
pinMode(IN4, OUTPUT);_
_
pinMode(ENA, OUTPUT);_
_
pinMode(ENB, OUTPUT);_
_ /LED/_
_
pinMode(LED1, OUTPUT);_
_
pinMode(LED2, OUTPUT);_
close_led(1), close_led(2); //close led*

_ /init HC-SR04/
* pinMode(trig, OUTPUT);*
* pinMode(echo, INPUT);*
* digitalWrite(trig, LOW);*
/init buzzer/
* pinMode(buzzer, OUTPUT);*
* digitalWrite(buzzer, HIGH);*
* buzz_off();*
/init servo/
* head.attach(SERVO);*
* head.write(85);*
/init fan/
* pinMode(13, INPUT);*
* pinMode(3, OUTPUT);*
* pinMode(7, OUTPUT);*
/init microphone/_

samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
}
Robot_Avoidance_backup1.ino (6.13 KB)

And now without the italics please, but with a schematic.

Is that 2 x 3.7Vbatteries in series, so 7.4V? Connected where? Or what? Are the L298N driver and motors powered separately?

A circuit diagram would help but it sounds like you're falling foul of the fact that you can't get much current out of the UNO internal regulator. That is bypassed when you power through USB.

It's not very likely to be a problem with the code.

Steve

Sorry guys for the missing information, like I said, I’m new to this type of thing. I mostly wire things rather than program them. Yes it is 2x 3.7V batteries. I had run the code several times with external power before adding the frequency detection aspect of it and it and it worked just fine. The two battery’s put out 7.4V and I have an analog screen hooked up to monitor that. It never increases or decreases rapidly even when the code is trying to be ran. Those two batteries power the motor driver and the arduino Uno. Should I make an attempt to power them separately?

Yes. Tie the grounds, of the 2 separate power sources, together.

I forgot to mention that I am also using a ESP 13 shield.

So you seem to have a servo, a WiFi shield, a buzzer, 2 LEDs, an ultrasonic sensor, some sort of microphone and perhaps a fan as well as the motor driver and motors.

We really need to see a schematic/circuit/wiring diagram. Depending on how all that lot is powered I'm still guessing that you're overloading the Uno 5V pin. Without that information we're just wasting our time here.

Steve

I have confirmed that it is something in the code causing this issue. I took off the WIFI shield and wired to directly to the arduino uno. I also used a table top power supply to assure that both the arduino and the LM298 motor driver circuit are properly powered. After doing all this, the Arduino still had to be plugged in to make the code function. I also tried uploading a version of the code I saved before adding the frequency detection aspect to it and the code ran fine without being plugged into the USB port. Any ideas?

Any ideas?

Edit your post to add code tags, and POST A CLEAR SCHEMATIC DIAGRAM.

Read and follow the instructions in "How to use this forum". Those instructions mention to use code tags, and POST A WIRING DIAGRAM. Hand drawn is fine, as long as parts and pins are clearly labeled.

"Any ideas?"

An invalid assumption has been made somewhere. With all the "stuff" disconnected, can you get the "blink" example code to run when the USB cable is disconnected? Have you tried running your code with the power using hardware disconnected?

You have added a couple of large arrays involved in the FFT. Try temporarily changing SAMPLE from 128 down to 32 to see if that makes a difference. But there is nothing in that code I can see that should be affected by whether the USB is plugged in or not.

Can you try it with a USB cable with the power wires disconnected and the Arduino powered externally on the off-chance that it is the USB DATA connection that it needs. I'm grabbing at straws here but as you won't post a schematic or details of your components there's nothing else to look at.

Steve