Hi everyone, my code here is currently running but with issue.
The timing of my first two or three cycles is within parameters I need, but it slowly gets out of timing and the cycle then gets all over the place.
If there's some way I can zero out the Millis at the end of each cycle, or another fix, please let me know!
Thanks!
#include <LiquidCrystal_I2C.h>
// Pin definitions
const int relay1 = 53; // Valve 4, Exhuast
const int relay2 = 51; // Valve 3, Pressurize
const int relay3 = 49; // Valve 2, Slide Movement
const int relay4 = 47; // Valve 1, Chuck Movement
const int upLim = 52; // define limit switch pin and name
const int relay5 = 45; //reset
// Global variables
int cycleCount = 0;
int Switchcount = 0; //limit switch counter
int relay1state = LOW;
int relay2state = LOW;
int relay3state = LOW;
int relay4state = LOW;
int relay5state = LOW;
//time
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis5 = 0;
unsigned long intervalOn1 = 4000;
unsigned long intervalOff1 = 17000;
unsigned long interval1 = 14000;
unsigned long intervalOn2 = 3000;
unsigned long intervalOff2 = 17000;
unsigned long interval2 = 4000;
unsigned long intervalOn3 = 3000;
unsigned long intervalOff3 = 17000;
unsigned long interval3 = 0;
unsigned long intervalOn4 = 2000;
unsigned long intervalOff4 = 8000;
unsigned long interval4 = 0;
unsigned long intervalOn5 = 1000;
unsigned long intervalOff5 = 1000;
unsigned long interval5 = 20000;
int X = 0;
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on}
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW);
pinMode(relay3, OUTPUT);
digitalWrite(relay3, LOW);
pinMode(relay4, OUTPUT);
digitalWrite(relay4, LOW);
pinMode(52, INPUT_PULLUP); // defining pin 42 as an input for the upper limit switch. When this is closed the lift will lower
}
void loop() {
if (cycleCount < 150000){
while(digitalRead(52) == HIGH) { // while the upper limit switch is not pressed
lcd.setCursor(0,0);
lcd.print("Cycle Test Running");
lcd.setCursor(0,3);
lcd.print("Switch Failure: ");
lcd.setCursor(0,1);
lcd.print("Cycle Count: ");
lcd.setCursor(0,2);
lcd.print("Pressure Failure: ");
unsigned long currentMillis5 = millis();
unsigned long currentMillis4 = millis();
unsigned long currentMillis3 = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis1 = millis();
//Chuck movement
if (currentMillis4 - previousMillis4 >= interval4) {
previousMillis4 = currentMillis4;
if (relay4state == LOW) {
relay4state = HIGH;
interval4 = intervalOn4;
} else {
relay4state = LOW;
interval4 = intervalOff4;
}
digitalWrite(relay4, relay4state);
}
//Platform Movement
if (currentMillis3 - previousMillis3 >= interval3) {
previousMillis3 = currentMillis3;
if (relay3state == LOW) {
relay3state = HIGH;
interval3 = intervalOn3;
} else {
relay3state = LOW;
interval3 = intervalOff3;
}
digitalWrite(relay3, relay3state);
}
//Pressurize
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
if (relay2state == LOW) {
relay2state = HIGH;
interval2 = intervalOn3;
} else {
relay2state = LOW;
interval2 = intervalOff2;
}
digitalWrite(relay2, relay2state);
}
//Exhuast
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
if (relay1state == LOW) {
relay1state = HIGH;
interval1 = intervalOn1;
} else {
relay1state = LOW;
interval1 = intervalOff1;
}
digitalWrite(relay1, relay1state);
cycleCount ++;
lcd.setCursor(14,1);
lcd.print(cycleCount);
}
if(digitalRead(52) == LOW) {
Switchcount ++;
lcd.setCursor(17,3);
lcd.print(Switchcount);
}
}
}
if (cycleCount >150000) {
lcd.setCursor(0,1);
lcd.print("Cycle Test Finished!");
exit(0);
}
}