Arduino on/off

Hello, I wanted to turn on/off the power supply to my arduino using a pushbutton. When I hold the button it will turn off the arduino but as soon as I let it go, it turns it back on. I know that part of the code needs to be fixed. This is a simple battery tester that will turn on the green, yellow or red LED depending on the voltage. I am using an I2C LCD that displays the voltage.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#define pushButtonPin 5
int buttonPushed=0;

int greenLed = 2;
int yellowLed = 3;
int redLed = 4;

int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;

void setup()
  // Print a message to the LCD.
lcd.begin(16, 2);
  pinMode(greenLed, OUTPUT);
  pinMode (pushButtonPin, INPUT_PULLUP);

void loop()
  analogValue = analogRead(A1);
  voltage = 0.0048*analogValue;

  if( voltage >= 1.6 )
    digitalWrite(greenLed, HIGH);
  else if (voltage > 1.2 && voltage < 1.6)
    digitalWrite(yellowLed, HIGH);
  else if( voltage <= 1.2)
    digitalWrite(redLed, HIGH);  

  digitalWrite(redLed, LOW);
  digitalWrite(yellowLed, LOW); 
  digitalWrite(greenLed, LOW);

  lcd.setCursor(0, 1);

    buttonPushed = 1;
  if (buttonPushed){
    buttonPushed = 0;


I’m sure this last part in my code is the problem

    buttonPushed = 1;
  if (buttonPushed){
    buttonPushed = 0;

Thanks for your help!

What is the purpose of these lines?

  if (buttonPushed){
    buttonPushed = 0;

I am having great problems understanding what you are trying to do, perhaps I have missed something, but if you press a button that the Arduino reads to turn the PSU to the Arduino off - that makes sense, but to press the button to turn he Arduino on when the Arduino has to read the button seems to make no sense to me (how can it read a button if it is not powered on?).

What sort of button are you using?

You store a value in "buttonPushed" but you don't use that value for anything so all of the button code seems to be completely useless.

Thank you countrypaul and everyone who replied. That is very true, how can the arduino read the button if there is no power to it! I will just have to put a switch to turn the power on and off from the battery, which was what I wanted it to do. Funny how the simplest of things can elude us when our mind is focused on something else (for me thinking it was the code). This is a D'oh! moment for me. Thanks again!