Turning on and off States instead of physical output/imputs

Hi I have tried to self help but am possibly googling the wrong things. I am trying to create a dosing pump sketch and have 4 peristaltic pumps. My sketch is huge as it is part of a more involved controller however I want to create a touch screen button and when I push it, it includes the selected pump in a schedule. I know how to activate an input/output when I push the coords but how can I cycle though states easily? Am I looking to create a finite state machine??

For example I would have dosing pump 1 with a fill circle beside it that is red, pushing it turns it green and then that particular pumps dosing and on/off times would be included in a schedule. If I push the circle beside pump 2, that one would also be included in the schedule. So at any stage I could have any combination of the 4 pumps included or excluded.

My question is, what is the best way to organize the states of the pumps so its easy to code. I cannot seem to find examples of what I am looking to do because I think I am searching for the wrong definitions.

I don't understand whether you're asking about driving the pumps, or about managing the display/api of the pumps. Your code should probably look like:

void loop() {
  update_touchswitches();
  update_display();
  drive_pumps();
}

and yes, each of those functions can probably be set up as an FSM, of greater or lesser formality, and none of them should use delay()

Every time you touch the button increment a state variable then use that variable to determine how to draw the button/graphic.

if (buttonPressed()) {
    butState++;
    if (butState > MAX_STATES)
        butState = 0;
}
switch (butState) {
    case 0:
        // draw pic for state 0
        // do other state 0 stuff
        break;
    case 1:
        // draw pic for state 1
        // do other state 1 stuff
        break;
     //etc
}

Rob

That last one sounds like what I am trying to achieve, it looks complicated to change the states though for the situations I mentioned.
Say you advance the state to pump on for the first pump, pump on for the second pump, then turn the first one off again then turn the third one on it will get messy quickly won’t it?
I want to be able to enable or disable all of them on the fly.

Edit
I should add I am using the UTFT library
the “button” is coordinates on a touch screen
Something like

if((x>=85) && (x<=114)){
    if((y>=38) && (y<=63)){

No1Daemon: Hi I have tried to self help but am possibly googling the wrong things. I am trying to create a dosing pump sketch and have 4 peristaltic pumps. My sketch is huge as it is part of a more involved controller however I want to create a touch screen button and when I push it, it includes the selected pump in a schedule. I know how to activate an input/output when I push the coords but how can I cycle though states easily? Am I looking to create a finite state machine??

That is probably the simplest solution.

For example I would have dosing pump 1 with a fill circle beside it that is red, pushing it turns it green and then that particular pumps dosing and on/off times would be included in a schedule. If I push the circle beside pump 2, that one would also be included in the schedule. So at any stage I could have any combination of the 4 pumps included or excluded.

My question is, what is the best way to organize the states of the pumps so its easy to code. I cannot seem to find examples of what I am looking to do because I think I am searching for the wrong definitions.

You could represent ON/OFF elements as bits in a variable. It could even be a state variable but doesn't have to.

State doesn't have to be a 1-2-3-4-etc sequence either, just something that your conditionals (if-else, or switch-case, or loops) either run on or not.

I'm NOT saying "organize it this way" here, just giving an example of out of the box use of state values/

const byte pump1 = 0x10; // bit 4 = 16 decimal = 10 hex const byte pump2 = 0x20; // bit 5 byte state = 0; // I can have 16 process states, those cover 0-15. I can specify hardware to use in the upper 4 bits.

if ( state && 0x10 ) // pump 1 { switch ( state && 0x0F ) // which function pump 1 should do { case 0 : // do whatever break;

// etc } }

f ( state && 0x20 ) // pump 2 { switch ( state && 0x0F ) // which function pump 2 should do { case 0 : // do whatever break;

// etc } }

Whatever fits your existing code, chart that out and different ways may become obvious.

It's not that complicated, the simple thing to do is to just replicate that code 4 times, once for each button/motor. But that will look like a dog's breakfast before long.

The better way is like westfw did (compartmentalise each function) then start using arrays to hold all the states etc. Once you have code to set all the states of the buttons it's then a simple thing to scan the states and activate all motors that are in state ON or whatever you want to call it.

So let's forget about the mechanics of drawing circles etc, that's irrelevant to the fundamental job.

Document how many states each button can have, is it just ON/OFF or are there more?

it will get messy quickly won't it?

Not if written correctly.


Rob

Graynomad:

it will get messy quickly won't it?

Not if written correctly.

Lol. I will still be writing it. Will give it my best shot and see how I go. Thanks all.

I would use a top-down approach where you have all the main logic in place that calls functions that in time will do the actual work (much like westfw did). At first the functions will have nothing in them but then you can start testing by hardcoding values, for example

void update_touchswitches() {
    button_values[0] = NOT_PRESSED;
    button_values[1] = PRESSED;
    button_values[2] = NOT_PRESSED;
    button_values[3] = NOT_PRESSED;
}

#define PRESSED 1
#define NOT_PRESSED 0

byte button_values[] = {OFF,OFF,OFF,OFF};

update_touchswitches();
for (int i = 0; i< 4, i++) {
    if (button_values[i] == ON) {
         do ON stuff for this motor/button
    } else {
        do OFF stuff for this motor/button stuff
    }
}

Now you have a reliable source of data for the switches so your other code should (in this case) draw button #1 appropriately and turn on motor #1 (just a LED at first).

Then change it to

void update_touchswitches() {
    button_values[0] = NOT_PRESSED;
    button_values[1] = PRESSED;
    button_values[2] = PRESSED;
    button_values[3] = NOT_PRESSED;
}

And see if it works with two button pressed etc.


Rob

with 4 variables and in this case I will be setting up a schedule I would have 24 possible combinations. For instance if button 1 is pressed then timer and motor pin turns on from 4pm for 12 seconds button 2 is pressed timer and motor pin turns on from 5pm for 12 seconds button 1 is pressed again 4pm start is removed from schedule button 3 is pressed timer and motor pin turns on at 6pm for 12 seconds etc etc So there would be 24 combinations that could exist within the schedule. It looks quite easy to write a finite state machine with 4 variables and 24 states but I was just wondering if there was an easier way to achieve it. I have been working on other issues to be honest and have not yet tried what you suggested.

You have planned run conditions of nothing, 4pm, 5pm, 6pm.

If run == nothing and you press button #1 then run = 4pm else if #2 the run = 5pm, etc If 4pm is set and you press button #1 then run = nothing, etc You can guess about if 5pm or 6pm.

Then you look that over and do you see a general pattern? Like if the planned runs are 0, 1, 2, 3 how that could relate to buttons as numbers too?