Need help in creating peristaltic pump library

I have created an library for my peristaltic pump without delay, the peristaltic pump starts but doesn't stops.
main code --

#include <PeristalticPump.h>

PeristalticPump pump1(3,5,4);//pul,step,dir
 void setup() {
    pump1.PinSetup();
    pump1.pumpstart(50,1);
 }

 void loop() {
    
 }
 

cpp file code --


#include "Arduino.h"
#include "PeristalticPump.h"

int pulse;
int direct;
int step;

unsigned long intervalPump;
unsigned long msecPump;
unsigned long msec;

PeristalticPump::PeristalticPump(int ppin1, int ppin2, int ppin3){
   
           pulse=ppin1;
           step=ppin2;
           direct=ppin3;
          }

void PeristalticPump::PinSetup() {
            pinMode(pulse,OUTPUT);
            pinMode(step,OUTPUT);
            pinMode(direct,OUTPUT);
            }

void PeristalticPump::pumpdelay()
            {
                if (HIGH == digitalRead (direct) && (msec - msecPump) >= intervalPump)  {
                    digitalWrite (step,   LOW);
                    digitalWrite (direct, LOW);
                    analogWrite  (pulse,  0);
                }
            }

void PeristalticPump::pumpstart(float pwm,int interval) 
            {
                msec = millis ();
                intervalPump = interval*1000;
                pumpdelay();
                analogWrite(pulse, pwm);  
                digitalWrite(step, HIGH);          
                digitalWrite(direct, HIGH);  
                msecPump = msec;
            }

header file code --

#ifndef PeristalticPump_h
#define PeristalticPump_h
#include "Arduino.h"

class PeristalticPump {

        private:
          
         int ppin1;
         int ppin2;
         int ppin3;
        
         public:
      
           PeristalticPump(int ppin1, int ppin2, int ppin3);
           void PinSetup();
           void pumpdelay();
           void pumpstart(float pwm,int interval);

};

#endif

You need a function in the library that checks whether the pump is running and, if so, whether the required period has elapsed and, if so, stop the pump

That function, often called update(), must be called frequently, usually every iteration of the loop() function

could you please help me with it, I tried creating one but no results.

Please post what you tried

Think also about those variables:Some might actually be instance related and not just global variables, may be other don’t need to be global at all….

looking at your library, IMHO, you need to add the "pumpdelay()" routine withing loop to check the the duration has expired ie:

#include "PeristalticPump.h"

PeristalticPump pump1(3,5,4);//pul,step,dir
 void setup() {
    pump1.PinSetup();
    pump1.pumpstart(50,1);
 }

 void loop() {
    pump1.pumpdelay();
 }

hope that helps...

PS this is not the kind or "without delay" operation you want, you may want to look into using Timer interrupts to start/stop your pump, independently of your "loop" code. :wink:

no its not working

think I found the bug...

Try this sketch if you may. its essentially your 'corrected' library, without the library :wink:
(compiles, NOT tested!)

//#include <PeristalticPump.h>
int pulse;
int direct;
int step;

unsigned long intervalPump;
unsigned long msec;

//PeristalticPump pump1(3, 5, 4); //pul,step,dir

void setup() {
  //pump1.PinSetup();
  pulse = 3;
  step = 5;
  direct = 4;

  //pump1.pumpstart(50,1);
  int interval = 1;
  int pwm = 50;
  //pumpstart(50,1);
  intervalPump = interval * 1000;
  analogWrite(pulse, pwm);
  digitalWrite(step, HIGH);
  digitalWrite(direct, HIGH);
  msec = millis ();
}

void loop() {
  //pumpdelay()
  if (HIGH == digitalRead (direct) && (millis() - msec) >= intervalPump)  { //<---- use millis here
    digitalWrite (step,   LOW);
    digitalWrite (direct, LOW);
    analogWrite  (pulse,  0);
  }
}

hope that helps...

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