sequential timing

Hi there

this is going to seem like a silly question I know. but I am trying to sequence a variety of outputs to occur one after the other with varying delay times.

I am trying my best to avoid delay at all costs so I am utilizing a technique outlined in the "doing many things at the same time" example.

however I am running into an issue in regards to my timing statements not running in sequential order. the first if statement engages fine but the sequence then seems to skip right to the base state. most examples I have found tend to have only the two states, if and else. I want to run through a sequence of states before reaching my base state which is my else.

#include <PLDuino.h>
using namespace PLDuino;

//Includes required to use Roboclaw library
#include <SoftwareSerial.h>
#include "RoboClaw.h"


  
RoboClaw roboclaw(&Serial3,10000);

int buttonstate1 = 0;

#define address1 0x80
bool sequence = false;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

void setup() {
  
  PLDuino::init();
  

}

void loop() 
  {
  roboclaw.begin(9600);
  currentMillis = millis();

 

  buttonstate1 = digitalRead(30);
  if (buttonstate1 == LOW)
	{
    roboclaw.ForwardBackwardM1(address1,127); //start Motor1 forward at full speed
    roboclaw.ForwardBackwardM2(address1,127);
	digitalWrite(PLDuino::RELAY1, HIGH); //close relay 1
    previousMillis = currentMillis;
    
  
  if(currentMillis - previousMillis > 7000) 
  {
    roboclaw.ForwardBackwardM1(address1,0); //motor one slows
    roboclaw.ForwardBackwardM2(address1,0);
	digitalWrite(PLDuino::RELAY1, LOW); //open relay 1, planning to move the relay operation to a seperate function
    
    previousMillis = currentMillis;
  }

  if(currentMillis - previousMillis >= 500)
  { 
    roboclaw.ForwardBackwardM1(address1,100); //motor continues at 70 percent speed
    roboclaw.ForwardBackwardM2(address1,100);
  }
      
  
  if(currentMillis - previousMillis >= 20000)
  {
    roboclaw.ForwardBackwardM1(address1,0);
    roboclaw.ForwardBackwardM2(address1,0); //motor slows down to near stop
    previousMillis = currentMillis;
  }

  if(currentMillis - previousMillis >= 4250)
  {
   roboclaw.ForwardBackwardM1(address1,64); //motor brakes
   roboclaw.ForwardBackwardM2(address1,64);
   
   previousMillis = currentMillis;
 }
 

  
}
  

else
{
 
  roboclaw.ForwardBackwardM1(address1,64); 
  roboclaw.ForwardBackwardM2(address1,64);
 
  }



}

its a simple problem I am well aware, I am getting mightily irritated by the whole thing and would be very grateful for the input.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

The examples were not written to illustrate sequential timing but you should be able to see how to do that by using the completion of the interval in one function to set the start-time for the next function rather than setting the re-start time for itself.

...R

Shouldn't there be an else statement between each motor command? In other words, start with this; then do this; then do this; and so on, until running out of else statements.

Sorry, I did not see the code in the Original Post when I wrote Reply #1.

Rather than use the same variables for each stage you need separate variables - something like

  if(currentMillis - stage1Millis > 7000)
  {
    roboclaw.ForwardBackwardM1(address1,0); //motor one slows
    roboclaw.ForwardBackwardM2(address1,0);
    digitalWrite(PLDuino::RELAY1, LOW); //open relay 1, planning to move the relay operation to a seperate function
   
    stage2Millis = currentMillis;
  }

  if(currentMillis - stage2Millis >= 500)
  {
    roboclaw.ForwardBackwardM1(address1,100); //motor continues at 70 percent speed
    roboclaw.ForwardBackwardM2(address1,100);
    
    stage3Mills = currentMillis;
  }

...R

I (and several others) helped someone walk through this before. The result is quite a monster of a thread: Timing trouble with millis() for LED control - Programming Questions - Arduino Forum

TL;DR, use arrays to store the action and interval data for the sequence. That way you can use an index variable to keep track of your place in the sequence.

The essence of non-blocking coding is to use data to define the sequence of operations you need to do, and not a list of code statements. When the state of operations is stored completely in variables, it's easy for the processor to drop it for a moment to do something else, then come back and pick up where it left off.

Jiggy-Ninja:
The essence of non-blocking coding is to use data to define the sequence of operations you need to do, and not a list of code statements.

+1

...R