Digital pin 0 and 1 programming problem

After uploading the code via Arduino IDE, I disconnect USB cable and THEN i use battery power to the Arduno Uno board,

in this situation,

Is there any method, tips and tricks to use 0 and 1 pins connect with Buttons (pressed - High, not pressed LOW) without any interference (ARDUINO uno board)

it's essential requirement for my project, because all other pins already used except digital pin 0 and 1for the project,

waiting positive reply from experts.

Try connecting the pins to a N.O. switch connected to GND.

Turn on pull-ups on the pins.

Show us the sketch.

image

Hi,
Are you using the analog input pins on the UNO.
They can also be used as digital inputs.

pinMode(A0, INPUT);

Tom.... :smiley: :+1: :coffee: :australia:

take values HIGH and LOW properly, via these pin No 0, 1 and 2, but sometimes not take HIGH/LOW properly after POWER ON 3- 5 minutes later,

Hi,
Can you please post a circuit diagram so we can see if it is possible to add the extra inputs another way?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

this is circuit diagram, i have a code also, written by my friend,

my problem is in order to the wiring diagram, take get THREE sensor values HIGH and LOW properly, via these pin No 0, 1 and 2, but sometimes not take HIGH/LOW properly after POWER ON 3- 5 minutes later,

thank you.

The wiring of your 3 switches is odd and probably contributes to your problem, although I must say I find these Fritzy kind of drawings very confusing to read. A proper schematic is much better.
Also, you're using the same pins for sensors as well as buttons, which is likely to result in problems.

I would propose to use a GPIO expander like the MCP23017; this would solve your problem.

Hi,
As @anon35827816 said a port expander like MCP23017 just to control your LED indicators would solve your problem.
A proper schematic would also make your circuit easier to read, having labels covering wiring does not make the diagram very readable.

Hand draw a schematic, carefully and post an image, using proper symbols for switches, LEDs and power supply components.
Include component labels and pin names.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

if you have an account on Tinkercad account, you can see, code, circuit diagram also,

here is the link
Tinkercad

it is an Online Simulator, its working well, but the problem i said, working with physical circuit.

better if you could create an account, and see that link, please

thank you so much

Not really, it's created an unreadable schematic so far.

Please just do what @TomGeorge said above.

If the inputs are operating properly for several minutes when first powered on, then I would suspect that the problem is in the code, not the wiring or the use of pins 0 and 1 for inputs.

I hope your actual circuit has resistors in series with the LEDs, otherwise you will damage the LED and/or the UNO.

when i pressed reset on the Uno Board, then working well again..

I'll say it again: those buttons aren't wired in any logical way. It just doesn't make sense.
All you need for a button is to connect one pin to GND, the other to an input pin, and configure the input as INPUT_PULLUP. That's all. It will read HIGH by default and LOW if pressed.

Coincidentally this will free up the pins that are now apparently shared between sensors and buttons, so you can dedicate them to sensors. With the caveat some of them are still part of the Serial UART so you cannot debug this way, which doesn't help.

Hi,
Your Tinker diagram, different to original .

  1. Where are your current limit resistors for ALL your LEDS?
  2. What are the 10K resistors for between the buttons and input D3, D4 and D5 respectively.
  3. Why are you using 1K pullup and pull down resistors rather than 10K?

Have you tried any of this in the real world?

Tom.... :smiley: :+1: :coffee: :australia:

Drive those LEDs through a CD4094 or similar shift register and ULN2803 or similar current sink array. 40 cents, 8 IO.

@sanjeewamdn

Did all the problems from your previous thread on this project get solved?

Did all the questions we asked on your previous thread get answers from you?

I notice right away again, as have others, your odd circuitry…

And I too recommend the addition of hardware to provide more i/o pins or moving to an Arduino board that has more than enough already.

a7

Save 2 inputs (pins 0 and 1) by using a 3-bit R2R ladder connected to an analog input for your digital inputs waterLevelSensorLow, waterLevelSensorMedium and waterLevelSensorHigh. Then update your code for water level detection.

Your code from TINKERCAD:

#include <EEPROM.h>

const unsigned long debounceDelay = 50;

const int waterLevelSensorLow = 0;
const int waterLevelSensorMedium = 1;
const int waterLevelSensorHigh = 2;

const int btnPause = 3;
int btnPauseState;
int lastBtnPauseState = LOW;
unsigned long lastBtnPauseDebounceTime = 0;

const int btnAutoManual = 4;
int btnAutoManualState;
int lastBtnAutoManualState = LOW;
unsigned long lastBtnAutoManualDebounceTime = 0;

const int btnSemiAuto =5;
int btnSemiAutoState;
int lastSemiAutoState = LOW;
unsigned long lastBtnSemiAutoDebounceTime = 0;

const int ledWaterLevelLow = 6;
const int ledWaterLevelMedium = 7;
const int ledWaterLevelHigh = 8;

const int ledAuto = 9;
const int ledManual = 10;
const int ledPause = 13;
const int ledSemiAuto = A0;

const int waterMotor = A2;
const int buzzer = A3;

const int sumpTankLowSensor = A4;
const int ledSumpTankLow = A5;

const int ledRunning = 12;
const int ledStandby = 11;
int standbyLedBrightness = 0;
int standbyLedFadeAmount = 2;

const int ledTimer = A1;

bool isPaused = false;
bool isAuto = true;
bool isSemiAuto = false;

bool isPauseLedOn = false;
bool isRunningLedOn = false;
bool isTimerLedOn = false;

const long ledBlinkingTimeInMilliSeconds = 200;
long lastPauseLedChangedTime = 0;
long lastRunningLedChangedTime = 0;
long lastTimerLedChangedTime = 0;

//Water Level 0 = Empty, 1 = LOW, 2 = MEDIUM, 3 = HIGH
int waterLevel = -1;
int lastWaterLevel = -1; 

bool runWaterMotor = false;
bool isWaterMotorRunning = false;

bool isSumpTankLow = false;

long motorStarTime = 0;
//5 minutes
const long motorMaxRunTimeInMilliseconds = 300000;

void setup() {
  
  Serial.begin(9600);
  
  pinMode(btnPause, INPUT);
  pinMode(btnAutoManual, INPUT);
  pinMode(btnSemiAuto, INPUT);
  
  pinMode(ledPause, OUTPUT);
  pinMode(ledAuto, OUTPUT);
  pinMode(ledManual, OUTPUT);
  pinMode(ledSemiAuto, OUTPUT);
  pinMode(ledWaterLevelLow, OUTPUT);
  pinMode(ledWaterLevelMedium, OUTPUT);
  pinMode(ledWaterLevelHigh, OUTPUT);
  
  pinMode(waterLevelSensorHigh, INPUT);
  pinMode(waterLevelSensorMedium, INPUT);
  pinMode(waterLevelSensorLow, INPUT);
  
  pinMode(waterMotor, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  pinMode(sumpTankLowSensor, INPUT);
  pinMode(ledSumpTankLow, OUTPUT);
  
  pinMode(ledRunning, OUTPUT);
  pinMode(ledStandby, OUTPUT);
  
  pinMode(ledTimer, OUTPUT);
  
  readFromEEPROM();
  
  buzzerBeep();
  buzzerBeep();
}

void readFromEEPROM() {
  
  bool isEEPROMUpdated;
  EEPROM.get(EEPROM.length() - 1, isEEPROMUpdated);
  
  if (!isEEPROMUpdated){
    return;
  }
  
  int eeAddress = 0;
  
  EEPROM.get(eeAddress, isPaused);
  eeAddress += sizeof(bool);
    
  EEPROM.get(eeAddress, isAuto);
  eeAddress += sizeof(bool);
  
  EEPROM.get(eeAddress, isSemiAuto);
  eeAddress += sizeof(bool);
  
  EEPROM.get(eeAddress, lastWaterLevel);
  eeAddress += sizeof(int);
  
  EEPROM.get(eeAddress, runWaterMotor);
  eeAddress += sizeof(bool);
  
  EEPROM.get(eeAddress, isWaterMotorRunning);
  eeAddress += sizeof(bool);
  
  EEPROM.get(eeAddress, motorStarTime);
  eeAddress += sizeof(long);
}

void loop() {
  
  updateSumpTankWaterStatus();
  updateWaterLevel();
  updateButtonStatuses();
  handleWaterMotor();
  updateEEPROM();
}

void updateSumpTankWaterStatus() {
  int status = digitalRead(sumpTankLowSensor);
  
  isSumpTankLow = !status;
  digitalWrite(ledSumpTankLow, isSumpTankLow);
}

void updateWaterLevel() {
  setWaterLevel();
  handleWaterLevelLed();
}

void setWaterLevel() {
  
  int low = digitalRead(waterLevelSensorLow);
  int medium = digitalRead(waterLevelSensorMedium);
  int high = digitalRead(waterLevelSensorHigh);
  
  int calculatedWaterLevel;
  
  if (low == LOW && medium == LOW && high == LOW) {
  	calculatedWaterLevel = 3;
  } else if (low == LOW && medium == LOW && high == HIGH) {
  	calculatedWaterLevel = 2;
  } else if (low == LOW && medium == HIGH && high == HIGH) {
  	calculatedWaterLevel = 1;
  } else if (low == HIGH && medium == HIGH && high == HIGH) {
  	calculatedWaterLevel = 0;
  } else {
  	calculatedWaterLevel = -1;
  }
  
  if(waterLevel != calculatedWaterLevel) {
    
    if(lastWaterLevel == -1) {
      lastWaterLevel = calculatedWaterLevel;
    } else {
      lastWaterLevel = waterLevel;
    }
    
    waterLevel = calculatedWaterLevel;
  }
}

void handleWaterLevelLed() {
    
  if (waterLevel == 1) {
    
    digitalWrite(ledWaterLevelHigh, LOW);
    digitalWrite(ledWaterLevelMedium, LOW);
    digitalWrite(ledWaterLevelLow, HIGH);
    
  } else if (waterLevel == 2) {
    
    digitalWrite(ledWaterLevelHigh, LOW);
    digitalWrite(ledWaterLevelMedium, HIGH);
    digitalWrite(ledWaterLevelLow, HIGH);
    
  } else if (waterLevel == 3) {
    
    digitalWrite(ledWaterLevelHigh, HIGH);
    digitalWrite(ledWaterLevelMedium, HIGH);
    digitalWrite(ledWaterLevelLow, HIGH);
  
  } else {
  
    digitalWrite(ledWaterLevelHigh, LOW);
    digitalWrite(ledWaterLevelMedium, LOW);
    digitalWrite(ledWaterLevelLow, LOW);
  }
}

void updateButtonStatuses() {
  setAutoStatus();
  setPauseStatus();
  setSemiAutoStatus();
  
  handleAutoManualLeds();
  handlePauseLed();
  handleSemiAutoLed();
}

void setAutoStatus() {
  
  int reading = digitalRead(btnAutoManual);
  
  if (reading != lastBtnAutoManualState) {
    lastBtnAutoManualDebounceTime = millis();
  }
  
  if ((millis() - lastBtnAutoManualDebounceTime) > debounceDelay) {
    if (reading != btnAutoManualState) {
      btnAutoManualState = reading;

      if (btnAutoManualState == HIGH) {
        isAuto = !isAuto;
        buzzerBeep();
      }
    }
  }
  lastBtnAutoManualState = reading;
}

void handleAutoManualLeds() {
  
  bool isAutoLedOn = isAuto && !isPaused;
  digitalWrite(ledAuto, isAutoLedOn);
  
  bool isManualLedOn = !isAuto && !isPaused;
  digitalWrite(ledManual, isManualLedOn);
}

void setPauseStatus() {
  
  int reading = digitalRead(btnPause);
  
  if (reading != lastBtnPauseState) {
    lastBtnPauseDebounceTime = millis();
  }
  
  if ((millis() - lastBtnPauseDebounceTime) > debounceDelay) {
    if (reading != btnPauseState) {
      btnPauseState = reading;

      if (btnPauseState == HIGH) {
        isPaused = !isPaused;
      }
    }
  }
  lastBtnPauseState = reading;
}

void handlePauseLed() {
    
  if(isPaused) {
    
    long timeElapsedAfterLastChange = millis() - lastPauseLedChangedTime;
    
    if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
      
      isPauseLedOn = !isPauseLedOn;
      digitalWrite(ledPause, isPauseLedOn);
      lastPauseLedChangedTime = millis();
    }
  } else {
  
    digitalWrite(ledPause, LOW);
    isPauseLedOn = false;
  } 

  if(isPauseLedOn) {
    buzzerBeep();
  }
}

void setSemiAutoStatus() {
  
  int reading = digitalRead(btnSemiAuto);
  
  if (reading != lastSemiAutoState) {
    lastBtnSemiAutoDebounceTime = millis();
  }
  
  if ((millis() - lastBtnSemiAutoDebounceTime) > debounceDelay) {
    if (reading != btnSemiAutoState) {
      btnSemiAutoState = reading;

      if (btnSemiAutoState == HIGH) {
        isSemiAuto = !isSemiAuto && (waterLevel < 3 && waterLevel > 0);
        buzzerBeep();
      }
    }
  }
  lastSemiAutoState = reading;
  
  if (waterLevel == 3) {
    isSemiAuto = false;
  }
}

void handleSemiAutoLed() {
  digitalWrite(ledSemiAuto, isSemiAuto && isAuto && !isPaused);
}

void handleWaterMotor() {
  setWaterMotorRunStatus();
  
  if(isWaterMotorRunning != runWaterMotor) {
    
    if(runWaterMotor) {
      buzzerBeep();
      buzzerBeep();
      buzzerBeep();
      
      motorStarTime = millis();
    }
    
    digitalWrite(waterMotor, runWaterMotor);
    isWaterMotorRunning = runWaterMotor;
    
    if(!runWaterMotor) {
      buzzerBeep();
      buzzerBeep();
      buzzerBeep();
      
      motorStarTime = 0;
    }
  }
  
  handleRunningLed();
  handleSandbyLed();
  handleTimerLed();
}

void setWaterMotorRunStatus() {
  
  bool isWaterLevelLow = waterLevel == 0;
  bool isWaterLevelLowToHigh = waterLevel < 3 && waterLevel > 0;
  bool isTankFillingUp = isWaterLevelLowToHigh && waterLevel > lastWaterLevel;
  bool runMotorAutoMode = isAuto && (isWaterLevelLow || isTankFillingUp);
  bool runMotorSemiAutoMode = isSemiAuto && (isWaterLevelLowToHigh || isTankFillingUp);
  bool isTimerUp = isAuto && (millis() - motorStarTime) >= motorMaxRunTimeInMilliseconds;
  
  
  runWaterMotor = (runMotorAutoMode || runMotorSemiAutoMode || !isAuto) && !isPaused && !isSumpTankLow && !isTimerUp;
}

void handleRunningLed() {
    
  if(isWaterMotorRunning) {
    
    long timeElapsedAfterLastChange = millis() - lastRunningLedChangedTime;
    
    if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
      
      isRunningLedOn = !isRunningLedOn;
      digitalWrite(ledRunning, isRunningLedOn);
      lastRunningLedChangedTime = millis();
    }
  } else {
  
    digitalWrite(ledRunning, LOW);
    isRunningLedOn = false;
  } 

  if(isRunningLedOn) {
    buzzerBeep();
  }
}

void handleSandbyLed() {
  
  if(!isWaterMotorRunning && (isAuto || isSemiAuto) && !isPaused) {      
  	
    analogWrite(ledStandby, standbyLedBrightness);
  	standbyLedBrightness = standbyLedBrightness + standbyLedFadeAmount;

  	if (standbyLedBrightness <= 0 || standbyLedBrightness >= 255) {
    	standbyLedFadeAmount = -standbyLedFadeAmount;
  	}
    
  	delay(30);
    
  } else {
    digitalWrite(ledStandby, LOW);
  }
}

void handleTimerLed() {
  
  if(isWaterMotorRunning && motorStarTime > 0 && isAuto){
    
    long timeElapsedAfterLastChange = millis() - lastTimerLedChangedTime;
    
    if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
      
      isTimerLedOn = !isTimerLedOn;
      digitalWrite(ledTimer, isTimerLedOn);
      lastTimerLedChangedTime = millis();
    }
  } else {
     digitalWrite(ledTimer, LOW);
     isTimerLedOn = false;
  }
}

void buzzerBeep() {
  const int buzzerTime = 250;
  
  digitalWrite(buzzer, HIGH);
  delay(buzzerTime);
  digitalWrite(buzzer, LOW);
  delay(buzzerTime);
}

void updateEEPROM() {
  
  int eeAddress = 0;
  
  EEPROM.update(eeAddress, isPaused);
  eeAddress += sizeof(bool);
    
  EEPROM.update(eeAddress, isAuto);
  eeAddress += sizeof(bool);
  
  EEPROM.update(eeAddress, isSemiAuto);
  eeAddress += sizeof(bool);
  
  EEPROM.update(eeAddress, lastWaterLevel);
  eeAddress += sizeof(int);
  
  EEPROM.update(eeAddress, runWaterMotor);
  eeAddress += sizeof(bool);
  
  EEPROM.update(eeAddress, isWaterMotorRunning);
  eeAddress += sizeof(bool);
  
  EEPROM.update(eeAddress, motorStarTime);
  eeAddress += sizeof(long);
  
  EEPROM.update(EEPROM.length() - 1, true);
}

Remove Serial.begin(), you do not want to initialize pins 0 and 1 for serial communications when you are using them for something else.

How are you driving the motor? What type battery are you using? Electrical interference from the motor is a common cause of problems.

No,

if All are OK, we can use relay for water motor switching by using Transistor connected with assigned Arduino motor pin know...?