how to escape from while control structure

I can not understand how while works the previous post didn't shine any light on the simple escape from while control structure.
It sounds so simple "while loops continuously until untrue" only mine gets locked in to loop.
I am building a brewery controller and want to make sure the heater is covered with liquid before the heater is turned on ( with PID controller)
PID and DS18b20 all work great. I simply have three electrodes into the top of the vessel one will be ground one either side will be connected to PINs I am using A0 and A1 as digital inputs because of pin usage with LCD, PWM and a couple of temp sensors.
Basically pinMode ( A0, INPUT_PULLUP) returns HIGH until liquid pulls it LOW . I want both pins pulled LOW before the heater can turn on just to be sure the liquid is there reduce the chance of sticky liquid giving false positive.
I digitalRead the pins fine they go HIGH they go LOW. but WHILE they will not come out. If I break out at the bottom then it continues on through the heater part before returning to test.
I am confused about the logic
Without the break it just will not drop out what ever the pins are.
This is my while trial bit. I can just jumper from grnd to A0 and A1 to test on an UNO

#include <arduino.h>

void setup(){
  Serial.begin(9600);
  pinMode(A0, INPUT_PULLUP);   // pins digitalRead High or 1
  pinMode(A1, INPUT_PULLUP);  // until contected to grnd
  pinMode(13, OUTPUT); // warning light or buzzer

}

void loop(){
 
  int zero = digitalRead(A0); // check sensors
  int one = digitalRead(A1);

  Serial.println("Before");
  Serial.println(zero);
  Serial.println(one);
  
 while(zero == HIGH||one == HIGH) {
    digitalWrite(13, HIGH);  // warning light or buzzer
    delay(1000);
    Serial.println("in ");       // tells me its in while loop
   int zero = digitalRead(A0);  // check sensors again
   int one = digitalRead(A1);
   Serial.println(zero);
  Serial.println(one);
  break;   // leave it in or take it out it don't help  
}
 delay(1000);
    digitalWrite(13, LOW);     // light or buzzer off get on with sketch
    Serial.println("after");  // all the rest of the sketch heater PID etc.
}

I am sorry if I am missing something stupid but I have been around and around for days.

maxmaisy:
Without the break it just will not drop out what ever the pins are.

Are you seeing two zeros in the serial console?

[/quote]

Here is your problem:

   int zero = digitalRead(A0);  // check sensors again
  int one = digitalRead(A1);

You are declaring new variables zero and one inside the loop, which are different from the ones you are testing in the while(..) clause. Change it to:

   zero = digitalRead(A0);  // check sensors again
  one = digitalRead(A1);

Yes, the serial shows two zeros

dc42
I don't see it. but I have changed to

  zero = digitalRead(A0);  // check sensors again
    one = digitalRead(A1);

an it does exactly the same thing.

I don't see it. but I have changed to ... an it does exactly the same thing.

Then, it's time to talk about what you have connected to the two pins. With a pullup resistor, the pins will read HIGH until the switch is pressed.

Why are you using analog pins for this? Are all your digital pins in use?

How long are you holding the switch down for? The one second delay() is going to make the code very unresponsive.

Thanks dc42
I did try just to remove the int but didn't seem to work. So I renamed them to make explaining it easier and it worked.

void loop(){
 
  int cat = digitalRead(A0); // check sensors
  int dog = digitalRead(A1);

  Serial.println("Before");
  Serial.println(cat);
  Serial.println(dog);
  
 while(cat == HIGH||dog==HIGH) {
    digitalWrite(13, HIGH);  // warning light or buzzer
    delay(1000);
    Serial.println("in ");       // tells me its in while loop
   cat = digitalRead(A0);  // check sensors again
   dog = digitalRead(A1);
   Serial.println(cat);
  Serial.println(dog);

}
 delay(1000);
    digitalWrite(13, LOW);     // light or buzzer off get on with sketch
    Serial.println("after");  // all the rest of the sketch heater PID etc.
}

Could someone just explain the difference between

 int dog = digitalRead(A1);

and checking

 dog = digitalRead(A1);

thanks I am always amazed by how great this forum is.

Sorry Paul didn't mean to be rude and not reply to your comment.
Yes my didgital pins are almost being used and I was interested in being able to use the analog pins as digital INPUT pins.
I have previously connected an LCD using just the analog pins set to digital OUTPUT which was useful for that particular project.

The code is always responsive in that it read the change just gets stuck looping in while even when showing 0's.

maxmaisy:
Could someone just explain the difference between

 int dog = digitalRead(A1);

and checking

 dog = digitalRead(A1);

thanks I am always amazed by how great this forum is.

The first one creates a new variable of type 'int' called 'dog' and initialises it with the result of the digitalRead call. Any reference to 'dog' inside the innermost pair of { } that encloses this statement refers to this new variable. Any reference to 'dog' outside the innermost { } enclosing this statement means whatever 'dog' would mean if the { } and its contents were not there.

The second one does not create an new variable. It assigns the existing variable called 'dog' with the result of the digitalRead call.

The coding standards we use when writing safety-critical software forbid declaring a variable in an inner scope that has the same name as a variable in an enclosing scope, precisely to avoid this kind of error.

After dc42's help the sketch behaved well. Then out of no where when installing the electronics into the brewery the sketch exited the loop and would not re-enter.
I debuged it by getting serial.print the state of the analog pins A0 and A1 at every step through the sketch. They start out HIGH ie. 1
The state of the analog pins changed from HIGH as set by INPUT_PULLUP to LOW at the end of the set up as the sketch entered the Loop no line of instruction was present at that point.
The last line of setup was serial.print A0 and A1 both were 1
the first line after void loop (void){ was serial.print A0 and A1 both were 0

How can this happen?

Sounds like a wiring issue if it occurred when you moved your circuitry. Nonetheless, post your latest code.

It can't be wiring because I took the sketch and loaded it into my UNO board with two jumpers to Ao and A1 and it still did it.

Here is the code if anyone wants to way in. Thanks

#include <Arduino.h>

#include <OneWire.h>
#include <DallasTemperature.h>         // temperature sensors.
#define ONE_WIRE_BUS 8


// Data wire is plugged into pin 8 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);      // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature. 
DeviceAddress  Rims = {
  0x28, 0xDC, 0x09, 0x22, 0x04, 0x00, 0x00, 0xBD}; //DS18B20 address Done!!
DeviceAddress  Mash = {
  0x28, 0xEB, 0x6A, 0x21, 0x04, 0x00, 0x00, 0x02 }; //DS18B20 address Done!!


#include <Button.h>
Button one = Button(10,PULLUP); // button on pin 7
Button two = Button(9,PULLUP); // button on pin 9
const int heaterpin = 6;       // output on pin 3 conected to heater
float tempC = 23;               // tempC will be asigned actual temp later
int cat = 0;  // electrodes to insure water level
int dog = 0;  // white and blue contect ot A4 & A5 Red central to +ve
int set = 0; // set will be setpoint to display purposes.
int ledpin = 13; // set led to pin 13
int heat = 0;  // 
int safecat = A2;  // pin number A2 is connected the safety pin in the RIMS tube
int safedog = A3;  // pin number A3 is connected the safety pin in the RIMS tube
//int Maxpower = 200; // sets how hot the heating element gets when full on upto 255

#include <PID_v1.h>
double Setpoint, Input, Output;  //Define Variables we'll be connecting to
PID hltPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);  //Specify the links and initial tuning parameters


#include <LiquidCrystal.h>                 // include the library code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // numbers of the LCD interface pins Analog as digital



void setup(void){ 
  Serial.begin(9600);  // start serial port if need to debug

  pinMode(ledpin,OUTPUT); //  warning light or buzzer

  pinMode(safecat,INPUT_PULLUP);   //  seting the Analog pins to digital input
  pinMode(safedog,INPUT_PULLUP);   // gnd------ probe---water or not-- probe------pin  
  cat = digitalRead(safecat);  // asigning cat & dog variables with the value
  dog = digitalRead(safedog);

  sensors.begin(); // Start up the DS18B20 library
  sensors.setResolution(Mash,10);// set the resolution to 10 bit (good enough?)
  sensors.setResolution(Rims,10);// set the resolution to 10 bit (good enough?)

  lcd.begin(20, 4); // set up the LCD's number of columns and rows: 
  lcd.print("Rims Temp is :");
  lcd.setCursor(0, 1);
  lcd.print("Mash Temp is :" );
  lcd.setCursor(0, 2);
  lcd.print("   Set point :" );

  Input = tempC; //PID initialize the variables we're linked to
  Setpoint = 68.50;  // Setpoint entered here can adjust with buttons
   hltPID.SetMode(AUTOMATIC);  //turn the PID on 
 //  hltPID.SetOutputLimits(0,Maxpower); // limits how hot the heating element gets when full on.




}
// button press sub-routine for increasing setpoint
void Up(){
  lcd.setCursor(14, 2);
  lcd.print(Setpoint = Setpoint + 0.5);  
}
// button press sub-routine for decreasing setpoint
void Down(){
  lcd.setCursor(14, 2);
  lcd.print(Setpoint=Setpoint - 0.5);
}


void loop(void){ 
  
  
  delay (200);
  sensors.requestTemperatures();      //get temperature
  float tempC = sensors.getTempC(Rims);//asign the temperature to tempC
  float tempM = sensors.getTempC(Mash);//asign the temperature to tempM

  lcd.setCursor(14, 0);               
  lcd.print(tempC);                   // print tempC to LCD
  lcd.setCursor(14, 1);               
  lcd.print(tempM);                   // print tempM to LCD
  lcd.setCursor(14,2);                 
  lcd.print(set);                     // print Setpoint temp.

  // ________________________________________________________________

  // lock up the sketch until RIMS is full of water 
    cat = digitalRead(safecat); // re sample
    dog = digitalRead(safedog);
    
  while(cat == HIGH||dog == HIGH){    // both pins need to be conducting to ground
    analogWrite(heaterpin,0); // make sure heater is off
    digitalWrite(ledpin,HIGH);  // Big red led do not heat 
    // may use a buzzer
    cat = digitalRead(safecat); // re sample
    dog = digitalRead(safedog);
    delay(1000);
    Serial.println("in while");   //debug shows it is in the "while" loop
    Serial.print("AO= ");
    Serial.println(cat);
    Serial.print("A1= ");
    Serial.println(dog);
    lcd.setCursor(5, 3);    
    lcd.print(cat);              // print 1 or 0 just to reasure me
    lcd.setCursor(8, 3);
    lcd.print(dog);

    heat = digitalRead(ledpin);   // prints to lcd state of heater
    if (heat=0){                  // may refine to show how much on
      lcd.setCursor(12,3);        // little-- medium-- full
      lcd.print("Heat ON");
    }
    else{
      lcd.setCursor(12,3);
      lcd.print("Heat OFF");
    }


    if(one.isPressed()){          // may remove this from loop as not needed.
      Up();
      Serial.print("button1");
    }
    else if(two.isPressed()){
      Down();
      Serial.print("button2");
    }
  }

  // __________________________________________________________________

   Serial.println("Out ");
   delay(500);
   digitalWrite(ledpin,LOW);     // RIMS tube is full good to go 
   lcd.setCursor(12,3);
   lcd.print("Heat OFF");
    
  set=Setpoint;
  Input = tempC;                      // Asign TempC to PID input
  hltPID.Compute();                    // PID do its stuff
  analogWrite(heaterpin,Output);       // Adjust the output 

    if(one.isPressed()){
    Up();
    Serial.print("button1");
  }
  else if(two.isPressed()){
    Down();
    Serial.print("button2");
  }
} // End

That latest code doesn't even reference A0 or A1. So, what is your problem?

Problem has resolved itself. Unknown reason. Same code same circuit.

Sorry the latest code got changed to A2 and A3 because I had tried so often to solder and re solder the connectors that I had to jump to the next two analog pins, As I only have 1 digital pin not being used. The issue still occurred after I changed Analog pins I actually tried ALL the analog pins in case it was an issue with just A0 and A1 but still occurred, then it didn't.
Thanks for all the help.