Background about the project:
I am making a automatic egg incubator tray. for that i am using 2 limit switches, 1 L298n motor driver and arduino leonardo. I am making the egg tray in a way that it would tilt 45 degrees on both sides. and i want to introduce a delay off time in this code. so that whenever the tray touches the switch it turns off the motor for one hour, reverse the direction and repeat
Main Question:
I have programmed it in such a way that it stops for a certain amount of before it goes in the other direction. that works. well kinda. i have found out that if i set the interval time to 10 seconds and let the motor run for about 5 mins and then press the switch to reverse it. the off time is very different than when i let the motor run for 1 min and then reverse it.
CODE:
If you want to visit the link and check the code out. it's your choice. i don't know if this is allowed. if not let me know and i will delete the link.
unsigned long currentMillis;
unsigned long previousMillis;
const long onInterval = 10000;
const byte in1 = 3;
const byte in2 = 4;
const byte sw1 = 6; //this is left switch
const byte sw2 = 5; //this is right switch
boolean sw1Status;
boolean sw2Status;
int stateMachine;
void setup() {
// put your setup code here, to run once:
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
Serial.begin(9600);
millis();
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis();
sw1Status = digitalRead(sw1);
sw2Status = digitalRead(sw2);
Serial.println("This is another process");
switch (stateMachine) {
case 0: //motor stop/ reset
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
if(sw1Status == HIGH && sw2Status == LOW) //tray is tilted to the left
{
stateMachine = 1;
} else if (sw1Status == LOW && sw2Status == HIGH) { //try is tilted to the right
stateMachine = 2;
}
break; //erase the break see what happens
case 1: //go right
if(sw2Status == LOW) { //go right until the right switch is high
if(currentMillis - previousMillis >= onInterval) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
previousMillis = currentMillis;
} else if (sw2Status == HIGH ) {
stateMachine = 0; // go to reset
}
} else if (sw2Status == HIGH) {
stateMachine = 0; // go to reset
}
break;
case 2: //go left
if(sw1Status == LOW) { //go left until it touches left switch
if(currentMillis - previousMillis >= onInterval) {
digitalWrite(in1, LOW); //go right mechanism
digitalWrite(in2, HIGH);
previousMillis = currentMillis;
} else if (sw1Status == HIGH) { //if it touches left switch
stateMachine = 0; //go to reset
}
} else if (sw1Status == HIGH) {
stateMachine = 0;
}
break;
}