my first self balance robot... I have to robot put together and working.
I tried to put in a dead spot(stop the motors) at the balance point but it is not working
here is my code. please help of advise
//for this project, I have had partial succes with a self balance robot. I used the
//"cheap" tumbler rc robot gear boxes, arduino uno rev 3, 2 small proto boards,
//I am also using the parallax dual axis accelerometer from radio shack.
//this version uses cut pieces of code from the seeed grove sample code and also
//cut up sample code from parallax
//you can contact me if you need at stevecobb76@gmail.com
//this is the first version of the code for this bot and does not work as of yet.
//it is just my starting point.
//version 0.1 beta testing phase
//
//notes, robot seems jumpy, and dead spot or (balance point) is not working in this code.
//might need servo motors or faster gear motors instead of tumbler rc motor/gear boxs
// p.s. grove motor shield sucks hard... they block all pins...
const int xPin = 6; // X input from memsic dual axis accelerometermx2125
const int yPin = 5; // Y input accelerometer
int pinI1=8;//define I1 interface //code cut from grove motor shield sample
int pinI2=11;//define I2 interface //code cut from grove motor shield sample
int speedpinA=9;//enable motor A //code cut from grove motor shield sample
int pinI3=12;//define I3 interface //code cut from grove motor shield sample
int pinI4=13;//define I4 interface //code cut from grove motor shield sample
int speedpinB=10;//enable motor B //code cut from grove motor shield sample
int spead =255;//define the spead of motor //code cut from grove motor shield sample
int xPulse, yPulse; // read from accelerometer code cut from grove motor shield sample
int xVal, yVal; // converted value code cut from grove motor shield sample
void setup() {
Serial.begin(9600); //just so I can try to see what is going on...
pinMode(xPin, INPUT); //code cut from grove motor shield sample
pinMode(yPin, INPUT); //code cut from grove motor shield sample
pinMode(pinI1,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI2,OUTPUT); //code cut from grove motor shield sample
pinMode(speedpinA,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI3,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI4,OUTPUT); //code cut from grove motor shield sample
pinMode(speedpinB,OUTPUT); //code cut from grove motor shield sample
}
void forward() { //code cut from grove motor shield sample
analogWrite(speedpinA,spead);// I dont get this, but ok...
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
delay(500); // trying to smooth out response/reaction time
}
void backward() //code cut from grove motor shield sample
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
delay(500);
}
void loop()
{
// read x
xPulse = pulseIn(xPin,HIGH);
xVal = ((xPulse / 10) - 500) * 8;
Serial.print("x axes =");
Serial.print("\t");
Serial.print(xVal); // print to serial xval
Serial.println();
//______________________________________________________________
// this is where I need help!!!!
if (xVal > 50)//trying to create dead zone if xVal = 2 to 49 from acceleratometer.. did not work
{
backward();
}
if(xVal < 1)
{
forward();
}
}