Simultaneous running sub programs

Good day everyone,

I'm relatively new to the Arduino and coding world.
In order to better understand what I want to achieved, i've attache a explanatory image.
What i want to know, is this doable and how should i proceed.
Thank you very much.

Might be doable if you also decide when the turn the various LEDs off.
Paul

Very doable it looks like.

You must break down the handling of a thermocouple into steps, and write it so no steps hang things up waiting or measuring time.

Write it generally so it can serve for multiple thermocouples.

In the loop, call that routine with each thermocouple's data - like where it is in the sequence and how much time is left. One call to the handler for each thermocouple. Each call to make a particular thermocouple progress around your flowchart steps.

At a glance it looks like they are all doing exactly the same thing, with the same times and conditions applied to how to proceed. This makes things easier, it it would be straightforward to have each thermocouple's times and temperatures be part of that thermocouple data.

Indexed parallel arrays for the thermocouple data, or a struct that hold all a thermocouple data.

A switch/case in the function to manage the state of a thermocouple and run the correct section of code (if it is time) to advance.

a7

Strictly speaking, I suspect that you intended to write "struct".

Haha, yeth. Got the edit in there before the edit clock ran out.

a7

consider
what happens after 25 mins?


struct Station {
    int             id;
    byte            pinSensor;
    byte            pinLedBlue;
    byte            pinLedGreen;
    byte            pinLedRed;
    unsigned long   msec;
};

Station stations [] = {
    { 1,  A0, 11, 12, 13 },
};
#define N_STATIONS  (sizeof(stations)/sizeof(Station))

enum { Off = HIGH, On = LOW };

#define TempThresh   220

// -----------------------------------------------------------------------------
void
setLeds (
    Station *s,
    byte     blue,
    byte     green,
    byte     red )
{
    digitalWrite (s->pinLedBlue,  blue);
    digitalWrite (s->pinLedGreen, green);
    digitalWrite (s->pinLedRed,   red);
}

#define FiveMins    5000    // set to 5 * 60 * 1000

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    Station  *s = stations;
    for (unsigned n = 0; n < N_STATIONS; n++, s++)  {
        int temp = map (analogRead (s->pinSensor), 0, 1023, 0, 500);

        char t [80];
        sprintf (t, " %2d: pin %d, temp %d", s->id, s->pinSensor, temp);
        Serial.println (t);

        if (TempThresh <= temp)  {
            if  ((msec - s->msec) >= FiveMins)
                setLeds (s, Off, Off, On);
            else
                setLeds (s, Off, On, Off);
        }
        else  {
            s->msec = msec;
            setLeds (s, On, Off, Off);
        }

    }

    delay (1000);       // slow things down
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600); // Initialise Serial at 9600.

    Station  *s = stations;
    for (unsigned n = 0; n < N_STATIONS; n++, s++)  {
        setLeds (s, Off, Off, Off);
        pinMode (s->pinLedBlue,  OUTPUT);
        pinMode (s->pinLedGreen, OUTPUT);
        pinMode (s->pinLedRed,   OUTPUT);
    }
}

The station return to a "standby" mode waiting to see if the temperature return to 220°C. In standby, the blue led stays lit.


struct Station {
    int             id;
    byte            pinSensor;
    byte            pinLedBlue;
    byte            pinLedGreen;
    byte            pinLedRed;
    unsigned long   msec;
};

Station stations [] = {
    { 1,  A0, 11, 12, 13 },
};
#define N_STATIONS  (sizeof(stations)/sizeof(Station))

enum { Off = HIGH, On = LOW };

#define TempThresh   220

// -----------------------------------------------------------------------------
void
setLeds (
    Station *s,
    byte     blue,
    byte     green,
    byte     red )
{
    digitalWrite (s->pinLedBlue,  blue);
    digitalWrite (s->pinLedGreen, green);
    digitalWrite (s->pinLedRed,   red);
}

#define FiveMins        5000    // set to  5 * 60 * 1000
#define TwentyFiveMins  10000   // set to 25 * 60 * 1000

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    Station  *s = stations;
    for (unsigned n = 0; n < N_STATIONS; n++, s++)  {
        int temp = map (analogRead (s->pinSensor), 0, 1023, 0, 500);

        char t [80];
        sprintf (t, " %2d: pin %d, temp %d", s->id, s->pinSensor, temp);
        Serial.println (t);

        if (TempThresh <= temp)  {
            if  ((msec - s->msec) >= TwentyFiveMins)
                setLeds (s, On, Off, Off);
            else if  ((msec - s->msec) >= FiveMins)
                setLeds (s, Off, Off, On);
            else
                setLeds (s, Off, On, Off);
        }
        else  {
            s->msec = msec;
            setLeds (s, On, Off, Off);
        }

    }

    delay (1000);       // slow things down
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600); // Initialise Serial at 9600.

    Station  *s = stations;
    for (unsigned n = 0; n < N_STATIONS; n++, s++)  {
        setLeds (s, Off, Off, Off);
        pinMode (s->pinLedBlue,  OUTPUT);
        pinMode (s->pinLedGreen, OUTPUT);
        pinMode (s->pinLedRed,   OUTPUT);
    }
}