digitalWriting to an INPUT is correct?

Hello,

I am trying to wipe EEPROM if a button pressed while powering on Arduino and here is my code looks like; my approach is correct? before digitalWriting to INPUT It wipes when power cycled whether button pressed or not. After it works like expected. Is there any better way?

  pinMode(wipeB, INPUT); 
  digitalWrite(wipeB,LOW); // Make sure wipeB Pin gets LOW ??? Why it gets high when power cycled??
  wipe = digitalRead(wipeB); // Write pin state to variable
void setup() {
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH); // Make sure door is locked
  
  //Protocol Configuration
  Serial.begin(9600);	 // Initialize serial communications with PC
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  
  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  int wipe = 0;  // variable integer to keep if wipe button pressed
  pinMode(wipeB, INPUT); 
  digitalWrite(wipeB,LOW); // Make sure wipeB Pin gets LOW ??? Why it gets high when power cycled??
  wipe = digitalRead(wipeB); // Write pin state to variable
 //delay(???); do i need to delay?
  if (wipe == HIGH) {     
    wipeModeOn();   // Red Blue Led flashes then Red Led stays on to inform user we are going to wipe 
    Serial.println("!!! Wipe Button Pressed !!!");
    Serial.println("You have 5 seconds to Cancel");
    Serial.println("This will be remove all records and cannot be undone");
    delay(5000);    // Give user enough time to cancel operation
    Serial.println("!!! Starting Wiping EEPROM !!!");
    for (int i = 0; i < 1024; i++) { // Loop repeats equal to the number of array in EEPROM
      EEPROM.write(i, 0);
    }
    wipe = 0;
    Serial.println("!!! Wiped !!!");
    digitalWrite(redLed, LOW);  
  }
  //Ready
  Serial.println("##### RFID Door Unlocker #####");
  Serial.println("");
  Serial.println("Waiting PICCs to bo scanned :)");
}

void wipeModeOn()
{
  digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  delay(50);
  digitalWrite(redLed, LED_ON); // Make sure blue LED is on 
  digitalWrite(blueLed, LED_OFF); // Make sure green LED is off
  delay(200);
  digitalWrite(redLed, LED_OFF); // Make sure blue LED is off 
  digitalWrite(blueLed, LED_ON); // Make sure green LED is on
  delay(200);
  digitalWrite(redLed, LED_ON); // Make sure blue LED is on 
  digitalWrite(blueLed, LED_OFF); // Make sure green LED is off
  delay(200);
  digitalWrite(redLed, LED_OFF); // Make sure blue LED is off 
  digitalWrite(blueLed, LED_ON); // Make sure green LED is on
  delay(200);
  digitalWrite(redLed, LED_ON); // Make sure blue LED is off 
  digitalWrite(blueLed, LED_OFF); // Make sure green LED is on
  delay(200);
}
  digitalWrite(wipeB,LOW); // Make sure wipeB Pin gets LOW ??? Why it gets high when power cycled??

Look at the documentation for the digitalWrite() function. Pay particular attention to what it does for an INPUT pin.

Writing to an INPUT pin can be useful, when you know what it does.

The normal state of a digital input pin is HIGH. You should wipe the EEPROM when the digital input goes LOW. It might also help if you set the internal pullup as well.

pinMode(wipeB, INPUT_PULLUP);

Pete

How is the switch wired? It looks like it is active HIGH (HIGH when pressed). It may be better to wire the switch to ground and use pinMode(pin, INPUT_PULLUP) (or pinMode(pin, INPUT) and digitalWrite(pin, HIGH))to enable the internal pull up resistor. That way the input is pulled up at startup. The button pin will read LOW when pressed.

Thank you,

When input pullup resistor enabled, can i directly connect button to GND?

void setup() {
  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM

  int wipe = 0;  // variable integer to keep if wipe button pressed

  pinMode(3, INPUT_PULLUP); // enable pin's as INPUT enable pull-up resistor

  wipe = digitalRead(3); //Check if button pressed (button goes to ground directly)

  if (wipe == LOW) {     
    delay(5000);    // Give user enough time to cancel operation
    for (int i = 0; i < 1024; i++) { // Loop repeats equal to the number of array in EEPROM
      EEPROM.write(i, 0);
    }
  }
}

Like below. Leave out the 10K resistor if internal pullup enabled. The cap is a good way to debounce most switches.

Thank you all.

Now it works like a charm. I understood every code line.

void setup() {
  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM

  int wipe = 0;  // variable integer to keep if wipe button pressed

  pinMode(3, INPUT_PULLUP); // enable pin's as INPUT enable pull-up resistor

  wipe = digitalRead(3); //Check if button pressed (button goes to ground directly)

  if (wipe == LOW) {     
    delay(5000);    // Give user enough time to cancel operation
    for (int i = 0; i < 1024; i++) { // Loop repeats equal to the number of array in EEPROM
      EEPROM.write(i, 0);
    }
  }
}

Can combine these even:

wipe = digitalRead(3); //Check if button pressed (button goes to ground directly)

  if (wipe == LOW) {

into

  if (digitalRead(3) == LOW) {//Check if button pressed (button goes to ground directly)

Then add an EEPROM read, and only change a location to 0 if it is not 0 already. Every write takes 3.3mS, why not save a bunch of time when you can?

if (wipe == LOW) {     
    delay(5000);    // Give user enough time to cancel operation
    for (int i = 0; i < 1024; i++) { // Loop repeats equal to the number of array in EEPROM
      EEPROM.write(i, 0);

The 5 second delay in your code does not allow the user to cancel the erase. You enter the if condition, do nothing for 5 seconds and then proceed with the erase. You will need another digitalRead of the button.

 if (wipe == LOW) {     
    delay(5000);    // Give user enough time to cancel operation
    wipe=digitalRead(3);
    if(wipe==LOW){
      for (int i = 0; i < 1024; i++) { // Loop repeats equal to the number of array in EEPROM
        EEPROM.write(i, 0);
      }
    }
  }

@CrossRoads oh i see, i dont even need to store a variable integer

@cattledog and thank you for pointing a better way. My taught was stupid user was need to power of device, now this is more clever way.

@CrossRoads what you mean with that?

Then add an EEPROM read, and only change a location to 0 if it is not 0 already. Every write takes 3.3mS, why not save a bunch of time when you can?

This is what I mean - test the contents, don’t write it if it’s already 0.

if (digitalRead(3) == LOW) {//Check if button pressed (button goes to ground directly)
  for (x=0; x<1024; x=x+1){
  if (EEPROM.read(x) == 0){
  // do nothing, already clear, go to the next address
  } 
  else{
  EEPROM.write(x, 0); // write takes 3.3mS
  }
} // next address

Oh thank you. Now ı understood. This also helps to reduce writes to EEPROM which is great. EEPROM has limited write cycle

for those who wants to Wipe EEPROM at setup level this is the code:

#include <EEPROM.h>

#define wipeB 3 // Define wipe button pin 

void setup() {
  //Protocol Configuration
  Serial.begin(9600);	 // Initialize serial communications with PC
  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  pinMode(wipeB, INPUT_PULLUP);  // Enable pin's pull up resistor
  if (digitalRead(wipeB) == LOW) {     // when button pressed pin should get low, pin goes GND with button press
    Serial.println("!!! Wipe Button Pressed !!!");
    Serial.println("You have 5 seconds to Cancel");
    Serial.println("This will be remove all records and cannot be undone");
    delay(5000);    // Give user enough time (5 sec) to cancel operation simply dont press button anymore
    if (digitalRead(wipeB) == LOW) {  // If button still pressed, wipe that EEPROM out
      Serial.println("!!! Starting Wiping EEPROM !!!");
      for (int x=0; x<1024; x=x+1){
        if (EEPROM.read(x) == 0){
          // do nothing, already clear, go to the next address
        }
        else{
          EEPROM.write(x, 0); // write takes 3.3mS
        }
        Serial.println("!!! Wiped !!!");
      }
    }
    else {
      Serial.println("!!! Operation Cancelled !!!");
    }
  }
}

void loop () {
}

Exactly.