While Loop instead of if statements

This question is from Mark Quarto:

The Question:

Hi Michael:

I’m pulling my hair out with a While Loop control for an automotive air conditioning circuit and hope you could point me in the correct direction. I’m having difficulty exiting the While Loop. I’ve tried all kinds of statements, etc. with no luck. I really want to learn the While Loop instead of substituting with IF statements.

Would you please take a quick look at my code and see if you could provide me with some direction? My code is well documented so, it should be easy to read. I just can’t get a handle on exiting the While Loop. Any assistance would be greatly appreciated.

Best Regards,

//This program will monitor an A/C signal to control when the Engine Cooling Fan will be commanded ON

//Global Variables

float Sensor_1 = A0;//Sensor_1 assigned to Analog Pin A0.  This is a Variable that sets Analog Pin 0 to sense when A/C Input voltage has crossed the voltage threshold.
float vsense_1 = 0; //This is a Variable that will read the voltage of A/C Input Sensor_1
int Threshold = 300;// 300 will provide 3.00 Volt Threshold: 5/1024 = .00488281V/bit.  Therefore, (.00488281)(300) = 1.46 Volts
int Relay_1 = 6; //This is a Variable that sets Digital Pin 6 that controls the Engine Cooling Fan Relay.
int ledPin = 8; // This is a Variable that will illuminate an LED if the Cooling Fan Relay has been commanded to close and turn on Cooling Fan

void setup() //This loop will run only once after the program is initiated.
{
  pinMode (vsense_1, INPUT);//Set Analog Pin A0 as an INPUT to sense when A/C has been commanded ON
  pinMode (ledPin, OUTPUT); ////This Mode will assign ledPin(Digital Pin 8) as an OUTPUT.
  pinMode (Relay_1, OUTPUT); //This Mode will assign Relay_1 (Digital Pin 6) as an OUTPUT.
  Serial.begin (1200); //This function will start serial communications to a computer at 9600 Baud.
}

void loop() //This loop will run continuously after program is initiated.

{
 
  while (Sensor_1 >= Threshold); //Read Air Cond Input Voltage sense Analog Pin A0; Turn Relay & LED ON if Voltage threshold has been breached

  {
    digitalWrite(Relay_1, HIGH); //Turn Relay_1 ON
    digitalWrite (ledPin, HIGH); //Turn LED indicator
    Serial.print ("Relay 1 Indicator = ON  "); //Command to Print the Voltage Threshold Monitor to the Serial Monitor.
    Serial.println (vsense_1 * .00488281); //Command to Print the Digital value on Pin 4 to the Serial Monitor.
    analogRead (Sensor_1 >= Threshold);      
    //End While Loop.
  }  
   
    //IF the WHILE Loop is no longer TRUE then, it must exit its loop and do the next action.
  { 
    digitalWrite(Relay_1, LOW); //Turn Relay_1 ON
    digitalWrite (ledPin, LOW); //Turn LED indicator
    Serial.print ("Relay 1 Indicator = OFF  "); //Command to Print the Voltage Threshold Monitor to the Serial Monitor.
    Serial.println (vsense_1 * .00488281); //Command to Print the Digital value on Pin 4 to the Serial Monitor.

  }

 }
//END

Your Challenge:

Take a look at the code – can you see any reason why there may be any issue?

Hints:

Take a close look at the while statement condition.

Solution:

This is what I came up with…
[fvplayer src=”https://vimeo.com/333973689″ transcript=”auto” splash=”https://i.vimeocdn.com/video/780119881_1280x720.jpg?r=pad” caption=”Mark_Quarto_While_loop”]

You Thoughts:

This exact this happens to me all the time!  Has it ever happened to you?

Let us know what in the comments.

Leave a Comment