I am working on a larger project but in an attempt to better wrap my mind around what it is I'm doing (coding wise) and help me help myself down the line I am doing the logical thing of breaking it down to constituent components. What I am working on will have two loops that run independent of one another so I've naturally gravitated to the "SeveralThings..." post for guidance.
The first loop (where I am working currently) consists of two peristaltic pumps and two gate valves.
The second loop also consists of two peristaltic pumps and four gate valves.
(Some background: Pump1 is offloading dirty fluid through GV1 or returning clean enough fluid to container through GV2. Pump2 will replace lost volume when fluid is being offloaded.)
What I have thus far compiles but where I'm getting bogged down is I need to run 'pump1' every 30 minutes. 2 minutes into its run time take an optical density reading of the fluid in the line then proceed with an if/else statement. The IF/ELSE being IF the optical density reading is less than the threshold, stop running. ELSE keep running and take density readings every 2 minutes. What I am seeing is once I start receiving density readings, they are being printed every time through the loop and not every 2 minutes.
A: Where am I going wrong on the density readings every time through the loop?
B: There is an error on the IF/ELSE that I cannot put my finger on--(sarcasm font: massively helpful on my end).
Guidance would be greatly appreciated.
(For myself, it was easier to go ahead and include the constants I know I'll eventually be using and then add them to the loops as I get to their respective parts of the code)
// ------ LIBRARIES ------
#include <SPI.h>
#include <SD.h>
#include <Arduino.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
RTC_Millis rtc;
// ------ CONSTANTANTS (won't change--PIN #s) ------
const int RTCgroundpin = 19; // RTC Ground Pin
// *** OD sensor *** //
const int ODSensorVCC=51;
const int ODLogicOutA=47;
const int ODLogicOutB=48;
const int ODCalibrate=49;
const int ODAnalogIN=A14;
int val = 0;
const int ODSensorGND=50;
const int ODSensorPreviousMillis = 0;
const int ODSensorWaitTime = 1200000; // 2 minutes
int OD_HIGH_threshold = 250; // SUBJECT TO CHANGE
// *** GATEVALVES *** //
const int GV1=29;
const int GV2=27;
const int GV3=24;
const int GV4=25;
const int GV5=23;
const int GV6=22;
// *** PUMPS *** //
// *** VCC power to motor drivers *** //
const int MotorDriver1 = 52;
const int MotorDriver1STBY = 36;
const int MotorDriver2 = 53;
const int MotorDriver2STBY = 37;
const int P1A=35;
const int P1B=33;
const int P1PWM=8;
const int P2A=39;
const int P2B=41;
const int P2PWM=45;
const int P3A=38;
const int P3B=40;
const int P3PWM=46;
const int P4A=34;
const int P4B=32;
const int P4PWM=7;
// *** PUMP RUN TIMES *** //
const int ODpump1INTERVAL = 1800000; // 30 min
const int ODpump1RunTime1 = 120000; // 2 min
const int ODpump1RunTime2 = 300000; // 5 min
const int ODpump1RunTime3 = 600000; // 10 min
const int ODpump2RunTime1 = 120000; // 2 min
const int ODpump2RunTime2 = 300000; // 5 min
const int ODpump2RunTime3 = 600000; // 10 min
const int ByProductpump3RunTime1 = 120000; // 2 min
const int ByProductpump3RunTime2 = 300000; // 5 min
const int ByProductpump3RunTime3 = 600000; // 10 min
const int ByProductpump4RunTime1 = 120000; // 2 min
const int ByProductpump4RunTime2 = 300000; // 5 min
const int ByProductpump4RunTime3 = 600000; // 10 min
const int chipSelect = 4; // delete?
const int rs = 12, en = 11, d4 = 5, d5 = 6, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// ------ VARIABLES (will change) ------
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop
unsigned long PreviousPump1Millis = 0; // stores last time pump_ was updated
unsigned long PreviousPump2Millis = 0;
unsigned long PreviousPump3Millis = 0;
unsigned long PreviousPump4Millis = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Starting OD LOOP");
pinMode (RTCgroundpin, OUTPUT);
// ***OD Sensor *** //
pinMode (ODSensorVCC, OUTPUT);
pinMode (ODLogicOutA, OUTPUT);
pinMode (ODLogicOutB, OUTPUT);
pinMode (ODCalibrate, OUTPUT);
pinMode (ODAnalogIN, INPUT);
pinMode (ODSensorGND, OUTPUT);
// *** PUMPS *** //
pinMode (MotorDriver1, OUTPUT);
pinMode (MotorDriver1STBY, OUTPUT);
pinMode (MotorDriver2, OUTPUT);
pinMode (MotorDriver2STBY, OUTPUT);
pinMode (P1A, OUTPUT);
pinMode (P1B, OUTPUT);
pinMode (P1PWM, OUTPUT);
pinMode (P2A, OUTPUT);
pinMode (P2B, OUTPUT);
pinMode (P2PWM, OUTPUT);
pinMode (P3A, OUTPUT);
pinMode (P3B, OUTPUT);
pinMode (P3PWM, OUTPUT);
pinMode (P4A, OUTPUT);
pinMode (P4B, OUTPUT);
pinMode (P4PWM, OUTPUT);
// *** GATEVALVES *** //
pinMode(GV1, OUTPUT);
pinMode(GV2, OUTPUT);
pinMode(GV3, OUTPUT);
pinMode(GV4, OUTPUT);
pinMode(GV5, OUTPUT);
pinMode(GV6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (MotorDriver1, HIGH);
digitalWrite (MotorDriver1STBY, HIGH);
digitalWrite (MotorDriver2, HIGH);
digitalWrite (MotorDriver2STBY, HIGH);
digitalWrite (ODSensorVCC, HIGH);
digitalWrite (RTCgroundpin, LOW);
digitalWrite (ODSensorGND, LOW);
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
digitalWrite (GV1, LOW); // OPEN
digitalWrite (P1A, LOW); // CLOSED
digitalWrite (P1B, HIGH);
analogWrite (P1PWM, 200);
ODpump1RunTime1;
if (currentMillis - PreviousPump1Millis >= ODpump1INTERVAL) {
// Turn Pump1 ON, Open GateValve2
digitalWrite (GV1, HIGH); // CLOSED
digitalWrite (GV2, HIGH); // OPEN
digitalWrite (P1A, LOW); // Motor Counter Clockwise
digitalWrite (P1B, HIGH);
analogWrite (P1PWM, 200);
if (currentMillis - ODSensorPreviousMillis >= ODSensorWaitTime ) {
val = analogRead (ODAnalogIN);
Serial.println (val);
}
PreviousPump1Millis >= ODpump1INTERVAL;
if (ODAnalogIN < OD_HIGH_threshold) {
digitalWrite (P1A, LOW); // PUMP 1 OFF
digitalWrite (P1B, LOW);
analogWrite (P1PWM, 0);
digitalWrite (GV1, HIGH); // CLOSED
digitalWrite (GV2, LOW); // CLOSED
}
else {
ODpump1RunTime1;
digitalWrite (GV1, LOW); // OPEN --OFFLOADING dirty fluid
digitalWrite (GV2, LOW); // CLOSED
digitalWrite (P1A, LOW);
digitalWrite (P1B, HIGH);
analogWrite (P1PWM, 200);
digitalWrite (P1A, LOW);
digitalWrite (P1B, HIGH);
analogWrite (P2PWM, 200);
if (currentMillis - ODSensorPreviousMillis >= ODSensorWaitTime)
val = analogRead (ODAnalogIN);
Serial.println (val);
}
}
}