You called some pumpReq function.
But I can't see any pumpReq function defined in the whole program.
that code is ifdef'd out. presumably there's no need to immediately process a "program" because it should only be queued and processed only after the start signal
How do I give new inputs through serial?
enable the Serial code, #if 1 and call queue() with buf
there's no acknowledgement that wat i posted is what you're trying to do
You really need to do a few things.
First is to draw a flow diagram showing exactly what you are trying to do, secondly write pseudo code for how you envisage this process to work.
You say you are taking in a serial value, but you don't say where these value are coming from and at what rate.
There is no use going into writing a load of code if people are flailing around trying to understand what the issue is.
thank you, I have tested the code with my project it is working, I also want to give a signal(88) for terminating the program in middle just like a abort function. I am also attaching the modified code.
#include <RCA.h>
RCA modone;
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;
char s [80];
//const char *programs [] = {};
//int progIdx;
char buf [80];
unsigned long msec;
int pinS,pinC,pinS1,pinC1;
enum { Off = HIGH, On = LOW };
#define MaxTok 20
char *toks [MaxTok];
int vals [MaxTok];
#define NcmdQ 20
struct Q {
char cmdQ [NcmdQ][30];
int head;
int tail;
int cnt;
};
struct Pump {
byte pul,stp,dir;
const char *label;
bool active;
Q q;
int pwm;
int solenoid0;
int solenoid1;
unsigned long msecLst;
unsigned long period;
};
Pump pumps [] = {
{ 3, "pump-0" },
{ 9,10,11, "pump-1" },
{ 8,12,13, "pump-2" },
};
#define Npump (sizeof(pumps)/sizeof(Pump))
void setup (){
Serial.begin (9600);
modone.PinSetup();
Serial.println ("ready");
}
//--------------------------------------------------
int tokenize (char *s,const char *sep ){
unsigned n = 0;
toks [n] = strtok (s, sep);
vals [n] = atoi (toks [n]);
for (n = 1; (toks [n] = strtok (NULL, sep)); n++)
vals [n] = atoi (toks [n]);
return n;
}
void queue (const char *buf,struct Q *q,const char *label ){
sprintf (s, "queue: %s - %s", label, buf);
Serial.println (s);
if (NcmdQ <= q->cnt) {
sprintf (s, "queue: full");
Serial.println (s);
}
memcpy (q->cmdQ [q->head++], buf, strlen(buf));
if (NcmdQ <= q->head)
q->head = 0;
q->cnt++;
};
char *dequeue (Q *q){
if (0 == q->cnt)
return NULL;
char *buf = q->cmdQ [q->tail++];
if (NcmdQ <= q->tail)
q->tail = 0;
q->cnt--;
sprintf (s, " dequeue: %s", buf);
Serial.println (s);
return buf;
}
void Pumprun (char *buf ){
if (4 > tokenize (buf, ",")) {
sprintf (s, "Error: wrong # of values - %s", s);
Serial.println (s);
return;
}
unsigned idx = vals [0];
Pump *p = & pumps [idx];
p->pwm = vals [1];
p->solenoid0 = vals [2];
p->solenoid1 = vals [3];
p->period = vals [4] * 1000L;
p->msecLst = msec;
p->active = true;
if (idx == 1){
rusolenoid(p->solenoid0);
rdsolenoid(p->solenoid1);
if (pinS){
digitalWrite(pinS, HIGH);
}
if (pinC){
digitalWrite(pinC, HIGH);
}
}
else if (idx == 2){
lusolenoid(p->solenoid0);
ldsolenoid(p->solenoid1);
if (pinS1){
digitalWrite(pinS1, HIGH);
}
if (pinC1){
digitalWrite(pinC1, HIGH);
}
}
analogWrite (p->pul, p->pwm);
digitalWrite(p->stp, On);
digitalWrite(p->dir, On);
sprintf (s, " pumpRun: %s On (%lu ms)", p->label, p->period);
Serial.println (s);
}
void Pumpmonitor (){
Pump *p = pumps;
for (unsigned n = 0; n < Npump; n++, p++) {
if (p->active && (msec - p->msecLst) >= p->period) {
analogWrite (p->pul, 0);
digitalWrite(p->stp, Off);
digitalWrite(p->dir, Off);
unsigned idx = vals [0];
if (idx == 1){
modone.line1_soff();
}
else if (idx == 2){
modone.line2_soff();
}
p->active = false;
sprintf (s, " pumpRun: %s Off", p->label);
Serial.println (s);
char *buf = dequeue (& p->q);
if (NULL != buf)
Pumprun (buf);
else {
sprintf (s, " pumpRun: %s is done", p->label);
Serial.println (s);
}
}
}
}
void Pumpstart (){
Pump *p = pumps;
for (unsigned n = 0; n < Npump; n++, p++) {
if (! p->active) {
char *buf = dequeue (& p->q);
if (NULL != buf)
Pumprun (buf);
}
else {
sprintf (s, "%s: pump %d active", __func__, n);
Serial.println (s);
}
}
}
void rusolenoid(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 rdsolenoid(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 lusolenoid(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 ldsolenoid(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");
}
}
//---------------------------------------------------
void loop (){
msec = millis ();
Pumpmonitor ();
if (Serial.available ()) {
int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
unsigned idx = atoi (buf);
if (3 == idx){
Pumpstart ();}
else{
queue (buf, & pumps [idx].q, pumps [idx].label);}
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.