How I can make my code stop until I press a button?!

if (v2 <= 5.39) { digitalWrite (4,HIGH);
}
else if (v2 >= 5.45) { digitalWrite (4,LOW);
}

I connect a Relay to pin4
I want my code stop at this point and not continue until I press a push button
so if my relay turned ofF it will be forever OFF until i press a button

I connect a Relay to pin4

That's damned hard to see with that odd coding style.

if (v2 <= 5.39)
{
   digitalWrite (4,HIGH);
}
else if (v2 >= 5.45)
{
   digitalWrite (4,LOW);
}

is the way that most people do it, so you can see what is going on AND add more code.

I want my code stop at this point and not continue until I press a push button
so if my relay turned ofF it will be forever OFF until i press a button

Organized like that, you can see that there is more than one "at this point". Which point are you talking about?

   // Wait for a switch to be pressed (or released)
   while(digitalRead(somePin) == someState)
   {
       // do nothing
   }

Here's an example with cheap button (a jumper that you ground on the USB connector) that pushed, lights led13 after 1 second and pushed again, stops the led blinking.

Run it, mess with it. The serial output is 115200 baud, make sure your monitor is the same.

// All you need to do this example is an Arduino and a button on pin 2.
// Push the button once, led lights after delay. Again, led turns off.
// Button is debounced and wired directly to ground. The button can be a 
// jumper from pin 2 grounded on USB connector. Tested with jumper.
// By GoForSmoke for free public use. Using Arduino 1.0.5-r2

const byte ledPin =  13;      // this is the onboard led pin
const byte buttonPin = 2;


byte setLedPin = 0;
const unsigned long ledDelay = 1000UL; // blink interval
unsigned long ledDelayStart = 0U; // 16 bit blink (on or off) time ms

// button variables
byte buttonRead;  // wired for pullup -- if down then LOW, if up then HIGH 
byte lastButtonRead = HIGH; // so debounce knows previous read
byte checkDebounce = 0; // only checks decounce after every button pin change
byte lastButtonState = 0; // last stable button state
byte buttonState = 0;  // stable button state
// 0 = button is up after debounce
// 1 = button is down after debounce

// button debounce timing variables
const unsigned long debounceDelayMs = 100UL; 
unsigned long debounceStartMs; 
unsigned long msNow; 


byte processState = 0; // 1st press to start, 1st release, 2nd press to stop, 2nd release


void setup() 
{
  Serial.begin( 115200 );

  pinMode( ledPin, OUTPUT );  // default is LOW 
  pinMode( buttonPin, INPUT_PULLUP ); // my button connects to ground, not 5V
  // however that means that when the button is pressed the pin is LOW.
}

void loop() // make sure that loop() runs fast and stays in the "now".
{

  // BUTTON CODE BLOCK, it handles debouncing. 
  // the task is to set the variable buttonState when a Stable Button State is reached.
  // other sensor code could change the same variable if desired

  // read the pin which may be changing fast and often as the button bounces
  buttonRead = digitalRead( buttonPin ); // momentary state

  msNow = millis(); 

  if ( buttonRead != lastButtonRead )
  {
    debounceStartMs = msNow;
    checkDebounce = 1;
  }
  else if ( checkDebounce )
  { 
    if ( msNow - debounceStartMs >= debounceDelayMs ) // stable button state achieved
    {
      buttonState = !buttonRead; // mission accomplished, button is stable
      // note that buttonState is opposite buttonRead
      checkDebounce = 0; // stop debounce checking until pin change
    }
  }
  lastButtonRead = buttonRead;
  //
  // End of the BUTTON CODE BLOCK

  //==================================================================================

  // CONTROL CODE BLOCK that uses buttonState and processState
  
  if ( lastButtonState != buttonState )
  {
    lastButtonState = buttonState;

    Serial.println( F( "============================================================" ));    
    Serial.print( F( "processState " ));
    Serial.print( processState );
    Serial.print( F( "  buttonState " ));
    Serial.println( buttonState );
    Serial.print( F( "  millis " ));
    Serial.println( msNow ); 
    Serial.println( F( "============================================================" ));    

    switch ( processState )
    {
    case 0: // waits for press without blocking
      if ( buttonState == 1 ) // button is pressed 
      {
        processState = 1;
        setLedPin = 1; // start led blink task
        ledDelayStart = millis(); // led blink control value set in the control block
      }
      break; 

    case 1: // waits  for release without blocking
      if ( buttonState == 0 ) // button is released 
      {
        processState = 2;
      }
      break; 

    case 2: // waits  for press without blocking
      if ( buttonState == 1 ) // button is pressed 
      {
        processState = 3;
        setLedPin = 2; // led blink control value set in the control block
      }
      break; 

    case 3: // waits  for release without blocking 
      if ( buttonState == 0 ) // button is released 
      {
        processState = 0; // start over
      }
      break; 
    } 
  }
  // End of the CONTROL CODE

  //==================================================================================

  // LED BLINK CODE BLOCK
  if ( setLedPin == 1 )
  { 
    if ( millis() - ledDelayStart >= ledDelay )
    { 
      digitalWrite(ledPin, HIGH );
      setLedPin = 0;
    }
  }
  else if ( setLedPin == 2 )
  {
    digitalWrite(ledPin, LOW );
    setLedPin = 0;
  }
  // End of the LED BLINK CODE

  //==================================================================================

  // Want to add serial commands and args input? 
  // this is a good spot. Serial is so slow it can usually go last.
}

ok PaulS I will use your way :roll_eyes:
Please bring a cub of coffee because your brain will hurt you after this, it's a complicated problem.
This is my circuit

When I run the circuit, voltmeter read "0" because my relay is OFF then the relay turn ON because it's under 5.39V, now because my relay is ON the voltmeter will read 5.75V, because my power source is 5.75V, and because my power coerce is higher than 5.40 so my relay will turn off again and enter in infinity loop every 8 second relay ON then OFF,
So for that i want to add a push button or anything to avoid this.
to be more clear this is my code

#include <LiquidCrystal.h>
int thermistorPin = A0; //analog pin 0
int Relay = 4;
float vPow = 4.7;
 float r1 = 50000.0;
 float r2 = 4400.0;
 LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 void setup() {
   Serial.begin(9600);
   lcd.begin(16,2);

   Serial.print("\x1B");
   Serial.print("[2J");
   Serial.print("\x1B");
   Serial.println("[H");
   
   Serial.println("--------------------");
   Serial.println("DC VOLTMETER");
   Serial.print("Maximum Voltage: ");
   Serial.print((int)(vPow / (r2 / (r1 + r2))));
   Serial.println("V");
   Serial.println("--------------------");
   Serial.println("");
     
 }
 void loop() {
   float v = (analogRead(1) * vPow) / 1024.0;
   float v2 = v / (r2 / (r1 + r2));
   int thermistorReading = analogRead(thermistorPin); 
   // Send ANSI terminal codes
   Serial.print("\x1B");
   Serial.print("");
   
Serial.println(thermistorReading);
Serial.println(v2);
lcd.setCursor(0, 0);
lcd.print(v2);
lcd.print(" V");
lcd.setCursor(0, 1);
lcd.print(thermistorReading);
lcd.print(" Temp");

if (v2 <= 5.39 ) { digitalWrite (4,HIGH);
}
if (v2 >= 5.40 ) { digitalWrite (4,LOW);
}
delay(8000);
}

I am using this to charge my lithium battery from a power source, and it will turn the charger off when the battery is fully charged, i can't connect the voltmeter to my lithium battery, voltmeter must be connected to both "battery and my power source to read the Voltage correctly"
forget about the temp sensor and the charger, anyway i am using charger 5.75 V not AA battary like the pic

Have you ever heard of hysteresis?

And how often do you expect to feed the Arduino more than 5.5V?

I am using 9V-1A for my Arduino, everything under-control, That's not my fully circuit, I made a simple pic for this problem to solve it :roll_eyes: Hysteresis no it's my first time, I will read about it

I solve it :slight_smile: thanks everyone, I flip the relay wires and now there is no loops +1 for everyone here

narzan:
I am using 9V-1A for my Arduino, everything under-control, That's not my fully circuit, I made a simple pic for this problem to solve it :roll_eyes: Hysteresis no it's my first time, I will read about it

A digital pin uses hysteresis. Going from LOW to HIGH, voltage must pass about 3V (on 5V chip) to be HIGH but once HIGH must drop to almost 1V to switch to LOW.

A thermostat uses hysteresis. The difference between "cold, turn on" and "hot turn off" is a few degrees to prevent the heater from flickering itself broken.

( < 5.39V ) and ( > 5.4V ) is close for many things at .2V hysteresis but glad it is working now!

You are feeding the 9V to Arduino external power socket? That uses a 7805 regulator. The heat it makes is the excess 4V getting dropped to ground. Look for DC DC (buck) converter to turn your 9V 1A into 5V 1.4A to feed directly to VIN. If you run on batteries, it will make them last longer.

You do not feed analog pins more than 5V then?