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