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;
}
}