# Millis and motor control

Hey guys ive tried to solve this but im doing something wrong.

What I want is for my robot to turn right or left for say 3sec and then proceed forward so im using millis to time how long it has been turning for and when that time is false go forward.

``````else if(t1>301&&t2>301&&Correct==1){//Correcting path if signal lost

if (Left>Right){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis < interval) {
previousMillis = currentMillis;
analogWrite(PWM1,80);
analogWrite(PWM2,0);

}
else if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analogWrite(PWM1,100);
analogWrite(PWM2,100);
}

}
else if (Right>Left){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis < interval) {
previousMillis = currentMillis;
analogWrite(PWM2,80);
analogWrite(PWM1,0);
}

else if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analogWrite(PWM2,100);
analogWrite(PWM1,100);
}
}
Serial.print(currentMillis);
Serial.println();

}
``````

Please if you could tell me what im doing wrong or perhaps have a better way to do it, that would be great.

Thanks.

HI,
If it won't fit in the code tags, you can attach it if you use REPLY rather than Quick Reply.
Also what is it doing that is wrong?

Hope to help...Tom....

I an not the best or brightest programmer. far from it.

if we substiture now for current millis and then for previous.

if(currentMillis - previousMillis < interval) {

now minus then will, as far as I can figure will be one scan or loop time.
not sure how long your interval is, but best I can figure, there is no number small enough to use in interval.

Hey Tom thanks for the reply here is the whole code.

Also what is it doing that is wrong?

Hmmm hard to explain but its not doing what it supposed to lol, like dave said not that i totally understand what he meant but i think

there is no number small enough to use in interval.

so that first if statement for the turning is not working only the forward if statement is, I just need my robot to turn for 3 seconds ± (must still test to get the exact turn time) and then go forward.

``````#include <DualVNH5019MotorShield.h>
DualVNH5019MotorShield md;
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define usTRIG1 4
#define usTRIG2 8
#define usECHO1 3
#define usECHO2 7
char data;
unsigned long t1,t2;//timers

int MS1=0;//Motor speed 1
int MS2=0;//Motor speed 2
int Correct=0;
int Left=0;
int Right=0;
//int Once=0;//So the turn only happens once
long go = 0;
int rfsignalcheck=0;

long previousMillis = 0;
long interval = 3000;

const int M1INA=14;
const int M1INB=15;
const int PWM1=5;//Motor 1
const int M2INA=16;
const int M2INB=17;
const int PWM2=6;//Motor 2

void setup()
{
Serial.begin(9600);
pinMode(usTRIG1,OUTPUT);
pinMode(usTRIG2,OUTPUT);
digitalWrite(usTRIG1,LOW);
digitalWrite(usTRIG2,LOW);
pinMode(usECHO1,INPUT);
pinMode(usECHO2,INPUT);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_set_rx_pin(2);
vw_rx_start();
pinMode(13,OUTPUT);

pinMode(M1INA,OUTPUT);
pinMode(M1INB,OUTPUT);
pinMode(PWM1,OUTPUT);
pinMode(M2INA,OUTPUT);
pinMode(M2INB,OUTPUT);
pinMode(PWM2,OUTPUT);

digitalWrite(M1INA,LOW);
digitalWrite(M1INB,HIGH);
digitalWrite(M2INA,LOW);
digitalWrite(M2INB,HIGH);

//TCCR2B = TCCR2B & 0b11111000 | 0x02;
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);
TCCR0B = _BV(CS00);
}

void loop()
{
unsigned long currentMillis = millis();
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message
for (i = 0; i < buflen; i++)
{
if (buf[i] == 'C'){
digitalWrite(usTRIG1,LOW);
delayMicroseconds(10);
digitalWrite(usTRIG1,HIGH);
delayMicroseconds(10);
digitalWrite(usTRIG1,LOW);
t1=pulseIn(usECHO1,HIGH)/29;//return pulse length in uS
}
if (buf[i] == 'E'){
digitalWrite(usTRIG2,LOW);
delayMicroseconds(10);
digitalWrite(usTRIG2,HIGH);
delayMicroseconds(10);
digitalWrite(usTRIG2,LOW);
t2=pulseIn(usECHO2,HIGH)/29;
//Serial.print(t1);
//Serial.print("\t");
//Serial.println(t2);//distance in cm, 29 not 58 because ultrasound is 1 way only, no reflection
}
if (buf[i] == 'S'){//stop cart and put into remote mode
analogWrite(PWM1,0);
analogWrite(PWM2,0); //break both motors to stop
Correct=0;
t1=0;
t2=0;
rfsignalcheck=1;
}
if (buf[i] == 'K'){
rfsignalcheck=0;
go=0;
}
if (buf[i] == 'F'){//go forward
analogWrite(PWM1,100);
analogWrite(PWM2,100);
go=0;
}
if (buf[i] == 'G'){//go forward faster
analogWrite(PWM1,160);
analogWrite(PWM2,160);
go=0;

}
if (buf[i] == 'H'){//go forward fastest
analogWrite(PWM1,210);
analogWrite(PWM2,210);
go=0;
}
if (buf[i] == 'ST'){//stop
analogWrite(PWM1,0);
analogWrite(PWM2,0);
}
if (buf[i] == 'L'){//turn left when going foward
analogWrite(PWM1,150);
analogWrite(PWM2,0);
go=0;
}
if (buf[i] == 'R'){//turn right when going foward
analogWrite(PWM1,0);
analogWrite(PWM2,150);
go=0;
}

if (buf[i] == 'P'){//just turn left
analogWrite(PWM1,150);
analogWrite(PWM2,0);
go=0;
}
if (buf[i] == 'Q'){//just turn right
analogWrite(PWM1,0);
analogWrite(PWM2,150);
go=0;
}
}
digitalWrite(13, false);
}
if(rfsignalcheck == 1){
go ++;
if(go == 40000){
analogWrite(PWM1,0);
analogWrite(PWM2,0);
}
}
Correct=1;
MS1=t1*2.3;//Motor speed 1
MS2=t2*2.3;//Motor speed 2
if (t1<t2){
if(MS1>255){
MS1=255;
}
analogWrite(PWM2,(MS1-((t2-t1)*2.3)));
analogWrite(PWM1,MS1);
}
else if (t2<t1){
if(MS2>255){
MS2=255;
}
analogWrite(PWM2,MS2);
analogWrite(PWM1,(MS2-((t1-t2)*2.3)));
}
Right=(MS1-((t2-t1)*2.5));
Left=(MS2-((t1-t2)*2.5));
}

else if(t1>301&&t2>301&&Correct==1){//Correcting path if signal lost

if (Left>Right){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis  < interval) {
previousMillis = currentMillis;
analogWrite(PWM1,80);
analogWrite(PWM2,0);

}
else if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analogWrite(PWM1,0);
analogWrite(PWM2,0);
}

}
else if (Right>Left){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis  < interval) {
previousMillis = currentMillis;
analogWrite(PWM2,80);
analogWrite(PWM1,0);
}

else if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analogWrite(PWM2,0);
analogWrite(PWM1,0);
}
}
Serial.print(currentMillis);
Serial.println();

}
// else{analogWrite(PWM1,0);analogWrite(PWM2,0);}
}
``````
``````      unsigned long currentMillis = millis();
if(currentMillis - previousMillis  < interval) {
previousMillis = currentMillis;
analogWrite(PWM1,80);
analogWrite(PWM2,0);

}
``````

You keep resetting previousMillis inside the loop. So you forget the time that it actually started and put in a new time. Don’t change previousMillis while you’re using it for timing.

``````      else if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analogWrite(PWM1,0);
analogWrite(PWM2,0);
}
``````

OK, so after the time period has ended you’re finished with previousMillis but once you set it back to the current time, you will go right back to the state where you’re driving the PWM again. Set it to zero or use some other variable to record that you’ve finished turning and the robot should be doing the next task.