A friend and I are using an Arduino UNO R3 and relay module to turn 4 serer computers on and off at certain times but we also want a way to know if there has been a power out. To help with this we wanted to add an LED and a Button so when the power went out and the Arduino turned back on, the LED would turn on but once we saw we could then press the button and the LED would turn off. I am currently having trouble adding this to the program could anyone please give me some advice on how to add this to the program.
you will need to understand millis() time. on the ide go to file-examples-digital-blink with out delay
play with that code until you understand how time stamps work
added the code to your original code. No idea what you are doing with this type of code but if it worked with delays it should work this way. Please read about INPUT PULLUP or add a pull down resistor to your button
led will come on after boot-up and will remain on until button is pressed. It will not come back on unless arduino restarts
left test code in the program as the delays are so large. delete the lines marked //test code after testing
use serial monitor to test
const int SI = 7;
const int SII = 8;
const int SIII = 12;
const int SIV = 4;
const int LED = 2;
const int BUTTON = 3;
byte reset = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
//unsigned long interval1 = 17748000;
//unsigned long interval2 = 21600000;
//unsigned long interval3 = 64800000;
unsigned long interval1 = 3000;//test code 3 seconds
unsigned long interval2 = 5000;//test code 5 seconds
unsigned long interval3 = 7000;//test code 7 seconds
bool oneTime_1 = 0;
bool oneTime_2 = 0;
void setup() {
Serial.begin(9600);//test code
pinMode(SI, OUTPUT);
pinMode(SII, OUTPUT);
pinMode(SIII, OUTPUT);
pinMode(SIV, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
digitalWrite(SI, HIGH);
digitalWrite(SII, HIGH);
digitalWrite(SIII, HIGH);
digitalWrite(SIV, HIGH);
digitalWrite(LED, HIGH);
}
void loop() {
unsigned long currentMillis = millis();
reset = digitalRead(BUTTON);
if (reset == HIGH) {
digitalWrite(LED, LOW);
}
if (currentMillis - previousMillis1 >= interval1) {
if (oneTime_1 == 0) {//flag so this only happens once
runPattern();//run the repeated pattern
previousMillis2 = currentMillis;//set the time stamp for next timer
oneTime_1 = 1;//set flag
}
if (currentMillis - previousMillis2 >= interval2) {
if (oneTime_2 == 0) {//flag so this only happens once
runPattern();//run the repeated pattern
previousMillis3 = currentMillis;//set the time stamp for next timer
oneTime_2 = 1;//set flag
}
if (currentMillis - previousMillis3 >= interval3) {
previousMillis1 = currentMillis;//set the time stamp for next timer
runPattern();//run the repeated pattern
oneTime_1 = 0;//unset flag
oneTime_2 = 0;//unset flag
}
}
}
}
void runPattern() {
Serial.print(" running pattern ");//test code
Serial.println(millis());//test code
digitalWrite(SI, LOW);
delay(200);
digitalWrite(SI, HIGH);
delay(200);
digitalWrite(SII, LOW);
delay(200);
digitalWrite(SII, HIGH);
delay(200);
digitalWrite(SIII, LOW);
delay(200);
digitalWrite(SIII, HIGH);
delay(200);
digitalWrite(SIV, LOW);
delay(200);
digitalWrite(SIV, HIGH);
}
went to delete the code I wrote and I figured that I might as well remove all the delays and make it even more complicated by adding another millis timer
now instead of the button possible not working for 1.4 seconds it will work instantly
const int SI = 7;
const int SII = 8;
const int SIII = 12;
const int SIV = 4;
const int LED = 2;
const int BUTTON = 3;
byte reset = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
//unsigned long interval1 = 17748000;
//unsigned long interval2 = 21600000;
//unsigned long interval3 = 64800000;
unsigned long interval1 = 3000;//test code 3 seconds
unsigned long interval2 = 5000;//test code 5 seconds
unsigned long interval3 = 7000;//test code 7 seconds
bool runPattern=0;
bool oneTime_1 = 0;
bool oneTime_2 = 0;
int stages=0;
void setup() {
Serial.begin(9600);//test code
pinMode(SI, OUTPUT);
pinMode(SII, OUTPUT);
pinMode(SIII, OUTPUT);
pinMode(SIV, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
digitalWrite(SI, HIGH);
digitalWrite(SII, HIGH);
digitalWrite(SIII, HIGH);
digitalWrite(SIV, HIGH);
digitalWrite(LED, HIGH);
}
void loop() {
unsigned long currentMillis = millis();
reset = digitalRead(BUTTON);
if (reset == HIGH) {
digitalWrite(LED, LOW);
}
if (currentMillis - previousMillis1 >= interval1) {
if (oneTime_1 == 0) {//flag so this only happens once
runPattern = 1; //run the repeated pattern
previousMillis4 = currentMillis;
previousMillis2 = currentMillis;//set the time stamp for next timer
oneTime_1 = 1;//set flag
}
if (currentMillis - previousMillis2 >= interval2) {
if (oneTime_2 == 0) {//flag so this only happens once
runPattern = 1; //run the repeated pattern
previousMillis4 = currentMillis;
previousMillis3 = currentMillis;//set the time stamp for next timer
oneTime_2 = 1;//set flag
}
if (currentMillis - previousMillis3 >= interval3) {
previousMillis1 = currentMillis;//set the time stamp for next timer
runPattern = 1; //run the repeated pattern
previousMillis4 = currentMillis;
oneTime_1 = 0;//unset flag
oneTime_2 = 0;//unset flag
}
}
}
if (runPattern == 1) {
if (currentMillis - previousMillis4 >= 200) {
stages++;
previousMillis4 = currentMillis;
}
switch (stages) {
case 0:
Serial.println("run 0");//test code
digitalWrite(SI, LOW);
break;
case 1:
Serial.println("run 1");//test code
digitalWrite(SI, HIGH);
break;
case 2:
Serial.println("run 2");//test code
digitalWrite(SII, LOW);
break;
case 3:
Serial.println("run 3");//test code
digitalWrite(SII, HIGH);
break;
case 4:
Serial.println("run 4");//test code
digitalWrite(SIII, LOW);
break;
case 5:
Serial.println("run 5");//test code
digitalWrite(SIII, HIGH);
break;
case 6:
Serial.println("run 6");//test code
digitalWrite(SIV, LOW);
break;
case 7:
Serial.println("run 7");//test code
digitalWrite(SIV, HIGH);
runPattern = 0;
stages = 0;
break;
default :
stages = 0;
runPattern = 0;
break;
}
}
}
ahh so im wondering if the first long interval only runs once. This interval is only meant to run once to allw us to turn the server on at 6:00pm instead of 5:00am.
johns code should do what you need unless you plan on adding more code to do different tasks later. also check your maths on the first delay like john points out its kinda odd