Line follower - Only 2 cases

Hello! I am participating in an robotics contest with a line follower. The robot is programed a bit different since the driver is programed with serial. They provided a basic code for the robot but it only has 2 cases. In all other sites that show how to program an line follower robot they have 4 cases. Because of the 2 cases even when both sensors are on white he keeps turning left and right. I would really apriciate it if someone could add a 3rd case to my code for when both sensors are on white. Thanks!

volatile byte beggining;

volatile byte forward;

volatile byte backwards;

volatile int stare;

volatile int sensorLeft;

volatile int sensorRight;



void setup(){

  beggining = 254;

  forward = 240;

  backwards = 186;

  stare = 0;

  sensorLeft = 0;

  sensorRight = 0;

  pinMode(3, INPUT);

  pinMode(9, INPUT);

  Serial.begin(9600);

}



void loop(){

  sensorLeft = digitalRead(3);

  sensorRight = digitalRead(9);

  if (sensorLeft == 1) {

    if (sensorRight == 1) {

      state = 0;



    } else {

      state = 1;



    }



  } else if (sensorRight == 1) {

    state = -1;

  }

  switch (state) {

   case -1:

    Serial.write(beggining);

    Serial.write(1);

    Serial.write(forward);

    Serial.write(50);

    Serial.write(beggining);

    Serial.write(2);

    Serial.write(forward);

    Serial.write(0);

    break;

   case 1:

    Serial.write(beggining);

    Serial.write(1);

    Serial.write(forward);

    Serial.write(0);

    Serial.write(beggining);

    Serial.write(2);

    Serial.write(forward);

    Serial.write(50);

    break;

   default:

    Serial.write(beggining);

    Serial.write(1);

    Serial.write(forward);

    Serial.write(50);

    Serial.write(beggining);

    Serial.write(2);

    Serial.write(forward);

    Serial.write(50);

    break;

  }



}

Thanks again!

You have far too much white space in your program - it makes it very hard to read.

The following is how I would do it. I convert the left and right sensor values into a single number which might be 10, 11 or 1 depending on which sensors are detected.

I have also made the states characters to make the code clearer - no need to remember what 1 or -1 means.

void loop(){
  sensorLeft = digitalRead(3);
  sensorRight = digitalRead(9);
  sensorsCombined = sensorLeft * 10 + sensorRight;
  if (sensorsCombined == 11) {
    state == 'S'; // S for straight, L for left, R for Right
  }
  else if (sensorsCombined == 10) {
    state = 'L';
  }
  else if (sensorsCombined == 1) {
    state = 'R'
  }
  switch (state) {
   case 'S':
    Serial.write(beggining);
    Serial.write(1);
    Serial.write(forward);
    Serial.write(50);
    Serial.write(beggining);
    Serial.write(2);
    Serial.write(forward);
    Serial.write(0);
    break;
   case 'L':
    Serial.write(beggining);
    Serial.write(1);
    Serial.write(forward);
    Serial.write(0);
    Serial.write(beggining);
    Serial.write(2);
    Serial.write(forward);
    Serial.write(50);
    break;
   case 'R':
     // need to put appropriate code here
   default:
    Serial.write(beggining);
    Serial.write(1);
    Serial.write(forward);
    Serial.write(50);
    Serial.write(beggining);
    Serial.write(2);
    Serial.write(forward);
    Serial.write(50);
    break;
  }

}

...R

Thanks