I am using the millis function to switch 2 sensors on/off using MOSFET switches which is working great.
The idea is:
Switch both sensors OFF (Setup)
In Loop, I want to run:
Switch Sensor 1 ON for 5 seconds - I then do what I need and switch all sensors off.
Switch Sensor 2 ON for 7 seconds - I then do what I need and switch all sensors off.
Using the millis example, my timing is not coming in correctly.
The first 60 seconds display in Serial Monitor:
Time: 5s - Sensor 1 ON
Time: 7s - Sensor 2 ON
Time: 10s - Sensor 1 ON
Time: 14s - Sensor 2 ON
Time: 15s - Sensor 1 ON
Time: 20s - Sensor 1 ON
Time: 21s - Sensor 2 ON
Time: 25s - Sensor 1 ON
Time: 28s - Sensor 2 ON
Time: 30s - Sensor 1 ON
Time: 35s - Sensor 1 ON
Time: 35s - Sensor 2 ON
Time: 40s - Sensor 1 ON
Time: 42s - Sensor 2 ON
Time: 45s - Sensor 1 ON
Time: 49s - Sensor 2 ON
Time: 50s - Sensor 1 ON
Time: 55s - Sensor 1 ON
Time: 56s - Sensor 2 ON
Time: 60s - Sensor 1 ON
Time: 63s - Sensor 2 ON
My Sketch:
#define INTERVAL_MESSAGE1 5000
#define INTERVAL_MESSAGE2 7000
unsigned long time_1 = 0;
unsigned long time_2 = 0;
void print_time(unsigned long time_millis);
void setup() {
Serial.begin(9600);
pinMode(6, OUTPUT);// Sensor 1
pinMode(7, OUTPUT);// Sensor 2
digitalWrite(6, HIGH);// Sensor 1 OFF
digitalWrite(7, HIGH);// Sensor 2 OFF
}
void loop() {
if (millis() > time_1 + INTERVAL_MESSAGE1) {
time_1 = millis();
print_time(time_1);
Sensor1ON();
Serial.println("Sensor 1 ON");
}
if (millis() > time_2 + INTERVAL_MESSAGE2) {
time_2 = millis();
print_time(time_2);
Sensor2ON();
Serial.println("Sensor 2 ON");
}
}
void print_time(unsigned long time_millis) {
Serial.print("Time: ");
Serial.print(time_millis / 1000);
Serial.print("s - ");
}
void Sensor1OFF() {
digitalWrite(6, HIGH);// Sensor 1 OFF
digitalWrite(7, HIGH);// Sensor 2 OFF
}
void Sensor1ON() {
digitalWrite(6, LOW);// Sensor 1 ON
digitalWrite(7, HIGH);// Sensor 2 OFF
}
void Sensor2OFF() {
digitalWrite(6, HIGH);// Sensor 1 OFF
digitalWrite(7, HIGH);// Sensor 2 OFF
}
void Sensor2ON() {
digitalWrite(6, HIGH);// Sensor 1 ON
digitalWrite(7, LOW);// Sensor 2 OFF
}
Time: 0s - Sensor 1 ON
Time: 5s - Sensor 1 OFF Sensor 2 ON
Time: 12s - Sensor 2 OFF Sensor 1 ON
Time: 17s - Sensor 1 OFF Sensor 2 ON
Time: 24s - Sensor 2 OFF Sensor 1 ON
Time: 29s - Sensor 1 OFF Sensor 2 ON
Time: 36s - Sensor 2 OFF Sensor 1 ON
Time: 41s - Sensor 1 OFF Sensor 2 ON
Time: 48s - Sensor 2 OFF Sensor 1 ON
Time: 53s - Sensor 1 OFF Sensor 2 ON
Time: 60s - Sensor 2 OFF Sensor 1 ON
Time: 65s - Sensor 1 OFF Sensor 2 ON
Time: 72s - Sensor 2 OFF Sensor 1 ON
Time: 77s - Sensor 1 OFF Sensor 2 ON
Time: 84s - Sensor 2 OFF Sensor 1 ON
Time: 89s - Sensor 1 OFF Sensor 2 ON
Time: 96s - Sensor 2 OFF Sensor 1 ON
Time: 101s - Sensor 1 OFF Sensor 2 ON
Time: 108s - Sensor 2 OFF Sensor 1 ON
Time: 113s - Sensor 1 OFF Sensor 2 ON
Time: 120s - Sensor 2 OFF Sensor 1 ON
Time: 125s - Sensor 1 OFF Sensor 2 ON
Time: 132s - Sensor 2 OFF Sensor 1 ON
Time: 137s - Sensor 1 OFF Sensor 2 ON
Time: 144s - Sensor 2 OFF Sensor 1 ON
Time: 149s - Sensor 1 OFF Sensor 2 ON
Time: 156s - Sensor 2 OFF Sensor 1 ON
Time: 161s - Sensor 1 OFF Sensor 2 ON
Time: 168s - Sensor 2 OFF Sensor 1 ON
Time: 173s - Sensor 1 OFF Sensor 2 ON
Time: 180s - Sensor 2 OFF Sensor 1 ON
Time: 185s - Sensor 1 OFF Sensor 2 ON
Time: 192s - Sensor 2 OFF Sensor 1 ON
Time: 197s - Sensor 1 OFF Sensor 2 ON
Time: 204s - Sensor 2 OFF Sensor 1 ON
Time: 209s - Sensor 1 OFF Sensor 2 ON
Time: 216s - Sensor 2 OFF Sensor 1 ON
Time: 221s - Sensor 1 OFF Sensor 2 ON
Time: 228s - Sensor 2 OFF Sensor 1 ON
Time: 233s - Sensor 1 OFF Sensor 2 ON
Time: 240s - Sensor 2 OFF Sensor 1 ON
Time: 245s - Sensor 1 OFF Sensor 2 ON
Time: 252s - Sensor 2 OFF Sensor 1 ON
Time: 257s - Sensor 1 OFF Sensor 2 ON
Time: 264s - Sensor 2 OFF Sensor 1 ON
Time: 269s - Sensor 1 OFF Sensor 2 ON
Time: 276s - Sensor 2 OFF Sensor 1 ON
Time: 281s - Sensor 1 OFF Sensor 2 ON
Time: 288s - Sensor 2 OFF Sensor 1 ON
Time: 293s - Sensor 1 OFF Sensor 2 ON
Time: 300s - Sensor 2 OFF Sensor 1 ON
Time: 305s - Sensor 1 OFF Sensor 2 ON
Time: 312s - Sensor 2 OFF Sensor 1 ON
Time: 317s - Sensor 1 OFF Sensor 2 ON
Time: 324s - Sensor 2 OFF Sensor 1 ON
Time: 329s - Sensor 1 OFF Sensor 2 ON
Time: 336s - Sensor 2 OFF Sensor 1 ON
Time: 341s - Sensor 1 OFF Sensor 2 ON
Time: 348s - Sensor 2 OFF Sensor 1 ON
Time: 353s - Sensor 1 OFF Sensor 2 ON
Time: 360s - Sensor 2 OFF Sensor 1 ON
Time: 365s - Sensor 1 OFF Sensor 2 ON
Time: 372s - Sensor 2 OFF Sensor 1 ON
Time: 377s - Sensor 1 OFF Sensor 2 ON
Time: 384s - Sensor 2 OFF Sensor 1 ON
Time: 389s - Sensor 1 OFF Sensor 2 ON
Time: 396s - Sensor 2 OFF Sensor 1 ON
Time: 401s - Sensor 1 OFF Sensor 2 ON
Time: 408s - Sensor 2 OFF Sensor 1 ON
Time: 413s - Sensor 1 OFF Sensor 2 ON
Time: 420s - Sensor 2 OFF Sensor 1 ON
Time: 425s - Sensor 1 OFF Sensor 2 ON
Time: 432s - Sensor 2 OFF Sensor 1 ON
Time: 437s - Sensor 1 OFF Sensor 2 ON
Time: 444s - Sensor 2 OFF Sensor 1 ON
Time: 449s - Sensor 1 OFF Sensor 2 ON