1st state machine

Ok I'm trying to build a simple state machine I'm lost not working rite So far I'm trying to make it (do this) A) light up Leds = true got 2 so far (practice) B) Blink off at separate times would be nice to breakout of the loop so this only happens once. The Idea is to have like 6 blink off at separate times but start together.

//  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long sequenceDelay = 0;
unsigned long flashoffDelay2 = seconds_in_ms(1);
unsigned long flashoffDelay3 = seconds_in_ms(3); 
boolean LED3state = true;   //will turn on led 
boolean LED2state = true;     // will turn on led

long waitUntil13 = 0;
long waitUntil12 = sequenceDelay;
void setup() {
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
}
void loop() 
{
   digitalWrite(2, LED3state);
   digitalWrite(3, LED2state);
   // checking to see if enough time has elapsed
   if (millis() >= waitUntil13) {
      LED3state = !(LED3state);
      waitUntil13 = millis() + flashoffDelay3;
      
   }

   if (millis() >= waitUntil12) {
      LED2state = !(LED2state);
      waitUntil12 = millis() + flashoffDelay2;
      

   }
}

I think your delays are too short, seconds_in_ms(1);. 1 and 3 mec means they will always appear to be on. First try 100 and 300 msec, then lower if needed.

Also, right now, each loop is flashing on its own accord. If I read your requirements correctly, you need to add a synchronization timing state, which controls the Leds to all turn on at the same time, and then sets all of the off-intervals, starting from that time.

I'm using optical relays, so they work opposite off is on and on is off. They all have to be on 1st, then blink off for a set time variable, each one will have its own time. Not critical at the rate they switch so long as the blink off is accurate. Problem is I need them to do this only once, they are dosing various chemicals.

I think your delays are too short, seconds_in_ms(1);. 1 and 3 mec means they will always appear to be on.

@oric_dan: 1 second and 3 seconds. Read again

ok fixed some mistakes, better, think it works not sure, why the delay is too short those are in seconds not ms Problem is I want it only to blink once have no idea how to do that.

//  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long sequenceDelay = 0;
unsigned long flashoffDelay3 = seconds_in_ms(1);
unsigned long flashoffDelay2 = seconds_in_ms(10); 
boolean LED3state = true;   //will turn on led 
boolean LED2state = true;     // will turn on led

long waitUntil3 = 0;
long waitUntil2 = sequenceDelay;
void setup() {
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
}
void loop() 
{
   digitalWrite(2, LED3state);
   digitalWrite(3, LED2state);
   // checking to see if enough time has elapsed
   if (millis() >= waitUntil2) {
      LED2state = !(LED2state);
      waitUntil2 = millis() + flashoffDelay2;
      
   }

   if (millis() >= waitUntil3) {
      LED3state = !(LED3state);
      waitUntil3 = millis() + flashoffDelay3;
      

   }
}

Problem is I want it only to blink once have no idea how to do that.

Move all the code to setup(), then.

Thanks it sounds simple I cant, get it working somehow in setup.

Simple!

Warning: code fragments follow -

bool wasConditionMet = false;

void loop(void)
{
    if ( false == wasConditionMet )
    {
        ... do something useful perhaps ...
        ... once it's done ...
        ... and you don't wish it to ...
        ... execute ever again ...

        wasConditionMet = true;
    }
}

"wasConditionMet" Ok sounds right, not sure how you combine that I tried different ways not working yet.

You seem not to understand thread, after thread, after thread. There continues to be a failure to understand here! I I might think it's just me but I'm not the only one attempting to answer and yet the recurring problem.

I'm trying to give useful answers!

What is it specifically you don't understand?

AWOL:

I think your delays are too short, seconds_in_ms(1);. 1 and 3 mec means they will always appear to be on.

@oric_dan: 1 second and 3 seconds. Read again

My bad.

To OP, there are no doubt many ways to solve this problem. One way to do something one time only is setup a state machine with 3 states, that pass one to the next upon completion, and in the

first state, do the setup [ie turn on the leds, set the off times, and advance the state],

then next pass will be into the 2nd state [check the times and turn the leds off on queue, use flags to indicate when both are off, and when they are, advance the state],

then the next pass will be into the 3rd state [do nothing further, and do not advance the state].

Afterwards, the overall program will go through state 3 every pass through loop(), but it will quickly pass out again, and your program can still do anything else you want.

Also, no doubt 100 other people will have a better way to do this, but this should work.

perkunas: Ok I'm trying to build a simple state machine I'm lost not working rite So far I'm trying to make it (do this) A) light up Leds = true got 2 so far (practice) B) Blink off at separate times would be nice to breakout of the loop so this only happens once. The Idea is to have like 6 blink off at separate times but start together.

Seems to me that rather than a state machine controlling all the LEDs you might be better off having separate variables for each LED recording whether it's on, and how long it is intended to be on for. Also record when all the LEDs were all turned on.

In setup, turn all the LEDs on, note that they are all on and note the time when they were turned on.

In loop(), process each LED. If the LED is on, work out how long it has been on and whether this exceeds the time that it is intended to be on. If so, turn it off and note that it is now off.

For simplicity, I'd put all the LED-specific data in arrays so that you can process all the LEDs within a simple for loop.