/* ESP32 = Output HIGH to switch Relay on
* Setup
Set an interval
* Loop
after interval
choose a random time
switch relay on for the random time
switch relay off after random time
check interval remaining
repeat random time
**
20230915 Random time ON for a set time interval works
Step through time array works
need a 'run for time'
need a stop function after time finished
*/
//Define pins
#define RelayPin 23
// define pin states
bool RelayPin_state = false;
// Define variables
int i =0;
// Define time variables
unsigned long setInterval = 8000;
unsigned long LoopInterval;
unsigned long timeNow;
unsigned long timeInterval;
unsigned long timeRemaining;
unsigned long timeOn;
unsigned long timerLoop;
unsigned long resultNextLoop = (timeNow - timeInterval);
unsigned long resultNextRelaySwitch = (timeNow - (timerLoop + timeOn));
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.println(RelayPin_state);
//Set pin modes
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
//set start timers
LoopInterval = setInterval;
}
void loop() {
Serial.println("************");
timeNow = millis();
Serial.print("timeNow = "); Serial.println(timeNow);
resultNextLoop = timeNow -(timeNow - timerLoop);
Serial.print("NextLoop = "); Serial.println(resultNextLoop);
resultNextRelaySwitch = (timeOn);
Serial.print("time Interval = "); Serial.println(timeInterval);
Serial.print("resultNextRelaySwitch = "); Serial.println(timeNow - timeInterval);
Serial.println("///////");
// after relay being on for Time On, switch relay off
if ((timeNow - timeInterval) > timeOn)
{
switchRelayOff();
//stepTime();
}
//after interval switch relay on for a choosen time
if ((timeNow - timerLoop) > LoopInterval)
{ Serial.println("Choose time");
//chooseTimeOn();
stepTime();
}
}
// step through and increase time on intervals
void stepTime()
{
bool stepTimeStarted;
if (!stepTimeStarted){int i = 0;}
stepTimeStarted = true;
int TimeSlotsArray[5] = {2, 3, 4, 5, 6}; // 2, 3, 4, 5, 6
int Timeslot = TimeSlotsArray[i];
i++;
timeOn = Timeslot * 1000 ;
Serial.print("index = ");
Serial.println(i);
Serial.print("stepTimeStarted = ");
Serial.println(stepTimeStarted);
switchRelayOn();
}
//choose a random time
void chooseTimeOn()
{ Serial.println("Random time");
int TimeSlotsArray[5] = {2, 3, 4, 5, 6}; // 2, 3, 4, 5, 6
int index = random(1,5);
int Timeslot = TimeSlotsArray[index];
timeOn = Timeslot * 1000 ;
timerLoop = millis();
Serial.println(timeOn);
// switch relay on for the random time
switchRelayOn();
}
// switch relay on for time and start relay on timer
void switchRelayOn()
{
Serial.println("Switch Relay ON");
digitalWrite(RelayPin, true);
timeInterval = millis();
timerLoop = millis();
Serial.println(RelayPin_state);
Serial.print("timeOn = ");
Serial.println(timeOn);
}
// switch relay off
void switchRelayOff()
{
Serial.println("Switch Relay OFF");
digitalWrite(RelayPin, false);
timeInterval = 0;
Serial.println(RelayPin_state);
Serial.print("timeOn = ");
Serial.println(timeOn);
}