Drink dispenser: Multiple Commands with Spark.function

I’m building a drink dispenser that controls 8 pumps via MOSFET’s. I want to be able to send the Spark core a command via WiFi (I presume with Spark.function), and trigger each “channel” depending on the command. Let’s say we have a drink that needs 4 cl from Pump1, 2 cl from Pump2, 10 cl from Pump 3 etc.

How would it be best to structure the command structure? Via multiple Spark.function calls, each per pump? If so, can I trigger each pump simultaneously via multiple calls? Does the Spark Core cue up Spark.function calls or can it handle it simultaneously. Because I want all pumps to activate simultaneously but end independently depending on incoming value.

How would you structure this and issue commands to the Spark Core to control it?

The facts of the project is:

8 Pumps controlled via MOSFET’s connected to the PWM pins (D0, D1, A0, A1, A4, A5, A6, A7)
I want to issue a/or multiple curl commands that trigger all the pumps to run for a set time. The time corresponds to a specific cl value. Basically I’m thinking of a syntax that says, PUMP0:10 where 10 equals 10 cl. But I want to be able to trigger them all so I don’t have to wait for each pump to finish before continuing to the next one.

At the moment you’d be limited to four independent Spark.functions, so one fn per pump would be out.
If you want to send the calls via the cloud, you might face unpredictable round trip time, but this is normally no problem.
With a local “cloud” setup you can fire calls with less round trip and more frequently.
You can fire each fn in “parallel” to the others, but I don’t think firing the same one twice in short succession would be safe.

I’d go for one function with a parameter like P0:10;P3:2;P8:7.
Inside the fn you parse you parameter and prepare the values for each pump and then fire the action.

Thanks for the feedback @ScruffR! I can conceive of sending just one request with just P0:10;P1:00;P2:20;P3:05;P4:07;P5:15;P6:00;P7:10 But the question remains, will it be executed in “parallel” if I do so and how would that look in code? How can I do simultaneous matches/triggers for each pump and not have the response/pumping be done in a cascading way?

@felixekman, the simple way is to parse your request into a valve control array which would contain the parsed valve timing. Then you would execute the entire array as one control sequence with the correct timing on each valve. Your Spark.function() parser could create a command queue which would be executed once the existing sequence runs out. Not sure if that makes sense or not :stuck_out_tongue:

@ScruffR, do your magic here. I love your explanations :wink:

Since it’s only one function call where you receive all values at once, you can prepare all the desired values in the Spark.function routine and as last action in the fn (before return) you’ll call “GO!”

int[] P = { 0, 0, 0, 0, 0, 0, 0, 0};  // milliseconds for each pump
long time2start = 0;

void setup()
  Spark.function("setPumps", setPumps);

void loop()
  for (int i = 0; i < 8; i++)
    if ((millis() - time2start) <= P[i])

int setPumps(String param)
  for (int i = 0; i < 8; i++)
    int pumpingTime = 0;
    // parse param and set desired time to run in milli seconds
    P[i] = pumpingTime; 
  time2start = millis(); // start NOW

  return (int)time2start;

void doPumping(int pumpNumber)
  // do something

void stopPumping(int pumpNumber)
  if (pumpRunning)
    // do something to stop it

But beware, if you don’t use this for about 49 days, but have your Core running all that time - the Overflow spirits will haunt you and your pumps will mix you the same cocktail all over again :cocktail: :stuck_out_tongue_winking_eye:

And again, I was almost done typing this out and @peekay123 beat me to it - sorry no fancy waffle this time :wink:


@ScruffR, you rock. And I love the code! And for us old folks, the multiple output and timing, single command concept is like a cam in a pinball machine or an engine. I’ve used to concept to do some funky timing in high-power pulsed laser power supply timing :wink:


Damn guys! Wow thank you so much @ScruffR! Now I know how to continue this. Btw, when I’m done with the whole thing I’ll post the code here https://github.com/felixekman/Drinkbot and post a note in this post.