Calibrating a Peristaltic Pump

I have been building an 8 head peristaltic dosing pump system for some months now, and have used the below linked circuit (x8). I did add a 1k resistor between digital pins and base of each transistor for safety and omitted the pushbutton as I plan to instruct the unit via a network connection. The circuit works fine, but I am now trying to locate some sketch material so I can accurately calibrate the thing. I'd like to add a pushbutton to a breadboard and wire it so the Arduino can count off time between button presses while also turning a digital pin on and off. So when I press button, motor spins and millis() counts. When a measurable amount of fluid has been pumped into a measuring cup, the button is ticked again and the motor stops spinning and the millis() count is printed to serial. I could then use those numbers to define how many milliseconds it takes to produce 1 ml of fluid, and from there can update my project sketch and add the 8 head unit into my currently running hydroponics controller.

Any assistance, study leads, sketch snippets or just any information that can get me closer to my solution is appreciated in advance!

Arduino TIP120 Circuit

Here's a simple stopwatch sketch for UNO or Nano. Pushbutton from GND to pin 4, pin 9 to TIP120 base resistor.

uint32_t start, dbStart;
float eTime;
const byte btn = 4, led = 13, pumpPin = 9, dbTime = 20;
bool pinState = true,
     btnState = true,
     prevBtnState = true,
     timing = false;
void setup()
  Serial.println(F("  Press button to start timing\n"));


void loop()
  // debounce button ++++++++++++++++
  if (bitRead(PIND,btn) != pinState)  // get state of pin 4
    dbStart = millis(); // reset db timer
    pinState ^= 1;      // now they are equal, won't enter
  }                     // here again unless pin state changes
  if (millis() - dbStart > dbTime) // db timer has elapsed
    btnState = pinState;           // button state is valid 
  if(btnState != prevBtnState && !btnState) // btnState changed
    // if button pressed and NOT timing
      start = millis(); // reset stopwatch
      eTime = 0;        // reset elapsed time
      timing = true;    // prevents entry while timing
      Serial.println(F("  Press button to stop\n"));
      // if button pressed while timing,
      // stop, calculate, display
      eTime = (millis() - start) / 1000.0;
      timing = false;
      Serial.print(F("  Elapsed seconds = "));
      Serial.print(eTime,3); Serial.print("\t");
      Serial.println(F("  Press button to start timing\n"));
  prevBtnState = btnState;

EDIT: Added decimal place (line 51), removed redundant line 57.

while also turning a digital pin on and off.

Just on a point of terminology, pins are not "on" or "off", they're "high" or "low". Whether the "high" turns your device "on" or "off" is a different thing (and I know, is probably what you meant :wink: )

But consider an LED with the anode on a digital pin, cathode to ground. Then indeed, a "high" will turn the led "on". But if the anode was to 5V and the cathode on the i/o pin, then the led will be "on" when the pin is "low".

@756E6C, Thanks for the sketch, though I am unable to produce a favorable result from the pump and serial monitor. When in the loop, the motor spins endlessly and nothing is counted in the serial monitor. I wired the button according to the above linked tutorial (TIP120).

Prior to reading your sketch, I made a sketch of my own that also does not work, but hopefully it conveys my intentions and what I'm hoping to do. I want the arduino to acknowledge a button press that Writes a value of 255 to a pin and begins counting milliseconds. When the button is pressed again or depressed, that respective pin is set to 0 and the counter stops counting and prints the total amount of milliseconds to the serial monitor.

int button = 30;                    // Button connected to digital 30 with a 10k pulldown resistor to GND
int pump = 9;                       // pump on digital pin 9
unsigned long currentMillis = 0;    // stores the value of millis() 
unsigned long previousMillis = 0;   // will store last time the LED was updated
unsigned long totalMillis = 0;

//========== THE SETUP ==============================

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //default comm speed between arduino and serial monitor. no need to change.
  pinMode(button, INPUT);
  pinMode(pump, OUTPUT);

//========== THE LOOP ==============================

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button) == HIGH)
    previousMillis = millis();   // capture the beginning value of millis()
    analogWrite(pump, 255);

  else if (digitalRead(button) == LOW)
    currentMillis = millis();   // capture the ending value of millis()
    analogWrite(pump, 0);
  totalMillis = currentMillis - previousMillis;

pump is on a digital pin, but you analogWrite() to it?

pump is on a digital pin, but you analogWrite() to it?

Yes, on the Mega, pins 2-13 and 44-46 support PWM.