Reading a String

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);
}

Strings do take up a lot of memory and the way to concatenate them together in your code probably makes them even worse. When they run out of space, they have to "grow" which means they may have to allocate en entire new chunk of memory which will eventually fail since these chips does have very much. That is why lots of people recommend learning how to deal with C strings (arrays of char with a NUL char at the end)

As for your code, a couple of things. All of your turn functions turn on the motors, but they never turn them off. You should probably do that or put that code in the main loop.

The way you have it currently, an 'L' just turns left but does not advance so an LTL is equivalent to no move, not a move one square ahead.

Somewhat a repeat of what blh64 answered while I wrote it, but there is some different information so I'll post it anyway:

Watson221:
I looked it up and it seems like serial.read might work, but I don't understand the syntax.

What about the syntax don't you understand? Have you read the documentation:

Your commented Serial.read() code looks fine to me.

Watson221:
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?

There are two different things with confusingly similar names: Strings and strings. Strings are indeed memory hogs. Worse, they can cause memory fragmentation that will make your program unreliable. I recommend against ever using String in Arduino code. strings are null terminated char arrays. This is what you should use.

The Serial.read part that I'm confused on is that according to the forums it could be used to read a string one bit at a time. I couldn't get the desired effect of read L from the shortened map and create a response.

Could you give me an example of string code in my use case? I wasn't even aware there was a difference! I wouldn't want to fragment my Arduino!

Thanks again this is helping a ton!