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 ![]()
// 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);
}
}
}
