Are nested while loops WITHIN if statements a thing? Help please.

Hey all,

I have this code that I need some serious help with. It seems like it should be simple but I've been at it for a long time now. Essentially, I have a push button, a photoresistor, some LEDs, a piezo buzzer, and an LCD on a circuit. I want my code to work like this:

IF push button is HIGH, print some stuff on LCD. Then, WHILE the code is in this IF loop, I want to read the photoresistor value. If it's more than a value, the code executes A. If it's less than a value, execute B.

I tried doing this by nesting some while statements within the if loop. This isn't working. Basically what's happening is if the photoresistor is above the specified value when the button is pressed, it'll execute A. If it's below the specified value when button is pressed (i.e. I turn the lights off and press the button), it executes B.

How can I write the code in a way that when the button gets pressed, the code executes A and when I turn the light off it executes B. Any and all help is appreciated. Full code is attached.

// LCD Library
#include <LiquidCrystal.h>

// Constants
const int v1 = 6; // Valve 1 pin
const int v2 = 7; // Valve 2 pin
const int pump = 8; // Pump pin
const int btn = 9;
const int alarm = 10; // Alarm Pin
const int photo = A1; // Photoresistor pin
const int temp = 42; // Temperature sensor pin

// Variables
int btnState = 0;

// Initialize LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//********************************************************************************************************************//

void setup()
{
// Make sure all valves and pump begin in closed and off positions
digitalWrite(v1, LOW); //Valve 1 closed
digitalWrite(v2, LOW); //Valve 2 closed
digitalWrite(pump, LOW); //Pump off

// Begin serial monitor for debugging and display some LCD stuff
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Intializing");
delay(3000);
lcd.clear();

// Set pinmodes
pinMode(v1, OUTPUT); // Valve 1 set as output
pinMode(v2, OUTPUT); // Valve 2 set as output
pinMode(pump, OUTPUT); // Pump set as output
pinMode(btn, INPUT); // Push button set as input
pinMode(alarm, OUTPUT); // Alarm set as output
pinMode(photo, INPUT); // Photoresistor set as input
pinMode(temp, INPUT); // Temperature sensor set as input
}

//********************************************************************************************************************//

void loop()
{
int temp1 = digitalRead(temp); // Read digital val of 12 to obtain temp in boil kettle
int v = analogRead(photo);
btnState = digitalRead(btn);
Serial.println(v);

// Mash Transfer Process:
if (btnState == HIGH){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Transfering");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
analogRead(v);
while(v > 300){
digitalWrite(v1, HIGH);
digitalWrite(v2, HIGH);
delay(5000);
digitalWrite(pump, HIGH);
}
while(v < 200){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Transfer Complete");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(pump, LOW);
delay(2000); // Delay to make sure pump is off
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
delay(5000); // Make sure valves close
// Alarm sequence
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
break;
}
}

// No analog vals read or switches switched, everything closed / off
else{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Suh dude");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
digitalWrite(pump, LOW);
}
delay(100);
}

I thought a function decleration with a simple if else statement would be the fix. However, when it runs through the first if statement in void Transfer() function, it seems to run through the valve openings and pump power on but then shuts everything off immediately after that - doesn't keep it running. Here's the new code.

//BREWERY CODE//

// LCD Library
#include <LiquidCrystal.h>

// Constants
const int v1 = 6; // Valve 1 pin
const int v2 = 7; // Valve 2 pin
const int pump = 8; // Pump pin
const int btn = 9;
const int alarm = 10; // Alarm Pin
const int photo = A1; // Photoresistor pin
const int temp = 42; // Temperature sensor pin

// Variables
int btnState = 0;

// Initialize LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//********************************************************************************************************************//

void setup()
{
// Make sure all valves and pump begin in closed and off positions
digitalWrite(v1, LOW); //Valve 1 closed
digitalWrite(v2, LOW); //Valve 2 closed
digitalWrite(pump, LOW); //Pump off

// Begin serial monitor for debugging and display some LCD stuff
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Intializing");
delay(3000);
lcd.clear();

// Set pinmodes
pinMode(v1, OUTPUT); // Valve 1 set as output
pinMode(v2, OUTPUT); // Valve 2 set as output
pinMode(pump, OUTPUT); // Pump set as output
pinMode(btn, INPUT); // Push button set as input
pinMode(alarm, OUTPUT); // Alarm set as output
pinMode(photo, INPUT); // Photoresistor set as input
pinMode(temp, INPUT); // Temperature sensor set as input
}

//********************************************************************************************************************//

void loop()
{
int temp1 = digitalRead(temp); // Read digital val of 12 to obtain temp in boil kettle
btnState = digitalRead(btn);
int v = analogRead(photo);
Serial.println(v);

// Transfer Process:
if (btnState == HIGH){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Transfering");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
Transfer();
}

// No analog vals read or switches switched, everything closed / off
else{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Suh dude");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
digitalWrite(pump, LOW);
}
delay(100);
}

//********************************************************************************************************************//

void Transfer()
{
int v = analogRead(photo);
int temp1 = digitalRead(temp);

if(analogRead(v) > 300){
digitalWrite(v1, HIGH);
digitalWrite(v2, HIGH);
delay(5000);
digitalWrite(pump, HIGH);
analogRead(v);
Serial.println(v);
}
else{lcd.clear(); lcd.setCursor(0,1);
lcd.print("Transfer Complete");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(pump, LOW);
delay(2000); // Delay to make sure pump is off
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
delay(5000); // Make sure valves close
// Alarm sequence
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
return;
}
}

THIS is a thing: https://forum.arduino.cc/index.php?topic=149036.0
-> use code tags!

I suspect part of your trouble is this

  if(analogRead(v) > 300){

When you probably actually mean

  if(analogRead(photo) > 300){

GOT IT. Thanks.

// LCD Library
#include <LiquidCrystal.h>

// Constants
const int v1 = 6; // Valve 1 pin
const int v2 = 7; // Valve 2 pin
const int pump = 8; // Pump pin
const int btn = 9;
const int alarm = 10; // Alarm Pin
const int photo = A1; // Photoresistor pin
const int temp = 42; // Temperature sensor pin

// Variables
int btnState = 0;

// Initialize LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//********************************************************************************************************************//

void setup()
{
// Make sure all valves and pump begin in closed and off positions
digitalWrite(v1, LOW); //Valve 1 closed
digitalWrite(v2, LOW); //Valve 2 closed
digitalWrite(pump, LOW); //Pump off

// Begin serial monitor for debugging and display some LCD stuff
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Intializing");
delay(3000);
lcd.clear();

// Set pinmodes
pinMode(v1, OUTPUT); // Valve 1 set as output
pinMode(v2, OUTPUT); // Valve 2 set as output
pinMode(pump, OUTPUT); // Pump set as output
pinMode(btn, INPUT); // Push button set as input
pinMode(alarm, OUTPUT); // Alarm set as output
pinMode(photo, INPUT); // Photoresistor set as input
pinMode(temp, INPUT); // Temperature sensor set as input
}

//********************************************************************************************************************//

void loop()
{
int temp1 = digitalRead(temp); // Read digital val of 12 to obtain temp in boil kettle
btnState = digitalRead(btn);
int v = analogRead(photo);
Serial.println(v);

// Transfer Process:
if (btnState == HIGH){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Transfering");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
Transfer();
}

// No analog vals read or switches switched, everything closed / off
else{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Suh dude");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
digitalWrite(pump, LOW);
}
delay(100);
}

//********************************************************************************************************************//

void Transfer()
{
int temp1 = digitalRead(temp);

while (analogRead(photo) > 350){
digitalWrite(v1, HIGH);
digitalWrite(v2, HIGH);
delay(5000);
digitalWrite(pump, HIGH);
}
while (analogRead(photo) < 250){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Transfer Complete");
lcd.setCursor(0,2); // Set cursor to bottom row of LCD
lcd.print("Temp: ");
lcd.println(temp1); // Display temp reading
digitalWrite(pump, LOW);
delay(2000); // Delay to make sure pump is off
digitalWrite(v1, LOW);
digitalWrite(v2, LOW);
delay(5000); // Make sure valves close
// Alarm sequence
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
tone(alarm, 2000);
delay(500);
noTone(alarm);
delay(500);
break;
}
}

jeebsinc:
GOT IT. Thanks.

Obviously not! Please use code tags when posting code!