Help - Millis Function

Thank you Dave.

You interpreted it correctly.

Thank you for catching the mistake in 4th line. It should be ON-ON-OFF

You should start with the example "Blink Without Delay" from the IDE to learn about millis();

Imho the table fits well in an array.

if you add the interval of 1000ms to the table, you could use a structure and the sequence gets even more versatile.

something like:

/* https://forum.arduino.cc/t/help-millis-function/1115289/16
   switch on/off outputs in a sequence
  by noiasca
  2023-04-15
  code in thread
*/

const uint8_t outputPin[3] {6, 5, 3}; // an array with all output pins

struct Sequence {     // one Sequence holds the interval an the state of the 3 outputs
  uint32_t interval;  // how long should this line be active
  int stateA;         // output 0
  int stateB;         // output 1
  int stateC;         // output 2
};

Sequence sequence[] {         // several sequences in an array
  {1000, HIGH, LOW, LOW},     // interval, stateA, stateB, stateC
  {1000, HIGH, LOW, HIGH},
  {1000, LOW, HIGH, HIGH},
  {1000, HIGH, HIGH, LOW}
};
const uint8_t noOfSequence = sizeof(sequence) / sizeof(sequence[0]);  // calculate the number of sequences

// a non blocking function to activate/deactivate outputs by an defined sequence
void sequenceRun() {
  static uint8_t current = noOfSequence - 1; // we start in the last sequence
  static uint32_t previousMillis = -5000;    // make the timestamp already outdated

  if (millis() - previousMillis > sequence[current].interval) {
    previousMillis = millis();               // remember current timestamp for next runt
    current++;                               // increase current sequence
    if (current >= noOfSequence) {           // handle rollover
      current = 0;
    }
    Serial.println(current);                 // debut print to follow what the program is doing
    digitalWrite(outputPin[0], sequence[current].stateA); // set the output according to the table line
    digitalWrite(outputPin[1], sequence[current].stateB);
    digitalWrite(outputPin[2], sequence[current].stateC);
  }
}

void setup() {
  Serial.begin(115200);
  for (auto &i : outputPin) pinMode(i, OUTPUT);  // call pinMode for each output pin
}

void loop() {
  sequenceRun();
}

if your relays are low active you have to change HIGH/LOW

So… does the very simple and straightforward four times set the outputs and delay work as you wish?

That code certainly looks like it could make any three part four step clocks at one second sequence happen.

Therefor we done?

a7

You could store the ONs and OFFs as values in a 2D array if you know about those? Arduino Reference on arrays, beginner friendly!

An array lets you have many variables that have the same name plus number(s) to identify which one.

A 1 dimension array is a line, the identifier is one number = position.
A 2 dimension array is a rectangle, identify by row and column = position.
A 3D array I am sure you already made the leap?

So you can make code that reads an array and switches outputs ON/OFF?

Yes. It works now. Thank you for the response. I also found out that one of the wiring was loose.

I am also trying to learn from all their suggestions. I need to learn and understand Arduino's Language Reference.

Thank you. I will read more about arrays and will apply to my project.

Yay! Works is good.

Please post the code that works. Even if all it is are corrections to the code in #11, which was just so close.

TIA and good luck on progress with figuring out the rest of the stuff ppl tossed into this along the way.

Soon you'll be wanting to do stuff that this approach will simply not support. For now, take the win.

a7

This is what I got from the OP first post..

-jim lee

Arrays are a basic part of C/C++. They can save you from tons of typing.

The use of struct{} with user defined data types will help too.