Hi all!
I'm trying to make an Ir line following maze solving robot. I don't have the hardware yet, so I've been messing around with the code and theory. The way I see this working is my robot will record the moves it took in a string. L for a left turn R for a right one S for straight through a turn and T for a turn around at a dead end. After it finds the end it will shorten the moves in the "Map" string so Left, Turn Around, Left is the same as just going straight and do some other shortening substitutions.
I haven't figured out how to read the Map string so the robot can follow the shortened instructions again. I looked it up and it seems like serial.read might work, but I don't understand the syntax. I also read in some forums that strings are RAM hogs and chars might be better for my situation. What's your guy's thoughts?
PS That stuff I commented out was me messing around with strings using serial commands.
#define turnTime 200
String L, R, S, T, Map;
bool On = 0;
char Command;
int rightMotor;
int leftMotor;
int irOne;
int irTwo;
int irThree;
int irFour;
int irFive;
void setup() {
Serial.begin(9600);
Map = String ();
L = String ("L");
R = String ("R");
S = String ("S");
T = String ("T");
}
void loop() {
// if (Serial.available() > 0) {
// Command = toupper(Serial.read());
// switch (Command) {
// case 'G':
// On = 1;
// break;
// }
// }
//
// if (On == 1) {
// Map += L;
// Serial.println(Map);
// delay (1000);
// On = 0;
// Map += L;
// Serial.println(Map);
// delay (1000);
// On = 0;
// Map += L;
// Serial.println(Map);
// delay (1000);
// On = 0;
//}
}
void shortenPath() {
Map.replace("LTL", "S");
Map.replace("RTR", "S");
Map.replace("LTR", "T");
Map.replace("RTL", "T");
Map.replace("LTS", "R");
Map.replace("STL", "R");
Map.replace("SBS", "B");
Map.replace("LTL", "B");
}
void turnLeft() {
Map += L;
digitalWrite( rightMotor, HIGH);
digitalWrite( leftMotor , LOW);
delay(turnTime);
}
void turnRight() {
Map += R;
digitalWrite( rightMotor, HIGH);
digitalWrite( leftMotor , LOW);
delay(turnTime);
}
void turnAround() {
Map += T;
digitalWrite( rightMotor, HIGH);
digitalWrite( leftMotor , LOW);
delay(turnTime * 2);
}
void straight (){
Map += S;
digitalWrite( rightMotor, HIGH);
digitalWrite( leftMotor , HIGH);
}