Adding multiple lines of input and running it line by line

hello,
I want to run my pumps and solenoid with multiple inputs given at a time for same pump. I want to store it as a buffer and run the next line after the program is finished with first line.
I already have a working program for running pumps and solenoid where after giving a line of input it executes completely then I have to give other line of input.

#include <RCA.h>
#include <textparser.h>
TextParser parser(",");  // Delimiter is a comma.

RCA modone;



//modone.Pininit();
int s1    = 22;
int s2    = 23;
int s3    = 24;
int s4    = 25;
int s5    = 26;
int s6    = 27;
int s7    = 28;
int s8    = 29;
int c1    = 30;
int c2    = 31;
int c3    = 32;
int c4    = 33;
int c5    = 34;
int c6    = 35;
int s11    = 36;
int s22    = 37;
int s33    = 38;
int s44    = 39;
int s55    = 40;
int s66    = 41;
int s77    = 42;
int s88    = 43;
int c11    = 44;
int c22    = 45;
int c33    = 46;
int c44    = 47;
int c55    = 48;
int c66    = 49;

int pumpnum, pul, From_Solenoid, To_Solenoid; 
unsigned long Delay;

byte            pinS;
byte            pinC;
byte            pinS1;
byte            pinC1;
byte            on;
byte            on1;
//char flag1,flag2;
unsigned long   msecLst;
unsigned long   interval;
unsigned long   msecLst1;
unsigned long   interval1;
unsigned long   msec;
unsigned long   msec1;
//enum { Off = LOW, On = HIGH };
void setup () {
    Serial.begin (9600);

    modone.PinSetup();
}
void pumpOff (){
  if (on)  {
   if ((msec - msecLst) >= interval){
      modone.line1_soff();
      on= false;
      modone.pump1_off();
      modone.sgnled_off();
      flag1 = 777;
      Serial.println("777(line 1 is done)");
//      Serial.println(flag1);
}
  }
    }
void pumpOff1 (){
  if (on1) {
   if ((msec1-msecLst1) >= interval1){
      modone.line2_soff();
      on1= false;
      modone.pump2_off();
      flag2 = 888;
      Serial.println("888(Line 2 Is Done)");
//      Serial.println(flag2);
}
  }
    }
void pumpOn (unsigned long   msecTime, int value){
    interval = msecTime;
    on       = true;
    modone.pump1_on(value);
    if (pinS)
        digitalWrite (pinS, HIGH);
    if (pinC)
        digitalWrite (pinC, HIGH);

    on      = true;
    msecLst = msec;
}
void pumpOn1 (unsigned long   msecTime1, byte value1){
  
    interval1 = msecTime1;
    on1       = true;
    modone.pump2_on(value1);
    if (pinS1)
        digitalWrite (pinS1, HIGH);
    if (pinC1)
        digitalWrite (pinC1, HIGH);

    on1      = true;
    msecLst1 = msec1;
}
void solenoid(int sval){
      if(sval == 1){
        pinS = s1;
      }
      else if(sval == 2){
        pinS = s2;
      }
      else if(sval == 3){
        pinS = s3;
      }
      else if(sval == 4){
        pinS = s4;
      }
      else if(sval == 5){
        pinS = s5;
      }
      else if(sval == 6){
        pinS = s6;
      }
      else if(sval == 7){
        pinS = s7;
      }
      else if(sval == 8){
        pinS = s8;
      }
      else {
        Serial.println("Select from solenoid");
      }
}
void solenoidc(int cval){
      if(cval == 1){
        pinC = c1;
      }
      else if(cval == 2){
        pinC = c2;
      }
      else if(cval == 3){
        pinC = c3;
      }
      else if(cval == 4){
        pinC = c4;
      }
      else if(cval == 5){
        pinC = c5;
      }
      else if(cval == 6){
        pinC = c6;
      }
      else {
        Serial.println("Select to solenoid");
      }
}
void solenoidone(int svalone){
      if(svalone == 1){
        pinS1 = s11;
      }
      else if(svalone == 2){
        pinS1 = s22;
      }
      else if(svalone == 3){
        pinS1 = s33;
      }
      else if(svalone == 4){
        pinS1 = s44;
      }
      else if(svalone == 5){
        pinS1 = s55;
      }
      else if(svalone == 6){
        pinS1 = s66;
      }
      else if(svalone == 7){
        pinS1 = s77;
      }
      else if(svalone == 8){
        pinS1 = s88;
      }
      else {
        Serial.println("Select from solenoid");
      }
}
void solenoidcone(int cvalone){
      if(cvalone == 1){
        pinC1 = c11;
      }
      else if(cvalone == 2){
        pinC1 = c22;
      }
      else if(cvalone == 3){
        pinC1 = c33;
      }
      else if(cvalone == 4){
        pinC1 = c44;
      }
      else if(cvalone == 5){
        pinC1 = c55;
      }
      else if(cvalone == 6){
        pinC1 = c66;
      }
      else {
        Serial.println("Select to solenoid");
      }
}
// --------------------------------MAIN LOOP---------------------------------------------
void loop (){
    msec = msec1 = millis ();
    
//Off Functions calling
      pumpOff ();
      pumpOff1 ();   

//Communication over Serial Port----------------Raspberry Pi----------
    if (Serial.available()) {
        char line[80];
        //Assume `line` is of the form "(1or2),255,2,3,1(in sec)\n".
        Serial.readBytesUntil('\n', line, sizeof(line));
        parser.parseLine(line, pumpnum, pul, From_Solenoid, To_Solenoid, Delay);

        Delay*=1000;
        
        switch (pumpnum){
          case 1:
              pinS = 0; pinC = 0;
              solenoid(From_Solenoid);
              solenoidc(To_Solenoid);
              modone.sgnled_on();
              pumpOn(Delay,pul);
             break;
          case 2:
              pinS1 = 0; pinC1 = 0;
              solenoidone(From_Solenoid);
              solenoidcone(To_Solenoid);
              pumpOn1(Delay,pul);
             break;
          case 3:
                modone.pump1_off();
                modone.pump2_off();
                modone.line1_soff();
                modone.line2_soff();
              break;
        }
    }
}

it's not obvious what the code is trying to do. (why are there 2 pumps)

looks like the 2 pumpOff functions need to be repeatedly called and are not, to check for the time interval to expire to perform some action

(i see a digitalWrite() setting pinS/S1 HIGH, but don't see similar setting pins LOW)

presumably you don't want to process the next line of input until the operations for the previous line complete.

seems that somewhere there needs to be flag(s) indicating that the current line of input hasn't completed and to defer reading the next line

when you say buffer a line of input, do you only want to accept one new line of input or multiple lines. the line is already buffered by the Serial code and won't be lost until readBytesUntil() is executed

  • use of arrays would simplify the code
  • good practice to only Capitalize constants, not variables

Is this a library/code that you wrote?

If you are worried about processing commands sequentially you can read the commands, place them on a queue, and then process commands from the queue one by one.

First of all, I don't know if this was your intention, but both pumps are turned off on a regular basis, every time you go around the loop, regardless of what the program is doing.
If you look at your case statement cases 1 & 2, where you turn on a pump, they will be immediately turned off after.

If your intention was just to initialise, then do this in the setup function.

Arrays have been mentioned already but all you need is a loop incrementing to the next array element to read a sequence of numbers.

hey, I want to store a bunch of lines in Arduino through serial port and split them and use there values to run the pumps at different intervals and at different pulse values one after another line. I tried doing this using struct to store the lines of values but I have no experience in using struct or is there a better way to do this.

struct Prog{
  char cmds[50];
};
Prog prog_line1 = {
  "1,255,1,2,10"
};

yes, that the library I created to just on/off the pumps and solenoids.

yes, this is what I want please refer post#5

why not

char * cmds [] =  {
  "1,255,1,2,10",
  "1,500,3, 4, 11"
};

In my project there are two lines ( line 1 = one pump and 2 solenoids combined) and line 2 = second pump and 2 solenoid combined). So both the lines will be operating with different lines of code one by one with different time interval. If this is possible with any method I have no problem. Accepting the program lines and storing them and using them for each line one after another.

Post #8

is the problem that you need to wait for one line to complete or that you need to wait for the operation of one pump to complete before accepting a command for that pump, but accepting a command for the other pump?

One line should work with multiple lines of program than accept different set of program (some lines of values). The values for both the lines will be different and first the values should be accepted Through serial it should be fed in the program lines then both the pumps should perform their respective programs simultaneously.

what does this mean ? what do you mean by "program"?

I mean the set of lines each line include (pump no., Pulse, from solenoid, to solenoid, delay)

unclear.
i just created a 2nd line with some #s with no specific meaning to illustrate an array with multiple c strings instead of a struct containing a single string

it's still unclear when you need to to wait to process the next line?

it's abundantly clear that the line of input specifies a pump, 2 solenoids and a time the pump needs to be on.

it appears that 2 pumps can be ON at the same time.

it makes sense to wait if the next command line if it is for a pump that is currently ON. but does that mean the following line can be processed if it is for the other pump that is OFF?

@Prominent_being This thread looks like it is further to your your previous questions here:

multiple operations with multiple inputs

from which it looks like you decided to take very little in terms of making your code better: easier to read, easier to fix, easier to enhance or extend and so forth.

I advise again that you take a bit of time off from the project and do some climbing on the learning curve. Every minute you spend learning will pay you back with hours later on, and save some trouble when the same things come back at you again and again.

a7

  • I am sorry for not being clear about my project in my previous post. Let me first tell you there are 2 lines each line consists of 1 pump and 2 solenoids of 8 valves each.

  • each lines takes input like pulse, from solenoid, to solenoid and delaytime.

  • first Arduino should take data for line 1 (some 4 to 5 lines of values like "pulse, from solenoid, to solenoid, delaytime" all the values like delay, pulse and solenoids might be different for different lines totally depends on input) then same for line 2 and store the data in Arduino.

example:

  • Once both the lines got the the values (or 4 to 5 lines of different values) and stored in Arduino.

  • Then if 1 is pressed pump 1 or line 1 should start running its saved program (lines of values) similarly if I press 2 pump 2 should carry out its operations with whatever the saved values and delays are.

I am confused, it seems like you are over using the word "line".

Guessing: you want to collect input that may be as many as 4 or 5 "commands" which come in over serial and may be for one or the other of two pimp/solenoid systems.

Once all those commands have been stored away, you want then to "run" one or the other set on the appropriate pimp.

One button will run the (as many as 5) commands on one, and so forth.

So… what signals the end of the input phase? You need some kind of "done telling you all about it" input to then switch to run mode.

And… what happens if you are running the commands for pimp,one and you press button two?

Please say how many commands total you might be talking about. It doesn't look like you will run out of memory for those, so pick a plausible maximum, static storage can be reserved for all of them directly literally in the code.

Or say how I've gotten it all wrong… but please use some new word for the stat that informs one action of one pimp.

And a different word to f]refer two whatever it is there are two of physically - pump, solenoids &c., that comprise them.

a7

when it receives '888' consider it as the end of the input phase.

then pump 2 stored commands should start running if there is, else return error in pump 2.

in one line ? about 4 values pulse, from solenoid, to solenoid and delaytime.
almost 7 to 8 command lines of the above format.

are these lines predefined in an array as i illustrated?

do you simply want the next line to be processed when a pump operation completes and is turned off

No these are not predefined lines. It takes input from serial once previous operation of pump is finished.