Line Follow Robot start at set times and finish on cross line

I am building my line follower robot and have had lots of help from this forum. I am a complete newbie so learning as I go.

I am currently trying to set the loop to start at specific times read from the RTC.

I have not yet put any code in for this as dont know where to start and all information I find I cant decipher to fit in my code.

On another note I can only get the robot to stop on a line if I put it as my first "if" statement, otherwise it doesnt stop on line anywhere else. I think this will cause issues when setting up the timer start function too. As what I want to achieve is to move off the line at set times and start its line following loop then when returning to the line stay there until the next time set.

/*
***Combined code with Line Follow***
***RTC Clock Set***
***LCD Display***
***& LED Lights***
*/


// **********  MAKE SURE YOU SET CURRENT TIME AND DATE WHEN UPLOADING CODE  **********



//Line Follow Include
#include <DFMobile.h>
DFMobile Robot(4, 5, 7, 6);
int RightValue;   //Right line tractor sensor on Pin 8
int MiddleValue;  //Middle line tractor sensor on Pin 9
int LeftValue;    //Left line tractor sensor on Pin 10

//RTC Include
#include "DFRobot_DS323X.h"
DFRobot_DS323X rtc;
#include <DS3231.h>

//LCD Include
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

//Line Follow void setup
void setup() {
  Robot.Direction(LOW, HIGH);
  Serial.begin(115200);
  Robot.Speed(120, 120); //move off start and end line to start line follow
  millis();

  //RTC void setup

   Serial.begin(115200);
   Wire.begin();
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        millis();
    }
    /*!
     *@brief Set time output format
     *@param eHours_t:e24hours, e12hours. default is e24hours
     */
    rtc.setHourSystem(rtc.e24hours);
    //rtc.setTime(/*year,1900-2100*/2023, /*month,1-12*/7, /*date,1-31*/26, /*hour,0-23*/11,/*minute,0-59*/20,/*second,0-59*/30);//Set Set initial time .
  

  //LCD void setup
  lcd.init();                      // initialize the lcd


  
}


void loop() {

   //RTC void loop
    Serial.print(rtc.getYear(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);//second
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    millis();

   

    // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(4, 0); // Set Cursor, Top Line
  lcd.print("ROBO TED"); // Message
  lcd.setCursor(1, 1); //set cursor Bottom line
  lcd.print(rtc.getDayOfWeek());
  lcd.print(" ");
  lcd.print(rtc.getDate());
  lcd.print(" ");
  lcd.print(rtc.getHour());
  lcd.print(":");
  lcd.print(rtc.getMinute());

   //Line Follow Void Loop
    //reading 3 pins values of Line Tracking Sensor
  RightValue = digitalRead(8);
  MiddleValue = digitalRead(9);
  LeftValue = digitalRead(10);

  //print out the value you read:
  Serial.print(LeftValue);
  Serial.print(MiddleValue);
  Serial.println(RightValue);
  millis();




 
  //check if the robot is located in the middle of line
  // if it is, the robot go straight.
  if (MiddleValue == LOW) {  //line in the middle
    Robot.Speed(120, 120);     //go straight
    millis();
  
  }

  //check if the robot is located in the left of line
  // if it is, the robot turn left.
  if ((LeftValue == LOW)) {  //line on left
    Robot.Speed(10, 240);                                 //turn left
    millis();
  }

  //check if the robot is located in the right of line
  // if it is, the robot turn right.
  if ((RightValue == LOW)) {  //line on the right
    Robot.Speed(240, 10);                                 //turn right
    millis();
  }

  //Line follow if statement stop on the line start and end point
   if ((LeftValue == LOW) && (MiddleValue == LOW) && (RightValue==LOW)) {
      Robot.Speed(0, 0);  
      millis();
      
  }
}


 



Thanks in advance

You keep calling the millis function, but never assign the value returned to a variable.
Why?

1 Like

Did you forget the "if" here?

1 Like

yes thats where ive been playing around with the stop on line function.

have added that back

Apoligies uploaded a sketch I was editing.

The Stop on line only works if I do it as followed



void loop() {

   //RTC void loop
    Serial.print(rtc.getYear(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);//second
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    millis();

   

    // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(4, 0); // Set Cursor, Top Line
  lcd.print("ROBO TED"); // Message
  lcd.setCursor(1, 1); //set cursor Bottom line
  lcd.print(rtc.getDayOfWeek());
  lcd.print(" ");
  lcd.print(rtc.getDate());
  lcd.print(" ");
  lcd.print(rtc.getHour());
  lcd.print(":");
  lcd.print(rtc.getMinute());

   //Line Follow Void Loop
    //reading 3 pins values of Line Tracking Sensor
  RightValue = digitalRead(8);
  MiddleValue = digitalRead(9);
  LeftValue = digitalRead(10);

  //print out the value you read:
  Serial.print(LeftValue);
  Serial.print(MiddleValue);
  Serial.println(RightValue);
  millis();


 //Line follow if statement stop on the line start and end point
  if ((LeftValue == LOW) && (MiddleValue == LOW) && (RightValue==LOW)) {
      Robot.Speed(0, 0);  
      millis();
      
  }
 
 //check if the robot is located in the middle of line
  // if it is, the robot go straight.
 else if (MiddleValue == LOW) {  //line in the middle
    Robot.Speed(120, 120);     //go straight
    millis();
  
  }

  //check if the robot is located in the left of line
  // if it is, the robot turn left.
  else if ((LeftValue == LOW)) {  //line on left
    Robot.Speed(10, 240);                                 //turn left
    millis();
  }

  //check if the robot is located in the right of line
  // if it is, the robot turn right.
  else if ((RightValue == LOW)) {  //line on the right
    Robot.Speed(240, 10);                                 //turn right
    millis();
  }

 
}

Don’t suppose you have any suggestions to add the stop and start on the line at set time do you?

Thanks in advance

Do you have any explanations for all those calls to millis that don't do anything with the return value?
I'm trying to understand your thought-processes.

I am a complete newbie learning as I go, I took suggested feedback added millis() with no value and it resolved my issue so had no reason to change that.

No, it didn't solve anything.
It's likely the compiler ignores them.

1 Like

okay so will add a value in to those then.

Excuse my ignorance like I said I am complete beginner and looking to learn as I go.

But what are they there for in the first place?

I assumed you had to have some sort of delay in the functions :confused:

The only delay a call to millis introduces is of the order of a few microseconds, trivial in the context of motors.

That's why I'm trying to get an insight into your thought processes.

duplicate post
https://forum.arduino.cc/t/rtc-ds3231-breaks-robot-line-follow-code/1151617

Im asking a different question to the last post, however the same issue has popped up in this one too.

Well I finally got there! Robot now ignores the LEFT and RIGHT value LOW at set times and continues off the diagonal line. Then once that time has passed it will stop on the diagonal line again until next time. Have took advice from people tidied up my code a little and even added the LED behaviour to it too. Here is how I achieved it for anyone looking at this Topic in future.

//Line Follow Loop
  DateTime now = rtc.now();
  int currentHour = now.hour();
  int currentMinute = now.minute();

  RightValue = digitalRead(8);
  MiddleValue = digitalRead(9);
  LeftValue = digitalRead(10);

  if ((currentHour == 14 && currentMinute == 00) || (currentHour == 15 && currentMinute == 00)) {
    shouldFollowLine = true;
  } else {
    shouldFollowLine = false;
  }

  //reading 3 pins values of Line Tracking Sensor
  RightValue = digitalRead(8);
  MiddleValue = digitalRead(9);
  LeftValue = digitalRead(10);

  //print out the value you read:
  Serial.print(LeftValue);
  Serial.print(MiddleValue);
  Serial.println(RightValue);
  //millis();

  // Check if the robot stops at the diagonal line
    if ((LeftValue == LOW) && (RightValue == LOW)) {
        if ((currentHour == 14 && currentMinute == 00) || (currentHour == 15 && currentMinute == 00)) {
            Robot.Speed(120, 120); // Continue forward at set time
            shouldStopLEDs = false; // LEDs should keep flashing
        } else {
            Robot.Speed(0, 0);     // Stop on the diagonal line
            shouldStopLEDs = true; // LEDs should stop flashing
            for (int i = 0; i < NUMPIXELS; i++) {
                pixels.setPixelColor(i, 0); // Turn off LEDs
            }
            pixels.show();
        }
    } else {
        // Check if the robot is located in the middle of the line
        if ((MiddleValue == LOW)) {
            Robot.Speed(120, 120); // Go straight
        }
        // Check if the robot is located on the left side of the line
        else if ((LeftValue == LOW)) {
            Robot.Speed(10, 240);  // Turn left
        }
        // Check if the robot is located on the right side of the line
        else if ((RightValue == LOW)) {
            Robot.Speed(240, 10);  // Turn right
        }
        // Flash LEDs if allowed
        if (!shouldStopLEDs) {
            unsigned long currentLEDMillis = millis();
            if (currentLEDMillis - previousLEDMillis >= intervalLED) {
                previousLEDMillis = currentLEDMillis;

                for (int i = 0; i < NUMPIXELS; i++) {
                    int randomColorIndex = random(NUM_COLORS); // Choose a random color index
                    uint32_t randomLEDColor = colorArray[randomColorIndex];
                    pixels.setPixelColor(i, randomLEDColor);
                }
                pixels.show();
            }
        } else {
            for (int i = 0; i < NUMPIXELS; i++) {
                pixels.setPixelColor(i, 0); // Turn off LEDs
            }
            pixels.show();
        }
    }
  

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.