Code Logic Correction

I'm trying to build this circuit. When I pressed the push button it will power the arduino nano and the A1 will be LOW for 10 seconds which will make the pin 2 High (which is relay pin) and as a result the Arduino will remain ON even I release the push button due to the relay which connects the GND of battery to the Arduino GND pin.
The issue is
The Arduino turns ON after 10 seconds but it doesn't turned OFF when i again press the same push button but making the relay pin LOW

const int RELAY_PIN = 2;        
const int BUTTON_PIN = A1;    

bool buttonPressed = false;  
unsigned long pressTime;     
int pressCount = 0;          

void setup() 
{
  pinMode(RELAY_PIN, OUTPUT);       
  pinMode(BUTTON_PIN, INPUT_PULLUP);      
}

void loop()
 {
  if (digitalRead(BUTTON_PIN) == LOW && !buttonPressed)
   {
    buttonPressed = true;         
    pressTime = millis();        
    pressCount++;                 
  }

  if (digitalRead(BUTTON_PIN) == HIGH) 
  {
    buttonPressed = false;        
  }

  if (pressCount == 1 && (millis() - pressTime >= 10000))
   {
    digitalWrite(RELAY_PIN, HIGH);   
   }

  if (pressCount == 2)
   {
    digitalWrite(RELAY_PIN, LOW);    
    pressCount = 0;               
  }
}

I would instal temporary serial.println of critical variables and see what's going on in the code.

1 Like

How is the Arduino going to hold a HIGH level on pin A1 if that cuts it's power? Once the power is cut then A1 will no longer be high and the relay will switch and the power will come back on.

Sit and really imagine what's going on here. I think you should be able to see the flaw here.

When pressing the button for 10 seconds it got enough time to run the code and make the pin2 HIGH*(I set the 10s timer in the code)*. Its works fine but I can't make the pin2 LOW when pressing the button again.

Because you cut the power to the Arduino. How will the Arduino run code if you've cut it's power?

  • Please explain this circuit ?

The schematic that I posted before is wrong.
Now this is the one with the right connections.

When the relay is cut then it cuts the ground connection to the Arduino right? So the Arduino loses power right? I'm still confused as to how you think code will keep running.

You have to press the button for more then 10 seconds.
When the button is pressed it will power the Arduino and at the same time it will make the pin A1 LOW for 10 seconds which will turn ON the relay.
That's the logic behind this.

You still haven't answered @LarryD's question in post #6.

It's a good thing BT2 'w a battery of small capacity, otherwise it would be an "explosion" when pressing S2 . :grin:


It doesn't explodes. Relay turns ON fine but the problem is I can't turned OFF when the button is pressed for the second time.

A1 will never work if it remains connected to GND.

As long as the relay is energized, the Arduino ground and the battery ground are connected together. Pressing the button will not have any affect at that point, since both sides of the switch are already connected together.

< edit > in fact, I don't see how the A1 input could ever be HIGH, since it is permanently connected to the Arduino ground.

  • Please explain exactly how the circuit in post #11 works.

  • Give us a link to the U5 relay.

This is the link to my circuit video on google drive. You can see the working of the relay in the video.

I think you still don't understand that in the schematic you posted, A1 is "eternally" connected to GND.