RTC DS3231 breaks robot line follow code

Hi,

I have made a line following robot that works perfectly on its own. I also have my RTC DS3231 and LCD sketches that work perfectly on their own. When I combine them together the Line following part does not work as expected.

I am completely new to this and trying to learn as I go and have been using project examples from GitHub. It is almost as if it adds a massive delay to the line IR sensors. So it will see the line but not react quick enough. I have even tried bringing my delays right down

When I strip it back to just line follow and remove RTC and LCD code it works perfect again.

On another note when powering down board the RTC DS3231does not keep time maybe I need to initiallised battery. I have checked the battery tried another RTC Module and replaced battery. It works plugged in and serial port showing it keeping time but if you power down reverts back to last stored time, so not switching to battery.

Any help is appreciated here is my code.

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


// **********  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(9600);
  Robot.Speed(120, 120); //move off start and end line to start line follow
  delay(10);

  //RTC void setup

   Serial.begin(9600);
   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. ");
        delay(10);
    }
    /*!
     *@brief Set time output format
     *@param eHours_t:e24hours, e12hours. default is e24hours
     */
    rtc.setHourSystem(rtc.e24hours);
    rtc.setTime(/*year,1900-2100*/2023, /*mouth,1-12*/7, /*date,1-31*/25, /*hour,0-23*/15,/*minute,0-59*/23,/*second,0-59*/0);//Set Set initial time .
  

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


  
}


void loop() {

   //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);
  delay(10);


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


  //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
    delay(10);
  
  }

  //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(-120, 120);                                 //turn left
    delay(10);
  }

  //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(120, -120);                                 //turn right
    delay(10);
  }


  //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();
    delay(1000);

   

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


delay(1000); will make the program basically sit and do nothing at all for 1 second, that is called blocking code. The robot can easily lose the line in that amount of time.

Look at the BlinkWithoutDelay example for a method to not block program execution.

2 Likes

That's going to slow things down too.

1 Like

Your code does that here:

    rtc.setTime(/*year,1900-2100*/2023, /*mouth,1-12*/7, /*date,1-31*/25, /*hour,0-23*/15,/*minute,0-59*/23,/*second,0-59*/0);//Set Set initial time .

1 Like

Missed that little delay sat there. Thanks

Excuse my ignorance but how so? Isnt that just setting the baud rate for serial port? What would you suggest? Many thanks

Do you know how I can set it so it keeps the time thereafter.

Many Thanks

Something much faster.

1 Like

115200? Will that do it

Yes, much better.

Comment out that line and re-upload your sketch.

2 Likes

Thanks for all your help, all suggested fixes in place and working great.

:slight_smile:

1 Like

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