Here is a code-version that uses two very useful macros for serial-debug-output
This version makes visible what your code does
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
const unsigned long eventInterval1 = 10000;
//EVENT 1 Motor going Down for 10 sec
const unsigned long eventInterval2 = 20000;
//EVENT 2 Motor going up for 10 sec
const unsigned long eventInterval3 = 30000;
//EVENT 3 Motor is off (Holding Position) for 10 sec
const unsigned long eventInterval4 = 40000;
//EVENT 4 Motor is going up for 10 sec
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_4 = 0;
int motor2pin1 = 4;
int motor2pin2 = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Setup-Start");
pinMode(motor2pin1, OUTPUT);// Motor
pinMode(motor2pin2, OUTPUT);//Motor
pinMode(9, OUTPUT); // Enable pin motor speed regulation
}
void loop(){
unsigned long currentTime = millis();
// put your main code here, to run repeatedly:
dbgi("Top of loop currentTime - previousTime_1",(currentTime - previousTime_1)/1000,2000);
dbgi("Top of loop currentTime - previousTime_2",(currentTime - previousTime_2)/1000,2000);
dbgi("Top of loop currentTime - previousTime_3",(currentTime - previousTime_3)/1000,2000);
dbgi("Top of loop currentTime - previousTime_4",(currentTime - previousTime_4)/1000,2000);
analogWrite(9, 240); //speed regulation
if (currentTime - previousTime_1 >= eventInterval1) { //EVENT1
//Motor is going up witth sensor
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
dbg("TRUE eventInterval1, HIGH/LOW",1);
previousTime_1 = currentTime;
}
if (currentTime - previousTime_2 >= eventInterval2) {
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
dbg("TRUE eventInterval2, LOW/HIGH",2);
previousTime_2 = currentTime;
}
if (currentTime - previousTime_3 >= eventInterval3) {
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
dbg("TRUE eventInterval3, LOW/LOW",3);
previousTime_3 = currentTime;
}
if (currentTime - previousTime_4 >= eventInterval4) {
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
dbg("TRUE eventInterval4, LOW/HIGH",4);
previousTime_4 = currentTime;
}
}
I guess that you want the things to happen in sequence
const unsigned long eventInterval1 = 10000;
//EVENT 1 Motor going Down for 10 sec
const unsigned long eventInterval2 = 20000;
//EVENT 2 Motor going up for 10 sec
const unsigned long eventInterval3 = 30000;
//EVENT 3 Motor is off (Holding Position) for 10 sec
const unsigned long eventInterval4 = 40000;
//EVENT 4 Motor is going up for 10 sec
This can be programmed with a software-functionality called state-machine.
State-machines reduce the number of if-conditions you have to check.
If you want to happen the things in sequence
then every 10 seconds the state-machine is switched to the next state
best regards Stefan