I've built a self-balancing unicycle, and for the most part, the project is going great. If I boot the Arduino with USB from the computer the sketch works properly and sends serial commands (through serial Tx pin) to my motor driver. If I attempt to start the Arduino with a 9v battery (DC power jack), it gets power but serial communication doesn't seem to be working and the unicycle stays motionless. If I start with USB to get it working, then remove the USB while 9v power is on, it will continue to work until I cut power to the Arduino and attempt to start it up again. I have researched the issue and found no one with the same issue. Can anyone explain whats going on, or give me some kind of alternative
Thanks, in advance
Here is my code, if that helps:
//code for homemade self balancing unicycle:
//sparkfun 6dof IMU
//PID output control
//Dimension Engineering SyRen 25 motor driver
//Arduino IDE 1.0
#include <FreeSixIMU.h> //fusion filtering library
#include <FIMU_ADXL345.h> //accelerometer library
#include <FIMU_ITG3200.h> //gyro library
#include <PID_v1.h> //PID control library
#include <Wire.h>
//timing
int STD_LOOP_TIME = 9;
int lastLoopTime = STD_LOOP_TIME;
int lastLoopUsefulTime = STD_LOOP_TIME;
unsigned long loopStartTime = 0;
//for IMU:
float angles[3]; //yaw pitch roll (only pitch is necessary for one-axis balance in this application)
FreeSixIMU sixDOF = FreeSixIMU(); //set the FreeSixIMU object
//for PID controller:
double BalancePoint; //angle that unicycle attempts to balance at
double PID_Output;
double Pitch;
PID myPID(&Pitch, &PID_Output, &BalancePoint,1,0,0, DIRECT); // numbers are (respectively): Kp,Ki,Kd
//for SyRen
int SyRen_Output; //final motor output value
int Int_Output; //used to convert pid output to int
int Direction;
void setup()
{
Serial.begin(2400); //the SyRen is completely non-responsive if a higher baudrate is chosen
Wire.begin();
delay(5);
sixDOF.init(); //begin the IMU
delay(5);
//initialize PID variables
Pitch = angles[1];
BalancePoint = 2; //this should be set to what pitch angle reads when unicyle is balanced
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(-127,127);
Serial.write(170);
}
void loop()
{
//reads angles from IMU then plugs pitch into the PID controller
sixDOF.getEuler(angles);
Pitch = (double) angles[1];
myPID.Compute();
Int_Output = (int) PID_Output; //converts to int for SyRen controller
//when output is negative, this code reverses the direction of the motor
SyRen_Output = abs(Int_Output);
if (angles[1] > BalancePoint && Direction == 1) {
Direction = 0;
}
if (angles[1] < BalancePoint && Direction == 0) {
Direction = 1;
}
//packetized serial control:
Serial.write(128);
Serial.write(Direction);
Serial.write(SyRen_Output);
Serial.write((128+Direction+SyRen_Output) & 0b01111111);
//Serial.print(SyRen_Output); //for debugging
//loop timing control
lastLoopUsefulTime = millis()-loopStartTime;
if(lastLoopUsefulTime<STD_LOOP_TIME) delay(STD_LOOP_TIME-lastLoopUsefulTime);
lastLoopTime = millis() - loopStartTime;
loopStartTime = millis();
}