I am new to this so please bear with me. I am trying to run several different delays in my code to give time for machinery to move into place etc... I am concerned that my use of multiple delay variables will not work as the code keeps resetting the current time based on example code I found. Thoughts? Sorry about the length - I'm not even sure if it acceptable to post this much code... like I said, new to this.
This is for a machine that de-stacks buckets at one station and then adds water at the next. Homemade in my garage.
// Define pins
const int sensewaterstn = 3; // Sensor pin (IR sensor, water station)
const int sensedrop = 4; // Sensor pin (IR sensor, bucket drop)
const int alarm = 6; // Alarm condition (light stalk) red wire / relay 7
const int drive = 7; // Motor pin (for conveyor belt stop/start) orange wire / relay 6
const int hold = 8; // Stop plates for bucket pile / relay 5
const int pawl = 9; // Air valve pin (bucket stack pawls) / relay 4
const int gate = 10; // Air valve pin (Bucket exit gate) / relay 3
const int waterfill = 11; // Water valve pin (water fill station) / relay 1
const int jets = 12; // Air valve pin (air jets) / relay 2
// delay code
unsigned long DELAY_TIME5 = 5000; // 5 second delay
unsigned long DELAY_TIME35 = 3500; // 3.5 second delay
unsigned long DELAY_TIME3 = 3000; // 3 second delay
unsigned long DELAY_TIME1 = 1000; // 1 second delay
unsigned long previousWaterstn = 0; // stores last time bucket present at water station
unsigned long previousDrop = 0; // stores last time bucket detected at drop location
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize sensor pins as input
pinMode(sensedrop, INPUT); // set pins to input
digitalWrite(sensedrop, HIGH); // turn on pullup resistors (means grounding pin completes circuit, connect to + side of sensor)
pinMode(sensewaterstn, INPUT);
digitalWrite(sensewaterstn, HIGH);
// Initialize output pins as outputs
pinMode(drive, OUTPUT); // set pins to output
digitalWrite(drive, HIGH); // set initial voltage to LOW or OFF NOTE: connect terminals 1 to 4 on Leeson ctrl to START drive - break conncection to STOP
pinMode(gate, OUTPUT);
digitalWrite(gate, HIGH);
pinMode(hold, OUTPUT);
digitalWrite(hold, HIGH);
pinMode(pawl, OUTPUT);
digitalWrite(pawl, LOW);
pinMode(jets, OUTPUT);
digitalWrite(jets, LOW);
pinMode(waterfill, OUTPUT);
digitalWrite(waterfill, LOW);
}
void loop() {
unsigned long currentWaterstn = millis(); //save current time for water station
unsigned long currentDrop = millis(); //save current time for bucket drop
//Water station
if (digitalRead(sensewaterstn) == LOW) { //bucket present at water station and waiting for fill
digitalWrite(drive, LOW); //turn off conveyor
digitalWrite(waterfill, HIGH); //open fill valve - trigger off-delay relay (analog manual adjust OFF delay simplifies operation)
// check if delay has timed out for initiating water valve
if (currentWaterstn - previousWaterstn >= DELAY_TIME1) { // 1 second delay timer
previousWaterstn = currentWaterstn; // this saves when the water valve last initiated
digitalWrite(waterfill, LOW); // turn OFF water - analog OFF delay relay will continue to dose water till done
} else {
digitalWrite(waterfill, HIGH); //leave water valve ON
}
// check if delay has timed out water valve to finish filling
if (currentWaterstn - previousWaterstn >= DELAY_TIME5) { // 5 second delay timer (maximum time alotted for analog delay timer to fill bucket)
previousWaterstn = currentWaterstn; //copy current time to memory
digitalWrite(waterfill, LOW); // JUST IN CASE - turn OFF water - 5 second overall alotment of time for fill
digitalWrite(drive, HIGH); //turn conveyor back on
}
} else {
digitalWrite(waterfill, LOW); //turn off water
digitalWrite(drive, LOW); //turn off conveyor
}
// Bucket drop detection
if (digitalRead(sensedrop) == HIGH) { //no bucket present
digitalWrite(gate, HIGH); //close exit gate
digitalWrite(pawl, HIGH); //engage pawls to hold stack of buckets
//delay
if (currentDrop - previousDrop >= DELAY_TIME1) { // 1 second delay timer togive time for pawls to engage before opening bottom holding tabs
previousDrop = currentDrop; // this saves when bucket drop last initiated
digitalWrite(hold, LOW); // retract holding tabs - freeing last bucket for drop
} else {
digitalWrite(hold, HIGH); //leave holding tabs engaged - this is to give pawls time to deploy and prevent entire bucket pile from falling
}
//delay
if (currentDrop - previousDrop >= DELAY_TIME3) { // 3 second delay timer
previousDrop = currentDrop; // this saves when bucket drop last initiated
digitalWrite(jets, LOW); // stop air jets
digitalWrite(hold, HIGH); //re-engage holding tabs under bucket pile
} else {
digitalWrite(jets, HIGH); //leave air jets on to give time for bucket dislodging
digitalWrite(hold, HIGH); //re-engage holding tabs - again for insurance
}
//delay
if (currentDrop - previousDrop >= DELAY_TIME35) { // 3.5 second delay timer
previousDrop = currentDrop; // this saves when bucket drop last initiated
digitalWrite(jets, LOW); // stop air jets
digitalWrite(pawl, LOW); //dis-engage pawls from bucket pile
} else {
digitalWrite(pawl, HIGH); //leave pawls engaged to give time for holding tabs to engage first
}
}
}

