Hi, im relatively new to using an arduino so please go easy on me. A little background, im trying to fill a container with an amount of water, it doesn't have to be exact but near enough. half way through filling id like to turn on 2 relay outputs that will be connected to a pump and heater and hold temperature.
Anyway ive been reading through some guides and I have come up with the below script. The problem I am having is that it will go through the on, ready and fill case, but then even though the command state changes to move on to the fillheat case it just appears to hang and I cant for the life of me understand why.
//----Libraries------
#include <LiquidCrystal_I2C.h>
//-----Defines-----
#define I2C_ADDR 0x3F
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
//Define all the command states
#define ON 0
#define Ready 1
#define Fill 2
#define FillHeat 3
#define Complete 4
#define Relay 5
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin); //Name lcd pins
//-----CONSTANTS----- (wont change)
const int Startswitch = 3;
const int waterInRelay = 4;
const int heaterRelay = 5;
const int mixPumpRelay = 6;
const int R1 = waterInRelay;
const int R2 = heaterRelay;
const int R3 = mixPumpRelay;
//-----VARIABLES----- (will change)
int commandState = ON; //0=ON, 1=Ready, 2=Fill, 3=Fill/Heat, 4=Complete, 5=Relay
//-----Intrupt counter-----
volatile unsigned long isrCounter;
unsigned long pulseCount;
// Flow sensor FL-S402B = 600 pulses/l
// Flow sensor YF-S201 = 450 pulses/l
int calFactor = 600;
// convert pulses to litres
unsigned long waterVol = pulseCount/calFactor;
// Set volume to stop at = required volume in ltr x calFactor
unsigned long stopCount = 600;//**** SET FOR 1Ltr****
void setup() {
Serial.begin(9600);
pinMode(Startswitch, INPUT_PULLUP); //configure pin 2 as an input and enable the internal pull-up resistor
//configure outputs
pinMode(R1, OUTPUT);
digitalWrite (R1, HIGH);
// Set LCD
lcd.begin (16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home ();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Flow counter"); // signal initalization done
lcd.setCursor(0, 1);
lcd.print ("starting");
delay (3000);
lcd.clear();
attachInterrupt(0, countP, RISING);
} // End of setup
void loop() {
bool Startbuttonstate = digitalRead(Startswitch);
//Serial.println(Startbuttonstate); //print out the value of the pushbutton
switch (commandState) {
//----------------------
case ON:
Serial.println(commandState);
clearLCD();
for (int ONi = 3; ONi > 0; ONi--) { //count down from 3
selectLineOne();
lcd.print("Elmo Industries");
selectLineTwo();
lcd.print(" Hello Dave");
delay (1000);
}
commandState = Ready;
delay(500);
break;
//---------------------
case Ready:
Serial.println(commandState);
clearLCD();
selectLineOne();
//delay(100);
lcd.print("Push button"); //Notify user that the system is ready
selectLineTwo();
lcd.print("to start");
//if (Levelsensorstate) { //(Levelsensorstate == HIGH) { //check to make sure container is not already filled
//LevelActivated();
//delay(500); //blanked out as seems to be tripping even tho switch not made
if (Startbuttonstate) { //(Startbuttonstate == HIGH) { //Wait for start button to be pressed
commandState = Ready;
delay(500);
} else { //if start button is pressed start fill.
clearLCD();
selectLineOne();
delay(100);
lcd.print(" Starting");
delay(2000);
clearLCD();
commandState = Fill; //Start flush countdown
}
break;
//-------------------------
case Fill:
Serial.println(commandState);
digitalWrite (R1, LOW); //water in relay on, solenoid open
delay(100);
noInterrupts();
long pulseCount = isrCounter;
interrupts();
lcd.setCursor(0,0);
lcd.print("Total Vol");
lcd.print (pulseCount);
lcd.setCursor(0,1);
lcd.print(waterVol);
lcd.print(" Ltrs");
if (pulseCount > (stopCount/2))
{
lcd.clear();
selectLineOne();
lcd.print("Starting Heat");
selectLineTwo();
lcd.print("and Mix");
Serial.println("start heat mix");
commandState = FillHeat; //set command state to fill and heat
Serial.println("state switched fill heat");
Serial.println(commandState);
}
break;
//----------------------
case FillHeat:
Serial.println(commandState);
digitalWrite (R1, LOW); //water in relay on, solenoid open
digitalWrite (R2, LOW); //water heater on, solenoid open
digitalWrite (R3, LOW); //mixing pump on, solinoid open
delay(100);
noInterrupts();
interrupts();
lcd.setCursor(0,0);
lcd.print("Total Vol");
lcd.print (pulseCount);
lcd.setCursor(0,1);
lcd.print(waterVol);
lcd.print(" Ltrs,Heat on");
if (pulseCount > stopCount)
{
digitalWrite (R1, HIGH);
lcd.clear();
selectLineOne();
lcd.print("Fill stopped");
selectLineTwo();
lcd.print(waterVol);
lcd.print(" Litres");
Serial.println("complete fill");
commandState = Complete; //set command state to complete
Serial.println("state switched");
Serial.println(commandState);
}
break;
//----------------------
case Complete:
clearLCD();
selectLineOne();
delay(500);
lcd.print(" Water fill");
selectLineTwo();
lcd.print(" completed!");
delay (2000);
commandState = Ready;
break;
case Relay:
Serial.println(commandState);
digitalWrite (R1, LOW);
delay (1000);
digitalWrite (R1, HIGH);
delay (1000);
break;
}
} // end of loop
void countP()
{
isrCounter++;
}
void clearLCD() {
lcd.setCursor(0, 0);
lcd.clear();
}
void selectLineOne() { //puts the cursor at line 0 char 0.
lcd.setCursor(0,0); //position
}
void selectLineTwo() { //puts the cursor at line 0 char 0.
lcd.setCursor(0,1); //position
}
So please any help would be greatly appreciated, or guidance if I could go about this using a different method.