Troubles implementing millis()

Hello, first time posting. Apologies if i haven't implemeted the ettiquette effectively.

I am hacking a cheep RC car. I am using an UNO and a H Bridge controller to drive 3 DC motors.
2 OFF drive the RC car, 1 OFF controls the steering.

THe wiring between the H Bridge controller, motors and Arduino are all confirmed to be correct. I have verified all the pin associations and confirm the correct operations of Pin HIGH/LOWs to get the required output for each function.

The trouble is implementing the use of millis() to time the on/off time of the motor drive.

Effectively I want to drive forward for a set period, then Turn right, drive forward, etc. repeating these steps to have the vehicle do a set circuit around the back yard.

below is the code (or at least should be, if i have used the TAGS correctly).

My trouble is i expect after the first IF statement in forward(); the motors to come on.
however i am finding that even when the comparison is TRUE the analog and digital write instructions seem to be skipped, and nothign happens.

I think it is in the equation 'currentMillis - prevousForwardMillis', as when i serial.print the result of that math the answer is always something like 4294953788, incrementing.

thoughts?

//variables that won't change
// For PWM maximum possible values are 0 to 255
const int motorSpeed = 200;

// Motor A connections
const int enA = 9; // steering speed
const int in1 = 8; // Left?
const int in2 = 7; //RIght?
// Motor B connections
const int enB = 3; // speed
const int in3 = 5; // forward?
const int in4 = 4; // back?

unsigned long previousForwardMillis = 0;  //some global variables available anywhere in the program
unsigned long previousTurnMillis = 0;// puts a starting value for the variable
unsigned long currentMillis = 0;
unsigned long currentTurnMillis = 0;

unsigned long period = 245;// how long to pulse to get 1 wheel rotation
unsigned long distance = 8;// how many wheel rotations in 2000mm @ 245ms
unsigned long turnPeriod = 735;// how long a turn will take
#define EVER (;;);// an endless loop to use in the all stop function

void setup() {
  Serial.begin(9600);
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
  period = distance * period;
}

void loop() {
  // this is the main program repeat area


 currentMillis = millis();// capture the latest value of millis() this is equivalent to noting the time from a clock
                    
 forward();
 
}


void forward() {

 if(currentMillis - previousForwardMillis < period){
  analogWrite(enB, motorSpeed);
  digitalWrite(in3, HIGH); //forward
  digitalWrite(in4, LOW); 
 
  
  }
  
 if(currentMillis - previousForwardMillis >= period){
  analogWrite(enB, 0);
  digitalWrite(in3, LOW); //stops motors
  digitalWrite(in4, LOW);
  
  }
   previousForwardMillis += period;
  Serial.print("currentMillis: ");
  Serial.println(currentMillis);
  Serial.print("previous ford Millis: ");
  Serial.println(previousForwardMillis);
}

I think this statement should be inside the } just above... so the timer is reset only once the period is reached. Currently you are reseting the timer every loop.

even putting previous inside the conditional would result in the < condition being immediately and repeatedly true

the code needs to recognize two state: going straight and turning, and remaining in each for some period of time

consider

// autonomous driver

const unsigned long PeriodStr  = 3000;
const unsigned long PeriodTurn =  500;
      unsigned long period     = PeriodStr;
      unsigned long msecLst;

bool straight = true;

// -----------------------------------------------------------------------------
void loop (void)
{
    unsigned long msec = millis ();

    if ( (msec - msecLst) >= period)  {
        msecLst = msec;

        if (straight)  {
            straight = false;
            period = PeriodTurn;
            Serial.println ("turn");
        }

        else {
            straight = true;
            period = PeriodStr;
            Serial.println ("straight");
        }
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);
    Serial.println ("straight");
}

@gcjr I apologize for this rather late reply. This work perfectly, there was some little adjustments I required to work for my purposes, but it was a great implementation.

I appreciate you taking the time to assist me.

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