I am working on a code, which is running a function once a second:
switch (gearbox1) {
case 1:
//manually close
Serial.println("Closing1");
digitalWrite(gear1OpenPin, OFF);
t.setTimer(1000, Close1, 1);
break;
case 2:
//manually open
Serial.println("Opening1");
digitalWrite(gear1ClosePin, OFF);
t.setTimer(1000, Open1, 1);
break;
This part is working fine. But then i want at case 10: call function autoClose()
which would work in 10 second cycles.
Struggle is, as my first function is called once a second, it is starting function autoClose()
once a second as well. How can I overcome this please?
At the moment I have current, non working code:
case 10:
//automatic close
Serial.println("A Close1");
digitalWrite(gear1ClosePin, OFF);
digitalWrite(gear1OpenPin, OFF);
t.enable(autoClose);
autoClose();
break;
Edit: Added minimal possible code. I want autoOpen()
and autoClose()
to loop in 10 sec. intervals.
#include <SimpleTimer.h>
SimpleTimer t;
int gearbox1;
#define gear1OpenPin 44
#define gear1ClosePin 46
int OFF = HIGH; // states for relays
int ON = LOW;
byte gear1Open = 0;
byte gear1Close = 0;
void setup() {
t.setInterval(1000, Control1);
int autoOpen = t.setInterval(10000, autoOpen);
int autoClose = t.setInterval(10000, autoClose);
}
void loop() {
t.run();
}
void Control1() {
//*****************************************
switch (gearbox1) {
case 1:
//manually close
Serial.println("Closing");
digitalWrite(gear1OpenPin, OFF);
t.setTimer(1000, Close1, 1);
break;
case 2:
//manually open
// Serial.println("Opening");
digitalWrite(gear1ClosePin, OFF);
t.setTimer(1000, Open1, 1);
break;
case 5:
//stop
Serial.println("Stopping");
digitalWrite(gear1ClosePin, OFF);
digitalWrite(gear1OpenPin, OFF);
break;
case 10:
//automatic close
digitalWrite(gear1ClosePin, OFF);
digitalWrite(gear1OpenPin, OFF);
if (t.isEnabled(autoClose)) {
autoClose();
} else {
t.restartTimer(autoClose) ; t.enable(autoClose);
autoClose();
}
break;
case 20:
//automatic open
digitalWrite(gear1ClosePin, OFF);
digitalWrite(gear1OpenPin, OFF);
t.restartTimer(autoOpen); t.enable(autoOpen);
autoOpen();
break;
}
}
void Open1() {
digitalWrite(gear1OpenPin, ON);
}
//**********************************************
void Close1() {
digitalWrite(gear1ClosePin, ON);
}
//**********************************************
void autoOpen() {
if (gear1Open == ON) {
gear1Open = OFF;
} else {
gear1Open = ON;
}
digitalWrite(gear1OpenPin, gear1Open);
}
//**********************************************
void autoClose() {
if (gear1Close == ON) {
gear1Close = OFF;
} else {
gear1Close = ON;
}
digitalWrite(gear1ClosePin, gear1Close);
}
Auto()
function being called at all, yet you state it is starting once per second. Show all your code, or at least a complete working example that demonstrates the problem.