Guidance/Checkup for code Running Peristaltic Pump

Im attempting to write this code with my own limited experience. If anybody can see any oddities or has any input to improve how my own code can operate according to how I have the system described, I'd love to hear feedback! If anything is unclear, I'll happily explain or give details to clarify anything. Im hoping there is enough info throughout it all.. It's been a WHILE since I wrote any code but I tried my best :smiling_face_with_tear:

// This is a sketch for the purpose of running a peristaltic pump to cycle 
// cleaning solution through a glass vessel at fluctuating flow rates. When the 
// StartStop button is pressed(after the user safely hooks up their rig to 
// the cycling tubes), the green LED will begin pulsing slowly to indicate that 
// the cycling process is 'on' and the motor will begin to force cleaning solution 
// through the tubing into the rig. When the rig fills all the way, the excess 
// solution will empty back into the cleaning solution reservoir for 
// recirculation. If the startStop button is pressed once again at any point 
// during the recirculation cycle, the motor will reverse flow in order to empty 
// the cleaning solution back into into the cleaning solution reservoir as much as 
// possible(Depending on rig design, there will be a certain amount of residual 
// cleaning solution left in the rig that the user will need to finally empty back 
// into the cleaning solution reservoir). If the cycle is able to complete, the green
// LED will rapidly blink until user presses the startStop button to complete
// the cycling program to an end. 

//things to do  -calculate time it takes to fill about 500ml at motorHI value
//              -wire up the trinket MO, the motor controller, the motor, the button&LED, and the shutoff switch

 
//pin variable names
const int cycleStartStop = 0;  // variable used to represent star and stop button input pin
const int greenLED = 1;        //variable used to represent the green LED pin
const int emrgnzShutOff = 2;   //variable use to represent the emergency shut off pin
const int motorOut1 =3; // variable used to represent motor control output pin 1
const int motorOut2 = 4; // variable used to represent motor control output pin 2

// variables for value storage
const int motorHI = 128;  // variable used to store motor high speed PWM value
const int motorLOW = 25;  // variable used to store motor low speed PWM value
const int motorOFF = 0;  // variable used to store motor shutoff speed PWM value
int motorPWM;           // variable used to store motor PWM values during cycles
int motorPWMfade = 5;       //variable used to store the motor PWM fade increment number
int cycleEndIndic = 0;    // variable used to indicate if the END cycle is ON or OFF
int cycleStartIndic = 0;     // variable used to indicate if the cycle is ON or OFF
int greenPWM;       // variable to store the PWM value to send to light LED with
int greenLEDfade = 5;   //variable used to store the LED PWM fade increment number
int emptyDuration = 500;   //variable used to store the value used to change the duration of the stop cycle in the stop loop counter(500cycles of 500ms = 250sec)
int cycleShortCount;     //variable used to store the end count value when pump hasn't ran longer than the time it takes to empty the rig
int cycleCount;          //variable used to store the end count value

//custom function used to call for activating emergency shutoff feature
void emrgnzShutOffFunction() { 
    digitalWrite( motorOut1, motorOFF); //motor1 output turns OFF
    digitalWrite( motorOut2, motorOFF); //motor2 output turns OFF
        //green LED to blink 3 times quickly, then to stay off for 0.75 seconds
        analogWrite( greenLED, 255);    //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(100);                     // delay of 0.1 seconds
        analogWrite( greenLED, 255);    //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(100);                     // delay of 0.1 seconds
        analogWrite( greenLED, 255);   //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(750);                     // delay of 0.75 seconds
         
}
void setup() {

  pinMode(cycleStartStop, INPUT); // initialize this pin as an input
  pinMode(greenLED, OUTPUT);  // initialize this pin as an output for LED PWM control
  pinMode(motorOut1, OUTPUT); // initialize this pin as an output for motor PWM control
  pinMode(motorOut2, OUTPUT); // initialize this pin as an output for motor PWM control
  pinMode(emrgnzShutOff, INPUT);   // initialize this pin as an input for emergency shutoff control
  
}
void loop() {

//    EMERGENCY SHUTOFF SEQUENCE BELOW
        //when triggered by the shutoff switch, the motors will shut off and the LED will flash three times continuously until
        //the shutoff switch is returned to its off position(upright)

  if(emrgnzShutOff == 1 ) {              
    emrgnzShutOffFunction();
    
  }   

  // CYCLE START LOOP
  if(cycleStartStop == 1 && emrgnzShutOff == 0) {       //begins or resumes START of cleaning cycle  IF button is pressed AND the emergency shutoff is NOT on.
      cycleStartIndic = 1; 

      //MOTOR CONTROL (FORWARD)
      digitalWrite(motorOut2, LOW);                        //turn motorOut2 OFF
      analogWrite(motorOut1, motorPWM);                    //turn motorOut1 ON using the motorPWM value
      motorPWM = motorPWM + motorPWMfade;                  //increment the motorPWM value by the motorPWMfade value
      if(motorPWM == motorLOW || motorPWM == motorHI) {   //when motor PWM value reaches its HI or LOW limit, the fade value flips
        motorPWMfade = -motorPWMfade;                    //flips the motor PWM fade value from negative to postive and vice versa
      }

      //LED CONTROL
      analogWrite(greenLED, greenPWM);             //turn green LED on
      greenPWM = greenPWM + greenLEDfade;          //increment greenPWM value by green LED fade value
      if(greenPWM == 0 || greenPWM == 255) {       //when PWM value reaches 255 or 0 the fade value flips
          greenLEDfade = -greenLEDfade;            //flips the fade value from negative to positive and vice versa
      }

       //START CYCLE COUNTER
     if(cycleStartIndic == 1 && cycleEndIndic == 0) {
        for(int cycleStartCount = 0; cycleStartCount <=1000; cycleStartCount++) {   //this counter counts up until its limit is reach which triggers the END cycle counter
             if(cycleStartCount == 1000 || cycleStartStop == 0) {                    //when counter reaches its limit OR stop button is pressed, counter resets to zero and 
                                                                                    //then cycle start indicator remains on until stop loop resets all values upon completion
                 cycleEndIndic = 1;                                                  //cycle end indicator turns ON to trigger the stop loop further down
                 cycleCount = cycleStartCount;
              }
         }
       if(emrgnzShutOff == 1) {    //checks status of emergency shutoff switch
       emrgnzShutOffFunction();   //calls the emergency shut off function to shut down motors
       }
       delay(500);                //999 cycles with 500ms delays adds up to around 500seconds(8.3minutes) of total run time of the start cycle
     }
 }
// CYCLE STOP LOOP  
        
        //begins the END of cleaning cycle when cycleStartStop button is turned back OFF, AND the start and stop Indicators are ON, AND the emergency shutoff is OFF
       if(cycleEndIndic == 1 && cycleStartIndic == 1 && emrgnzShutOff == 0) { 

      //MOTOR CONTROL (REVERSE)
      digitalWrite(motorOut1, LOW);                        //turn motorOut1 OFF
      analogWrite(motorOut2, motorHI);                    //turn motorOut2 ON using the motorPWM value

      //LED CONTROL
      analogWrite(greenLED, 255);             //turn green LED on
      delay(750);                              //0.75 second delay
      analogWrite(greenLED, 0);             //turn green LED off
      delay(750);                             //0.75 second delay
      
                // below line runs if cleaning cycle has just began and there is no need to empty a rig as much because it isn't completely full
                //if duration of cycleSTART(loop) is less than emptyDuration value, then duration of cycleEND(loop) is equal to cycleCount value(equal to cycleStartCount)

      if(cycleCount <= emptyDuration) {
        cycleShortCount = cycleCount;
            //increment cycleEndShortCount until it reaches cycleStartCount, then shutoff motors and LED, and then reset values for new cleaning cycle
            for(int cycleEndShortCount = 0; cycleEndShortCount <= cycleShortCount; cycleEndShortCount++) {
              if(cycleEndShortCount == cycleShortCount) {
                  digitalWrite( motorOut1, motorOFF);   //motor1 output turns OFF
                  digitalWrite( motorOut2, motorOFF);   //motor2 output turns OFF
                  analogWrite(greenLED, LOW);           //LED turns OFF
                  cycleStartIndic = 0;                  //resets cycleStartIndic value to 0
                  cycleEndIndic = 0;                    //resets cycleEndIndic value to 0
                  cycleCount = 0;                       //resets cycleCount value to 0
              }
            }
      delay(500);
      }
      for(int cycleEndCount = 0; cycleEndCount <= emptyDuration; cycleEndCount++ ){
           //increment cycleEndCount until it reaches emptyDuration value, then shutoff motors and LED, AND reset values for new cycle
            for(int cycleEndCount = 0; cycleEndCount <= emptyDuration; cycleEndCount++) {
              if(cycleEndCount == emptyDuration) {
                  digitalWrite( motorOut1, motorOFF);    //motor1 output turns OFF
                  digitalWrite( motorOut2, motorOFF);     //motor2 output turns OFF
                  analogWrite(greenLED, LOW);           //turns LED OFF
                  cycleStartIndic = 0;                  //resets cycleStartIndic value to 0
                  cycleEndIndic = 0;                    //resets cycleEndIndic value to 0
                  cycleCount = 0;                       //resets cycleCount value to 0
            }
      }

              if(emrgnzShutOff == 1) {       //checks status of emergency shutoff switch
              emrgnzShutOffFunction();    //calls the emergency shut off function to shut down motors
      }
      delay(500);
      }
    }
}

On many Arduino boards, pins 0 and 1 are the hardware serial pins, needed for code upload and communication.

Your best troubleshooting tool is the serial port, I suggest using some serial prints to monitor program flow and changes in the variable values.

What Arduino board?

Use the IDE autoformat tool to indent he code for easier readability.

A schematic or wiring diagram could be a big help as could some clear photos of the wiring.

Hello AsaBrown

Welcome back.

What makes you so unhappy with your programme?

Hello and thank you!
Im not quite unhappy with the program, I was just seeing if another pair of eyes could see any improvements as to how I could improve this code, or even if they think something is missing. It compiles.. but I havent tested it on my components yet.

Feedback! I see you use pin 2 to indicate emergency stop, but!!!!! Your program NEVER reads that pin.

oh wow.. ha! See stuff like that! :laughing: :sweat_smile:
I will definitely fix that. I could read the pin just before the if statement that runs the shutoff function.. right? I assume that would be a simple placement.

I also realised that I need to wait to reset the values at the end of the program until the startstop button is actually in the off position. The program could finish before the user actually presses stop, and I didnt code that in there yet! Oops!

Thanks for the feedback!

You need to also read the pin at the beginning of setup() in case someone is trying to stop the program when it begins.

Your feedback is VERY appreciated!

Its the Adafruit Trinket M0, Grothen G928 12VDC peristaltic pump/motor, a SPDT toggle switch, an on/off button with a green LED ring, and an Adafruit DRV8871 DC motor driver.

I was planning on just running it with no serial monitor and hope the program flowed properly as it ran. I was going to wire it all up and test/troubleshoot the shutoff and start/stop if it didnt run right, and then tune values from there when I'm able to test it on a vessel with the cleaning solution. Im currently looking for silicon tubing that I can use and a couple other things but Im just about there I hope! I will see if I can post the wire diagram soon.

The serial port troubleshooting will definitely happen if it comes down to it.. I have an Arduino mega that could help me out with that because the trinket M0 pins are all booked up!

If this is too low quality of a diagram, I understand.. lol I hope it helps though!

UPDATED CODE:
not edited for typos like "fluxuating"--> "fluctuating".. so basic.. :smiling_face_with_tear:

// This is a sketch for the purpose of running a peristaltic pump to cycle 
// cleaning solution through a glass vessel at fluxuating flow rates. When the 
// StartStop button is pressed(after the user safely hooks up their rig to 
// the cycling tubes), the green LED will begin pulsing slowly to indicate that 
// the cycling process is 'on' and the motor will begin to force cleaning solution 
// through the tubing into the rig. When the rig fills all the way, the excess 
// solution will empty back into the cleaning solution reservior for 
// recirculation. If the startStop button is pressed once again at any point 
// during the recirculation cycle, the motor will reverse flow in order to empty 
// the cleaning solution back into into the cleaning solution reservior as much as 
// possible(Depending on rig design, there will be a certain ammount of residual 
// cleaning solution left in the rig that the user will need to finally empty back 
// into the cleaning solution reservior). If the cycle is able to complete, the green
// LED will rapidly blink until user presses the startStop button to complete
// the cycling program to an end. 

//things to do  -calculate time it takes to fill about 500ml at motorHI value
//              -wire up the trinket MO, the motor controller, the motor, the button&LED, and the shutoff switch

 
//pin variable names
const int cycleStartStop = 0;  // variable used to represent start and stop button input pin
const int greenLED = 1;        //variable used to represent the green LED pin
const int emrgnzShutOff = 2;   //variable use to represent the emergency shut off pin
const int motorOut1 =3;       // variable used to represent motor control output pin 1
const int motorOut2 = 4;      // variable used to represent motor control output pin 2

// variables for value storage
const int motorHI = 128;      // variable used to store motor high speed PWM value
const int motorLOW = 25;      // variable used to store motor low speed PWM value
const int motorOFF = 0;       // variable used to store motor shutoff speed PWM value
int motorPWM = 125;           // variable used to store motor PWM values during cycles
int motorPWMfade = 5;       //variable used to store the motor PWM fade increment number
int greenPWM;       // variable to store the PWM value to send to light LED with
int greenLEDfade = 5;   //variable used to store the LED PWM fade increment number
int emptyDuration = 500;   //variable used to store the value used to change the duration of the stop cycle in the stop loop counter(500cycles of 500ms = 250sec)

//variables that will reset at end of stop cycle, allows program to start again 
int cycleShortCount = 0;     //variable used to store the end count value when pump hasnt ran longer than the time it takes to empty the rig
int cycleCount = 0;          //variable used to store the end count value
int cycleFinish = 0;          //variable used to indicate that the stop cycle has completed
int cycleEndIndic = 0;    // variable used to indicate if the END cyle is ON or OFF
int cycleStartIndic = 0;     // variable used to indicate if the cycle is ON or OFF

//custom function used to call for activating emergency shutoff feature, and will stay called as long as shutoff switch is on
void emrgnzShutOffFunction() { 
    digitalWrite( motorOut1, motorOFF); //motor1 output turns OFF
    digitalWrite( motorOut2, motorOFF); //motor2 output turns OFF

        //green LED to blink 3 times quickly, then to stay off for 0.75 seconds
        analogWrite( greenLED, 255);    //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(100);                     // delay of 0.1 seconds
        analogWrite( greenLED, 255);    //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(100);                     // delay of 0.1 seconds
        analogWrite( greenLED, 255);   //green LED turns ON
        delay(200);                     // delay of 0.2 seconds
        analogWrite( greenLED, 0);      //green LED turns OFF
        delay(750);                     // delay of 0.75 seconds
         
}
void setup() {

  pinMode(cycleStartStop, INPUT);       // initialize this pin as an input
  pinMode(greenLED, OUTPUT);           // initialize this pin as an output for LED PWM control
  pinMode(motorOut1, OUTPUT);          // initialize this pin as an output for motor PWM control
  pinMode(motorOut2, OUTPUT);          // initialize this pin as an output for motor PWM control
  pinMode(emrgnzShutOff, INPUT);       // initialize this pin as an input for emergency shutoff control
  
}
void loop() {

//    EMERGENCY SHUTOFF SEQUENCE BELOW
        //when triggered by the shutoff switch, the motors will shut off and the LED will flash three times continuously until
        //the shutoff switch is returned to its off position(upright)
  if(digitalRead(emrgnzShutOff) == 1 ) {              
    emrgnzShutOffFunction();
    
  }   

// CYCLE START LOOP
  if(cycleStartStop == 1 && emrgnzShutOff == 0) {       //begins or resumes START of cleaning cycle  IF button is pressed AND the emergency shutoff is NOT on.
      cycleStartIndic = 1; 

      //MOTOR CONTROL (FORWARD)
      motorPWM = motorPWM + motorPWMfade;                  //increment the motorPWM value by the motorPWMfade value
      if(motorPWM == motorLOW || motorPWM == motorHI) {   //when motor PWM value reachs its HI or LOW limit, the fade value flips
        motorPWMfade = -motorPWMfade;                    //flips the motor PWM fade value from negative to postive and vice versa
      }
      digitalWrite(motorOut2, LOW);                        //turn motorOut2 OFF
      analogWrite(motorOut1, motorPWM);                    //turn motorOut1 ON using the motorPWM value


      //LED CONTROL
      analogWrite(greenLED, greenPWM);             //turn green LED on
      greenPWM = greenPWM + greenLEDfade;          //increment greenPWM value by green LED fade value
      if(greenPWM == 0 || greenPWM == 255) {       //when PWM value reaches 255 or 0 the fade value flips
          greenLEDfade = -greenLEDfade;            //flips the fade value from negative to positive and vice versa
      }

       //START CYCLE COUNTER
     if(cycleStartIndic == 1 && cycleEndIndic == 0) {
        for(int cycleStartCount = 0; cycleStartCount <=1000; cycleStartCount++) {   //this counter counts up until its limit is reach which triggers the END cycle counter
             if(cycleStartCount == 1000 || cycleStartStop == 0) {                    //when counter reaches its limit OR stop button is pressed, counter resets to zero and 
                                                                                    //then cycle start indicator remains on until stop loop resets all values upon completion
              cycleEndIndic = 1;                             //cycle end indicator turns ON to trigger the stop loop further down
              cycleCount = cycleStartCount;                   //cycleCount value is changed to the cycleStartCount value
              }
         }
       if(digitalRead(emrgnzShutOff) == 1) {    //checks status of emergency shutoff switch
       emrgnzShutOffFunction();   //calls the emergency shut off function to shut down motors
       }
       delay(500);                //999 cycles with 500ms delays adds up to around 500seconds(8.3minutes) of total run time of the start cycle
     }
 }
// CYCLE STOP LOOP  
        
      //begins the END of cleaning cycle when the start and stop Indicators are ON, AND the emergency shutoff is OFF
       if(cycleEndIndic == 1 && cycleStartIndic == 1 && emrgnzShutOff == 0) { 

      //MOTOR CONTROL (REVERSE)
      digitalWrite(motorOut1, LOW);                        //turn motorOut1 OFF
      analogWrite(motorOut2, motorHI);                    //turn motorOut2 ON using the motorPWM value

      //LED CONTROL
      analogWrite(greenLED, 255);             //turn green LED on
      delay(750);                              //0.75 second delay
      analogWrite(greenLED, 0);             //turn green LED off
      delay(750);                             //0.75 second delay
      
// below line runs if cleaning cycle has just began and there is no need to empty a rig as much because it isnt completely full
//if duration of cycleSTART(loop) is less than emptyDuration value, then duration of cycleEND(loop) is equal to cycleCount value(equal to cycleStartCount)
      if(cycleCount <= emptyDuration) {
        cycleShortCount = cycleCount;
            //increment cycleEndShortCount until it reaches cycleStartCount, then shutoff motors and change cycleFinish value to HIGH
            for(int cycleEndShortCount = 0; cycleEndShortCount <= cycleShortCount; cycleEndShortCount++) {
              if(cycleEndShortCount == cycleShortCount) {
                  digitalWrite( motorOut1, motorOFF);   //motor1 output turns OFF
                  digitalWrite( motorOut2, motorOFF);   //motor2 output turns OFF
                  cycleFinish = 1;                   //turns cycleFinish value to HIGH
              }
            }
      delay(500);
      }

// When cleaning cycle is able to reach the end without being stopped by user, the motor runs for the normal ammount of time
      for(int cycleEndCount = 0; cycleEndCount <= emptyDuration; cycleEndCount++ ){
           //increment cycleEndCount until it reaches emptyDuration value, then shutoff motors and change cycleFinish value to HIGH
            for(int cycleEndCount = 0; cycleEndCount <= emptyDuration; cycleEndCount++) {
              if(cycleEndCount == emptyDuration) {  
                  digitalWrite( motorOut1, motorOFF);     //motor1 output turns OFF
                  digitalWrite( motorOut2, motorOFF);     //motor2 output turns OFF
                  cycleFinish = 1;                        //turns cycleFinish value to HIGH
            }
      }

              if(digitalRead(emrgnzShutOff) == 1) {       //checks status of emergency shutoff switch
              emrgnzShutOffFunction();                    //calls the emergency shut off function to shut down motors
      }
      delay(500);
      }
//after user has pressed the stop button AND the stop cycle has completed, program is ready to reset
      if(cycleStartStop == 0 && cycleStartIndic == 1 && cycleEndIndic == 1 && cycleFinish == 1){
                  cycleStartIndic = 0;                  //resets cycleStartIndic value to 0
                  cycleEndIndic = 0;                    //resets cycleEndIndic value to 0
                  cycleCount = 0;                       //resets cycleCount value to 0
                  cycleFinish = 0;                      //resets cycleFinish value to 0
                  cycleShortCount = 0;                  //resets cycleShortCount value to 0
      }
   }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.