Help - Millis Function

Hello Everyone. New guy here trying to learn Arduino.

I need to create a code that will continuously cycles / turns On and Off 3 different solenoids individually in different times.

Solenoid 1 - Start = 0 sec
Duration = 1 sec
Off = 1 sec

Solenoid 2 - Start = 1 sec
Duration = 1 sec
Off = 1 sec

Solenoid 2 - Start = 2 sec
Duration = 1 sec
Off = 1 sec

I used delay function but they all turned Off at the same time. I read some discussions and sample about millis but I am still confused.

Please help. Maybe there is already forum for it that I did not see.

Thanks,
Normz

1 Like

The BlinkWithoutDelay example shows how to toggle an LED on and off to make it blink. Try that first to turn a single solenoid on and off and report back.

2 Likes

Always follow the forum’s posting guidelines.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring. Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

2 Likes

You should at least post that code. It should be easy to do what you describe, using only the delay() function.

A design idea for you. There is really only one repeating interval of the same duration. Why not use the same interval for timing, but activate a different output each time? You can just keep a counter variable to indicate which output is currently active.

2 Likes

First you must understand what blocking code does, see flow chart below.

See the attached sketch to see how millis( ) can be used to achieve non blocking delays.


//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       23/02/02    Running code
//
//
//

const byte heartbeatLED      = 13;
const byte secondLED         = 2;

//timing stuff
unsigned long heartbeatTime;
unsigned long secondLedTime;

//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(secondLED, OUTPUT);

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  //**********************************************                h e a r t b e a t   T I M E R
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatTime > 1000ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //**********************************************                s e c o n d L E D   T I M E R
  //is it time to toggle the 2nd LED ?
  if (millis() - secondLedTime >= 200ul)
  {  
    //restart this TIMER
    secondLedTime = millis();

    //let's do something

    //toggle LED
    digitalWrite(secondLED, digitalRead(secondLED) == HIGH ? LOW : HIGH);
  }


  //**********************************************
  //Other non blocking code
  //**********************************************

} //END of   loop()


//********************************************^************************************************

2 Likes

What happens after that?

Solenoid2 is just the opposite of Solenoid1.

Is the Start period the same as an off solenoid?

Are you more interested in having this working and put out to work for you, or having the fun of really killing it?

a7

Then your code is wrong... if you posted it we could probably tell you exactly what was wrong.

You don't need millis to do what you have described.

1 Like

If you think of the on/off solenoids as LEDs, there's:

...but as written, your problem is simpler. It could be done with delays.

1 Like

consider

struct Timer {
    const byte          Pin;
    const unsigned long MsecOn;
    const unsigned long MsecOff;
    unsigned long       msecPeriod;
    unsigned long       msecLst;
};

Timer tmr [] = {
    { 10, 1000, 1000, 0 },
    { 11, 1000, 1000, 1000 },   // initial msecPeriod delays start
    { 12, 1000, 1000, 2500 },
};
const unsigned Ntimer = sizeof(tmr) / sizeof(Timer);

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();
    for (unsigned n = 0; n < Ntimer; n++)  {
        if (msec - tmr [n].msecLst >= tmr [n].msecPeriod)  {
            tmr [n].msecLst = msec;

            if (On == digitalRead (tmr [n].Pin))  {
                digitalWrite (tmr [n].Pin, Off);
                tmr [n].msecPeriod = tmr [n].MsecOff;
            }
            else {
                digitalWrite (tmr [n].Pin, On);
                tmr [n].msecPeriod = tmr [n].MsecOn;
            }
        }
    }
}

void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < Ntimer; n++)  {
        pinMode      (tmr [n].Pin, OUTPUT);
        digitalWrite (tmr [n].Pin, Off);
    }
}
2 Likes

Thank you so much. I'm reading it.

Hi Larry,

Thank you so much. Below is my coding for the delay.

int SOL1 = 11; //distal balloon
int SOL2 = 12; //mid balloon
int SOL3 = 13; //proximal balloon


void setup() {

  pinMode(SOL1, OUTPUT);
  pinMode(SOL2, OUTPUT);
  pinMode(SOL3, OUTPUT);

}

void loop() {

  digitalWrite(SOL1, HIGH); //distal balloon inflated
  digitalWrite(SOL2, LOW); //mid balloon deflated
  digitalWrite(SOL3, LOW); //proximal balloon deflated
  delay(1000); 

  digitalWrite(SOL1, HIGH); //distal balloon inflated
  digitalWrite(SOL2, LOW); //mid balloon deflated
  digitalWrite(SOL3, HIGH); //proximal balloon inflated
  delay(1000); 

  digitalWrite(SOL1, LOW); //distal balloon deflated
  digitalWrite(SOL2, HIGH); //mid balloon inflated
  digitalWrite(SOL3, HIGH); //proximal balloon inflated
  delay(1000); 


  digitalWrite(SOL1, HIGH); //distal balloon inflated
  digitalWrite(SOL2, HIGH); //mid balloon inflated
  digitalWrite(SOL3, LOW); //proximal balloon deflated
  delay(1000); 

}

Thank you Larry. I will try it.

Thank you so much!

Thank you so much.

Hello to all,

First, I apologize for not posting my codes (1st timer.. please accept my sincere apologies).

Second, Thank you so much for all your responses. I may not be able to thank you individually. I will try all your suggestions and I'm hoping to learn from all these.

Best Regards,
Normz

1 Like

The words above look like they might mean this (sloppy) timing diagram:

...but the code in your loop() looks like it would have a very different diagram. I'm not sure exactly how you'd like the solenoids to operate.

1 Like

Hi Dave,

Thank you so much for the reply. This is what I was trying to code. I'm not sure if it is even possible.

SEQUENCE

ON - 1 sec
OFF - 1 sec

Regards,
Normz

1 Like

Draw a timing diagram like @DaveX shows.

3 Likes

The 4th Sequence looks exactly like 1st in the table, and differs from the "HIGH, HIGH, LOW" in the 4th chunk of code. Is something inconsistent?

One possible non-code related problem is insufficient power-- if the solenoids overload the power supply/wires it could brown-out the Arduino and cause strange behavior where the outputs do not match the expected behavior.

Can you share a schematic and specifications on your power supply and parts?

If you've got adequate power and good connections, you might try adding print statements to show how the CPU progresses through your code, and to localize where things go wrong.

2 Likes

Here's a sloppy timing diagram, drawn on top of your table of my interpretation of your words:

Here, "Time" is the vertical axis, at 1 sec per sequence, and the horizontal axis is the different solenoids, and their LOW/HIGH values.

That seems straightforward and very possible mapping into your code: each row would implement the time interval as the delay(1000) in your code. The digitalWrite(SOLx, val) sttements in your code would seem like they should match each row your table.

However, your worry that it might not be possible, and this on/off 1 sec bit:

...make me think that I might be interpreting your table incorrectly.

2 Likes