Need assistance with mellis() and motor on/off

Hello All:

I am slowly getting all the components working correctly for an experimental marine Autopilot. The main sensor is the HMC6352 magnetic compass. The program follows a heading input, taken from the compass. The steering motor is driven with a Pololu 18v25 motor controller. In the final stage, the gear-reduced steering motor will actuate the hydraulic helm by means of a cog-belt and a manual clutch to the ships wheel.

However, I would like to turn the steering motor on/off in discreet steps to make the corrections gradually, not full ON right, or full ON left. After using delay's (simple, but not good) for on time and off time, the program is working pretty good. After trying many, many variations of millis(), the motor OFF time is working perfect, but I have hit the wall trying to get millis() working for the motor ON time.

Here is the motor timing code.

void Motor_state()
{
digitalWrite(relayPin, LOW);
//
if (millis() - previousMillis > interval) { // 1500 motor OFF time
previousMillis = millis();
digitalWrite(relayPin, HIGH);
}
delay (200); // motor ON time

return;
}

The relayPin is the output to open or close a relay, in series with the Pololu motor controller PWM signal, effectively starting or stopping the motor. The millis() interval is 1500ms in the set-up.

The motor off time with millis() works fine, and the program does work correctly to my expectations. The motor speed is variable with a potentiometer, as well as the dead-band +/- degrees variable set by a potentiometer. Looking good, so far. I will add pots for the motor off time , as well as motor on time later, so it can be fine tuned in actual operation. The times used are estimates of what will move the "wheel" in discreet steps.

The delay lets the motor run for 200ms, but how can I use the millis() function for the motor ON time, and get rid of the dreaded "delay"?

I read a bunch of posts in the forum, looking for an answer, and might have missed one like mine.

After much head-banging, it's time to bring in the heavy artillery!

Thanks,
John

The function name, Motor_state() implies that you are trying to control the state of the motor. Your problem description says the same thing.

The code in the function does not say that, though.

The first thing that is missing is any way to track the state of the motor. Is it running or not?

The function turns the motor off, and then decides whether, based on time, that was a mistake. If it was, it turns it back on.

How is this function being called? That info is needed to help you define a solution.

It seems like the function needs to know whether the motor SHOULD BE on or off, in addition to needing to keep track of whether it IS on or off.

Hi Paul:

I used the name Motor_state not thinking that "state" should be the test, or that anything should be tested! In my many efforts to control motor on/off. I copied the blink without delay example, and got the motor to turn on/off like an led, using motorState, and digitalWrite, HIGH.

Obviously, no success keeping the motor on.

I will go back to working/thinking, on the "state" of the motor, working with your explanation, and use a test.

Thanks for your analysis.

I will get back to you soon.

Regards,

John

Need assistance with mellis() and motor on/off (Read 50 times)

Where is MELLIS when you need him, that's what I want to know... :wink:

I recently needed a timer and kind of gave up on how I was using Millisecond timer function. Then I found a new timer option.

http://www.arduino.cc/playground/Main/MsTimer2

I was able to have a COUNTDOWN timer and a 1 second LED flash without a lot of hassle and NO delays.

I don't know if it helps, but here is some sample code on how to use it.

#include <stdio.h>
#include "MsTimer2.h"

int seconds_ones;
int minutes_ones;
int seconds_tens;        // Initialise values for tens and ones units of m,s
int minutes_tens;        // these are the values actually sent to the LEDs
volatile unsigned char tick;
int seconds = 10;
int minutes = 9;     // Inititialise actual values for mm,ss
char timestr[30];
int LEDPIN = 13;
boolean toggle;




void setup()
  {
  pinMode(LEDPIN,OUTPUT);
  Serial.begin(9600);
  Serial.println("Setup");
 
  toggle = true;
    MsTimer2::set(1000, decrement_time);// every second decrement_time() is called
    MsTimer2::start();


  }





void display_time () {            // Function to display the time

   toggle = !toggle;
   if (toggle == true){
     digitalWrite(LEDPIN,HIGH);
   } else {
     digitalWrite(LEDPIN,LOW);
   };



   sprintf(timestr,"time %02d:%02d\n", minutes, seconds );
   Serial.print(timestr);
}




void decrement_time() {
 seconds--;
if (seconds == 0 && minutes == 0)
{
    // We're done, exit
    //set done flag
}
else if (seconds < 0)
{

    seconds = 59;
    minutes--;
    if (minutes < 0)
    {
        minutes = 59;
    }
} 

    tick++;                             // indicate that the time has been updated
}







void loop () {
   if (tick) {         // If a tick has occurred
      tick = 0;         //    reset indicator that we have a new time
      display_time();  //    and show it off!
    }

}

Hello pwillard:

I never even noticed my mis-spelling of millis, and spelling has never been one of my difficulties. I have been typing the word so much lately, trying different approaches to the problem, my brain must be in a millis() loop!

Thanks for the example. I will look at if over. Today I am pretty worn out from coding frenzy. Every attempt, you think This is it! Then, bonk!
Anyhow, it is really fun to see things coming together after hours of effort.

I have been working at variations on the suggestions by Paul, but the problem is only a 250 to 300 millisecond delay in the main program, when the steering motor is moving the wheel a small amount. The longer delay is the time between the steering motor actuation commands, and that works perfect with millis().

Everything in this sketch as it stands, is working pretty good, and that time delay might not be affecting anything important, as the heading is being changed. It takes a period of time for a vessel to turn a small amount.

Looking forward to the real world test.

I still need to understand the nuts and bolts of the millis() function better, as well as "state".

John