Hi, I am building and coding a robot car for a school project. The objective is to accurately predict the time the robot takes to get from point A to point B. I've used speed encoder disks with IR sensors to sense how the robot is moving and currently the robot only does 4 movements: turn right, turn left, move forward, and move back.
I've decided that each of these movements including the delay after should take 4 seconds (the robot moves at most 50 cm at a time and 4 seconds is plenty of time). I'm not too familiar with using the millis() function, but I'm trying to use it to stop the robot for 4-(however many seconds it takes to do the movement) so I can just count up the functions and multiply by 4 to get the approximate time.
In the code below, I have made it move forward and right which should get a reading of 8000 milliseconds on the serial monitor, but I'm getting way over (8813, 8634, 8645, 8632, 8669 etc.) Is there a problem with the code?
Materias:
DC motors
L298N motor driver
arduino uno
IR sensors with encoder disks
6 AA batteries
const int MOTOR_A=2;
const int MOTOR_B=3;
const float slotcount=20.00; // 20 Slots in encoder disk
volatile int counter_A=0;
volatile int counter_B=0;
// MOTOR_A
int enA=10;
int in1=9;
int in2=8;
//MOTOR_B
int enB=5;
int in3=7;
int in4=6;
const long runTime= 4000;
unsigned long previousTime=0;
unsigned long currentTime=millis();
//MOTOR_A pulse count ISR
void ISR_countA(){
counter_A++;
}
//MOTOR_B pulse count ISR
void ISR_countB(){
counter_B++;
}
//Function to convert from centimeters to slots
int CMtoSlots(float cm) {
float circumference = 21.5;
float cm_slot = circumference/ slotcount; // CM per Slot
float result=cm/cm_slot;
return result;
}
//0<=mspeed<=255
// Function to Move Forward
void moveForward(int slots, int mspeed){
previousTime=currentTime;
int Counter_A=0;
int Counter_B=0;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
while (slots>= counter_A && slots>= counter_B){
if (slots>= counter_A){
analogWrite(enA, mspeed);
} else {
analogWrite(enA, 0);
}
if (slots>= counter_B){
analogWrite(enB, mspeed);
} else {
analogWrite(enB, 0);
}
}
counter_A=0;
counter_B=0;
analogWrite(enA, 0);
analogWrite(enB,0);
if(currentTime-previousTime <= runTime){
delay(runTime-(currentTime-previousTime));
}
}
// Function to Move Backward
void moveBackward(int slots, int mspeed){
previousTime=currentTime;
counter_A=0;
counter_B=0;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
while (slots>= counter_A && slots>= counter_B){
if (slots>= counter_A){
analogWrite(enA, mspeed);
} else {
analogWrite(enA, 0);
}
if (slots>= counter_B){
analogWrite(enB, mspeed);
} else {
analogWrite(enB, 0);
}
}
counter_A=0;
counter_B=0;
analogWrite(enA, 0);
analogWrite(enB,0);
if(currentTime-previousTime <= runTime){
delay(runTime-(currentTime-previousTime));
}
}
// Function to Move Left
void turnLeft(int slots, int mspeed){
previousTime=currentTime;
counter_A=0;
counter_B=0;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
while (slots>= counter_A && slots>= counter_B){
if (slots>= counter_A){
analogWrite(enA, mspeed);
} else{
analogWrite(enA, 0);
}
if (slots>= counter_B){
analogWrite(enB, mspeed);
} else{
analogWrite(enB, 0);
}
}
counter_A=0;
counter_B=0;
analogWrite(enA, 0);
analogWrite(enB,0);
if(currentTime-previousTime <= runTime){
delay(runTime-(currentTime-previousTime));
}
}
// Function to Move Right
void turnRight(int slots, int mspeed){
previousTime=currentTime;
counter_A=0;
counter_B=0;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
while (slots>= counter_A && slots>= counter_B){
if (slots>= counter_A){
analogWrite(enA, mspeed);
} else{
analogWrite(enA, 0);
}
if (slots>= counter_B){
analogWrite(enB, mspeed);
} else{
analogWrite(enB, 0);
}
}
counter_A=0;
counter_B=0;
analogWrite(enA, 0);
analogWrite(enB,0);
if(currentTime-previousTime <= runTime){
delay(runTime-(currentTime-previousTime));
}
}
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(MOTOR_A), ISR_countA, RISING);
attachInterrupt(digitalPinToInterrupt(MOTOR_B), ISR_countB, RISING);
moveForward(CMtoSlots(10),100);
turnRight(10,100);
int TotalTime= millis();
Serial.println(TotalTime);
}
void loop() {
}