Dc motor automation using h-bridge and millis

Sorry for all the edits, I managed to delete my initial question.

What i'm trying to achieve:
Motor forward
Duration 5 sek
Motor Pause
Duration 1 sek
Motor reverse
Duration 5 sek
Motor Pause
Duration 1 sek

On top of that function i would like to add smooth start/stopp

boolean MOTORdir1 = LOW;
boolean MOTORstate1 = HIGH;


const unsigned int nextTime1  = 6000;  // Do this  every 6000 milliseconds
const unsigned int onTime     = 5000; // EnA on for 5000 milliseconds
const unsigned int offTime    = 1000; // EnA off for 1000 milliseconds

long int goTime1, previousMillis, currentMillis;


int interval = onTime;

void setup() {

  pinMode(8, OUTPUT); //input1

  pinMode(7, OUTPUT); //input2

  pinMode(9, OUTPUT); //EnableA

  goTime1 = millis();

}



void loop() {

  functionGo1();

  if (millis() >= goTime1) functionGo2();


}



void functionGo1() {

  // Set Pin 14 to state of MOTORstate1 each timethrough loop()
  // If EnA hasn't changed, neither will the pin
  digitalWrite(9, MOTORstate1);

  unsigned long currentMillis = millis();

  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    // Change wait interval, based on current state
    if (MOTORstate1) {
      
      // EnA is currently on, set time to stay off
      interval = offTime;
    } else {
      
      // EnA is currently off, set time to stay on
      interval = onTime;
    }
    // Toggle the EnA's state
    MOTORstate1 = !(MOTORstate1);
    
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}


void functionGo2() {

  if (MOTORdir1 == HIGH) {
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    MOTORdir1 = LOW;
  }

  else {

    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    MOTORdir1 = HIGH;

  }

  goTime1 = millis() + nextTime1;

}

And does it work that way?

Paul

Yes it works as intended, how would i go about adding acceleration and deceleration to pin 9

To add acceleration you just need to increase the PWM value gradually.

You have given no indication of the time scale over which you want the acceleration to happen.

...R

So something like this?

// constants for min and max PWM
const int minPWM = 0;
const int maxPWM = 255;

boolean MOTORstate1 = true;


int fadeValue = 0;

// How smooth to fade?
byte fadeIncrement = 5;

// millis() timing Variable, just for fading
unsigned long previousFadeMillis;

// How fast to increment?
int fadeInterval = 50;

void setup() {
  analogWrite(9, fadeValue);
}

void loop() {
  doThefade();
}


void doTheFade(unsigned long thisMillis) {
  // is it time to update yet?
  // if not, nothing happens
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    // yup, it's time!
    if (MOTORstate1 == true) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM) {
        // At max, limit
        fadeValue = maxPWM;
      }
    } else (MOTORstate1 == true) && THIS TIME HAS PASSED {
      //decelerate
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit
        fadeValue = minPWM;
      }
    }
    // Only need to update when it changes
    analogWrite(9, fadeValue);

    // reset millis for the next iteration (fade timer only)
    previousFadeMillis = thisMillis;
  }
}

Wouldn’t the above function conflict with the later function?
My MOTORstate1 is already set as a true/false. HIGH/LOW
Is it possible to overwrite it in a similar manner as i did here or do i have to scrap and rebuild my void functionGo1?

void functionGo1() {

  // Set Pin 14 to state of MOTORstate1 each timethrough loop()
  // If EnA hasn't changed, neither will the pin
  digitalWrite(9, MOTORstate1);

unsigned long currentMillis = millis();
                         
  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    // Change wait interval, based on current LED state
    if (MOTORstate1) {
      // EnA is currently on, set time to stay off
      interval = offTime;
    } else {
      // EnA is currently off, set time to stay on
      interval = onTime;
    }
    // Toggle the EnA's state
    MOTORstate1 = !(MOTORstate1);
 
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}

bellbrant:
So something like this?

That seems to be going the right direction - have you tested it?

Wouldn't the above function conflict with the later function?

Have you tested it?

If there is a conflict then you must make whatever changes are needed to avoid it.

...R