Help with INPUT button

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.

Here is the code I currently have running:

int SI = 7;
int SII = 8;
int SIII = 12;
int SIV = 4;
int LED = 2;
int BUTTON = 3;
void setup(){
pinMode(SI, OUTPUT);
pinMode(SII, OUTPUT);
pinMode(SIII, OUTPUT);
pinMode(SIV, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
delay(200);
digitalWrite(SI, HIGH);
digitalWrite(SII, HIGH);
digitalWrite(SIII, HIGH);
digitalWrite(SIV, HIGH);
digitalRead(BUTTON)
delay(17748000);
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);
}
void loop(){
delay(21600000);
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);
delay(64800000);
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);
}
void powerout(){
if (BUTTON == HIGH){
digitalWrite(LED, LOW);
}
}

digitalRead(BUTTON)
delay(17748000);

This won't compile because you are missing the ';' at the end of the first line.

Also, reading a pin and doing nothing with the value doesn't accomplish anything.

The huge delays mean you may have to hold the button down for 18 hours before the LED can react.

You should change all the delays longer than 200 mS to be loops that count milliseconds. In those loops you can check for the button press.

johnwasser:
The huge delays mean you may have to hold the button down for 18 hours before the LED can react.

You should change all the delays longer than 200 mS to be loops that count milliseconds. In those loops you can check for the button press.

So what would be the best way to do this could you show me an example please as you can see I am quite new at this.

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;
    }
  }
}

Thanks for the help I am going to play around with the blink without delays example to help me understand.

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.

this would really do better with a rtc (real time clock)

ok so if im understanding you correctly the first interval will only run once then the next two will loop

so interval 1
then interval 2
then interval 3
then interval 2
and repeat?

If you have nothing better to do than check the input button while delaying you can use a function to do both:

const unsigned long HOUR = 3600000UL; // 3,600,000 milliseconds in an hour

const int SI = 7;
const int SII = 8;
const int SIII = 12;
const int SIV = 4;
const int LED = 2;
const int BUTTON = 3;

// Use this in place of all the delays to check the input button while delaying
void checkButton(unsigned long delayTime) {
  unsigned long start = millis();
  while (millis() - start < delayTime)
    if (digitalRead(BUTTON) == HIGH)
      digitalWrite(LED, LOW);
}

void setup() {
  pinMode(SI, OUTPUT);
  pinMode(SII, OUTPUT);
  pinMode(SIII, OUTPUT);
  pinMode(SIV, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);  // Indicate a power failure until the button is pressed
  pinMode(BUTTON, INPUT);
  
  checkButton(200);
  digitalWrite(SI, HIGH);
  digitalWrite(SII, HIGH);
  digitalWrite(SIII, HIGH);
  digitalWrite(SIV, HIGH);
  checkButton(HOUR*5 - 252000UL);  // 5 hours minus 252 seconds
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
}


void loop() {
  checkButton(HOUR*6);
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
  checkButton(HOUR*18);
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
}

Why is the first big delay "5 hours minus 252 seconds"?!?

gpop1:
this would really do better with a rtc (real time clock)

ok so if im understanding you correctly the first interval will only run once then the next two will loop

so interval 1
then interval 2
then interval 3
then interval 2
and repeat?

Yes correct

Ross2354:
Yes correct

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

Thanks for eveything I am going to upload this today and test it.
Thanks

Oops... The LED should be set HIGH in setup():

const unsigned long HOUR = 3600000UL; // 3,600,000 milliseconds in an hour

const int SI = 7;
const int SII = 8;
const int SIII = 12;
const int SIV = 4;
const int LED = 2;
const int BUTTON = 3;

// Use this in place of all the delays to check the input button while delaying
void checkButton(unsigned long delayTime) {
  unsigned long start = millis();
  while (millis() - start < delayTime)
    if (digitalRead(BUTTON) == HIGH)
      digitalWrite(LED, LOW);
}

void setup() {
  pinMode(SI, OUTPUT);
  pinMode(SII, OUTPUT);
  pinMode(SIII, OUTPUT);
  pinMode(SIV, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);  // Indicate a power failure until the button is pressed
  pinMode(BUTTON, INPUT);
  
  checkButton(200);
  digitalWrite(SI, HIGH);
  digitalWrite(SII, HIGH);
  digitalWrite(SIII, HIGH);
  digitalWrite(SIV, HIGH);
  checkButton(HOUR*5 - 252000UL);  // 5 hours minus 252 seconds
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
}


void loop() {
  checkButton(HOUR*6);
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
  checkButton(HOUR*18);
  digitalWrite(SI, LOW);
  checkButton(200);
  digitalWrite(SI, HIGH);
  checkButton(200);
  digitalWrite(SII, LOW);
  checkButton(200);
  digitalWrite(SII, HIGH);
  checkButton(200);
  digitalWrite(SIII, LOW);
  checkButton(200);
  digitalWrite(SIII, HIGH);
  checkButton(200);
  digitalWrite(SIV, LOW);
  checkButton(200);
  digitalWrite(SIV, HIGH);
}