I want to start a relay, have it run for 3 seconds, stop for 10 seconds, repeat.
I can't use delay, because I have a motor driver starting / stopping every 100 milis.
So I put together this code, but it does not work. Any help appreciated.
There's a 500ms delay to stop too much debugging info.
Also for some reason, tenSecondsBool switches to true as soon as you run the code.
unsigned long threeSeconds=3000; // the time we need to wait
unsigned long threeSecondsMillis=0; // millis() returns an unsigned long.
unsigned long fourSeconds=7000; // the time we need to wait
unsigned long fourSecondsMillis=0; // millis() returns an unsigned long.
unsigned long previousMillis=0; // millis() returns an unsigned long.
bool ledState = false; // state variable for the LED
bool led2State = false; // state variable for the LED
unsigned long tenSecondMilis=0; // millis() returns an unsigned long.
unsigned long tenSeconds=10000; // the time we need to wait
unsigned long paliGasiMilis=0; // millis() returns an unsigned long.
unsigned long paliGasi=1000; // the time we need to wait
unsigned long ventMilis=0; // millis() returns an unsigned long.
unsigned long ventInt=100; // the time we need to wait
bool threeSecondBool = false;
bool tenSecondBool = false;
int Fin1 = D7;
#define enA D1
#define in1 D2
#define in2 D3
#define button 4
int rotDirection = 0;
int pressed = false;
void setup() {
Serial.begin(9600); // send and receive at 9600 baud
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
pinMode(Fin1, OUTPUT);
digitalWrite(Fin1, HIGH);
threeSecondBool = false;
tenSecondBool = false;
}
void loop() {
unsigned long currentMillis = millis(); // grab current time
if ((unsigned long)(currentMillis - tenSecondMilis) >= tenSeconds) {
Serial.print("---------------\n");
Serial.print("10 second end\n");
Serial.print("---------------\n");
tenSecondBool = true;
tenSecondMilis = millis();
}
Serial.print("10 second bool = ");
Serial.print(tenSecondBool);
Serial.print("\n");
Serial.print("3 second bool = ");
Serial.print(threeSecondBool);
Serial.print("\n");
if (tenSecondBool) {
if ((unsigned long)(currentMillis - threeSecondsMillis) >= threeSeconds) {
Serial.print("---------------\n");
Serial.print("3 second end\n");
Serial.print("---------------\n");
threeSecondBool = true;
threeSecondsMillis = millis();
}
}
if (!tenSecondBool && threeSecondBool){
Serial.print("Both triggered!\n");
digitalWrite(Fin1, HIGH);
}
if (!tenSecondBool && !threeSecondBool){
Serial.print("Both triggered!\n");
digitalWrite(Fin1, HIGH);
}
if (tenSecondBool && threeSecondBool){
threeSecondBool = false;
tenSecondBool = false;
digitalWrite(Fin1, LOW);
}
delay(500);
if ((unsigned long)(currentMillis - ventMilis) >= ventInt) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
ventMilis = millis();
} else {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
}