Hey everyone,
I created a maze solving robot (which works), and i am adding a bluetooth RC car functionality (which works) as well. I have coded and tested these functions independently and they work . The goal is to power on, have it in a static state waiting for a command via bluetooth app from my cell. So it will await for a direction (RC car), or wait for the maze start button (begins the maze solving sequence). Once the maze solving sequence starts it goes through its loops solving the maze. Originally the maze solving sequence had no bluetooth interface and it began immediately on power up. So now with a bluetooth interface the maze solving sequence does not function correctly. On power up the robot waits for BT connection. Once connection is established it waits for commands. If i just use the RC car portion it works as expected. When i use the Maze solving portion (via the app), it does not solve the maze, it just spins in circles to the left. The app being used is called "roboremo." If someone could help me figure out why it is not working that would be greatly appreciated.
All the code is not attached. It exceed character limit in post. I posted the beginning portion which i believe is where the problem lies.
PLEASE DON'T JUDGE THE POORLY WRITTEN CODE FORMAT, I AM NEW TO THIS.
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of BT module to Arduino pin2
int bluetoothRx = 3; // RX-I pin of B module to Arduino pin3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
//sensor readings
int leftCenterReading;
int leftNearReading;
int leftFarReading;
int rightCenterReading;
int rightNearReading;
int rightFarReading;
int leftNudge;
int replaystage;
int rightNudge;
#define leapTime 200
#define led 13
//sensor definitions
int leftCenterSensor = 3;
int leftNearSensor = 4;
int leftFarSensor = 5;
int rightCenterSensor = 2;
int rightNearSensor = 1;
int rightFarSensor = 0;
//motor definitions
int leftMotor1 = 7;
int leftMotor2 = 6;
int rightMotor1 = 5;
int rightMotor2 = 8;
char cmd[100];
int cmdIndex;
char path[30] = {};
int pathLength;
int readLength;
void setup() { // at power on
delay(500); // wait for bluetooth module to start
pinMode(leftMotor1, OUTPUT);
digitalWrite(leftMotor1, LOW);
pinMode(leftMotor2, OUTPUT);
digitalWrite(leftMotor2, LOW);
pinMode(rightMotor1, OUTPUT);
digitalWrite(rightMotor1, LOW);
pinMode(rightMotor2, OUTPUT);
digitalWrite(rightMotor2, LOW);
digitalWrite(led, HIGH);
delay(1000);
// change Bluetooth module baud rate to 9600:
bluetooth.begin(115200); // Bluetooth default baud is 115200
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$"); // enter cmd mode
delay(250);
bluetooth.println("U,9600,N"); // change baud to 9600
bluetooth.begin(9600);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
if(c=='\n') { // each command ends with '\n'
exeCmd();
cmdIndex = 0;
}
}
}
void exeCmd() {
if(cmd[0]=='f') { // forward
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
if(cmd[0]=='q') { // reverse
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, HIGH);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
}
if(cmd[0]=='h') { // stop
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}
if(cmd[0]=='c') { // left
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, HIGH);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
if(cmd[0]=='y') { // right
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
}
if(cmd[0]=='m') { // solve maze
maze();
}
}
void maze(){
pinMode(leftCenterSensor, INPUT);
pinMode(leftNearSensor, INPUT);
pinMode(leftFarSensor, INPUT);
pinMode(rightCenterSensor, INPUT);
pinMode(rightNearSensor, INPUT);
pinMode(rightFarSensor, INPUT);
readSensors();
if(leftFarReading<200 && rightFarReading<200 &&
(leftCenterReading>200 || rightCenterReading>200) ){
straight();
}
else{
leftHandWall();
}
}
void readSensors(){
leftCenterReading = analogRead(leftCenterSensor);
leftNearReading = analogRead(leftNearSensor);
leftFarReading = analogRead(leftFarSensor);
rightCenterReading = analogRead(rightCenterSensor);
rightNearReading = analogRead(rightNearSensor);
rightFarReading = analogRead(rightFarSensor);
void leftHandWall(){
if( leftFarReading>200 && rightFarReading>200){
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
delay(leapTime);
readSensors();
if(leftFarReading>200 || rightFarReading>200){
done();
}
if(leftFarReading<200 && rightFarReading<200){
turnLeft();
}
}
Kind Regards,
Shaz