MBOT Line Follow Sharp Turns

I am trying to make my MBOT follow a straight line that has two 90 degree turns in it. Here is my current code that isn't working...any help is appreciated!

// Motor Speed
uint8_t motorSpeed = 50;
uint8_t turnSpeed = 30;

void setup() 
{
  // Communication open.
  Serial.begin(9600);
}

void loop() 
{
    lineFollow();
}

void lineFollow()
{
    int sensorState = lineFinder.readSensors();
  
    switch(sensorState)
    {
      // Both sensors are on the line
      case S1_IN_S2_IN:
         // Reverse
         motorRight.run(motorSpeed);
         motorLeft.run(-motorSpeed);
         delay(500);
         
         break;
      
      // Left sensor on line, right sensor off line
      case S1_IN_S2_OUT:
          // Turn left
          motorRight.run(turnSpeed);
          motorLeft.run(turnSpeed);
          delay(500);
          
          break;
          
      // Left sensor off line, right sensor on line
      case S1_OUT_S2_IN:
          // Turn right
          motorRight.run(-turnSpeed);
          motorLeft.run(-turnSpeed);
          delay(500);
          
          break;
          
      // Both sensors off the line
      case S1_OUT_S2_OUT:
          // Forward
          motorRight.run(-motorSpeed);
          motorLeft.run(motorSpeed);
          delay(500);
  
          break;
          
     default: break; 
    }   
  }

Because you use delay(500) in several places, your robot will do whatever it is doing for an entire half second without considering any changes (like 90 degree turns). delay(...) prevents you from doing (almost) anything else while the delay(...) is happening. You need to embrace using a state machine and millis(). This will help you to read the sensors frequently.

The electromechanics of the robot also play a role in determining how sharp a turn can be followed.

You have not explained anything about the electromechanics of the robot, and you have not shown anything about lineFinder.readSensors(). I cannot help further.

So I should remove the delays completely? The sensorstate is related to the switch statement (right sensor inside line-left sensor inside line, etc). It is a MakeBlock Ultimate 2.0 and here is the rest of the code that I missed:

#include "MeMegaPi.h"     

// Right
MeMegaPiDCMotor motorRight(PORT1B);

// Left
MeMegaPiDCMotor motorLeft(PORT2B);

// Ultrasonic Sensor
MeUltrasonicSensor ultraSensor(PORT_6);

// LED Light
MeRGBLed led(PORT_7); 

// Line Follower Module
MeLineFollower lineFinder(PORT_8);

kickskid21:
So I should remove the delays completely?

Yes, if you want a responsive program.

Have a look at how millis() is used to manage timing without blocking in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

PS... For the future always post a complete program in one piece.

You should remove the delay(...) calls completely and use a millis() approach, but this is not as simple as just removing the delay_s and just replacing them with millis.

Use the suggestions by Robin2.

I’m working on a robot that makes 90 degrees turns. It uses a state machine and timing to make the turn. I use a six sensor array. Check it out on git Technology-at-school/Robots/Makeblock/develop/project_mBot at master · Tauvic/Technology-at-school · GitHub