Hi, I write a code to control the water level sensor. There are 3 stages of the sequences:
- Stage 1: When switch1 is ON, the sensor will detect the level. (when it is minimum, the valveInlet will ON)(when it is maximum, the ValveInlet will OFF) stage1 include LED red, buzzer.
- Stage 2: When switch2 is ON, components from stages1 will OFF. Then, the ValveDrain will ON within a minute. LED yellow will ON.
- Stage 3: both switch1 &2 is OFF, LCD print "Detecting Water level.."
Here problem is when I test on the physical components, the sensor detection is slow. And, when S2 ispressed, the stage2 not operated.
Here I attached my code and my simple circuit schematic. How to improve it?
//projectEI fye
#include <LiquidCrystal.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
//defining cup Anemometer
// First Frame
byte wind1[] = {
B00000,
B00000,
B00000,
B00011,
B00111,
B00111,
B00111,
B00011
};
byte wind2[] = {
B11111,
B11111,
B01110,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte wind3[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B01110,
B11111,
B11111
};
byte wind4[] = {
B11000,
B11100,
B11100,
B11100,
B11000,
B00000,
B00000,
B00000,
};
// Second Frame
byte wind5[] = {
B00000,
B00000,
B00000,
B00000,
B01000,
B11100,
B11110,
B01111
};
byte wind6[] = {
B00001,
B00011,
B00111,
B01110,
B00000,
B00000,
B00000,
B00000
};
byte wind7[] = {
B00000,
B00000,
B00000,
B00000,
B00010,
B00111,
B01111,
B11110
};
byte wind8[] = {
B11110,
B01111,
B00111,
B00010,
B00000,
B00000,
B00000,
B00000
};
const int waterlevelsensor = A0; //A0 as water level sensor
int sensorValue = 0; //a variable for the sensor
int ledGreen = 3; //set LED green to D3
int buzzer = 4; //set buzzer and LED red to D4
const int ValveInlet = 8; //set solenoid valve1 (inlet) to pin8
const int ValveDrain = 9; //set solenoid valve1 (drain) to pin9
int switch1 = 1; //set Rockerswitch (1) to D1
int switch2 = 2; //set Rockerswitch (2) to D2
int switch1State = 0;
int switch2State = 0;
void setup() {
// declare pin to be an input:
pinMode(switch1,INPUT);
pinMode(waterlevelsensor,INPUT);
//digitalWrite(switch1,LOW);
pinMode(ledGreen,OUTPUT);
pinMode(buzzer,OUTPUT); //set Buzzer and LED Red to D4 as output
pinMode(ValveInlet, OUTPUT); //set Solenoid valve as output pin
Serial.begin(9600);
// initialize the LCD
lcd.begin();
lcd.noBlink();
lcd.setCursor(1,0);
lcd.print("POLITEKNIK PSA");
delay(2500);
lcd.setCursor(3,1);
lcd.print("Shah Alam ");
delay(3000);
lcd.clear();
lcd.noBacklight(); // turn off backlight
lcd.backlight(); // Turn on the blacklight and print a message.
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Water Level");
delay(1500);
lcd.setCursor(0,1);
lcd.print("Detecting System");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("for PlasmaThawer");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Helmer QuickThaw");
delay(1000);
lcd.setCursor(5,1);
lcd.print("(DH4)");
delay(3000);
lcd.clear();
}
// the loop routine runs over and over again forever:
void updatelcd() {
//if the RockerSwitch is 0, no mode (standby mode) is selected:
digitalWrite(3,LOW);
digitalWrite(4,LOW);
lcd.clear();
//------------------
//creating all the characters
lcd.createChar(1 , wind1); //Numbering should start at 1, not 0
lcd.createChar(2 , wind2);
lcd.createChar(3 , wind3);
lcd.createChar(4 , wind4);
lcd.createChar(5 , wind5);
lcd.createChar(6 , wind6);
lcd.createChar(7 , wind7);
lcd.createChar(8 , wind8);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.write(1);
lcd.setCursor(0,1);
lcd.write(2);
lcd.setCursor(1,0);
lcd.write(3);
lcd.setCursor(1,1);
lcd.write(4);
lcd.setCursor(3,0);
lcd.print("Detecting");
lcd.setCursor(3,1);
lcd.print("water level");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write(5);
lcd.setCursor(0,1);
lcd.write(6);
lcd.setCursor(1,0);
lcd.write(7);
lcd.setCursor(1,1);
lcd.write(8);
lcd.setCursor(3,0);
lcd.print("Detecting");
lcd.setCursor(3,3);
lcd.print("Water level..");
delay(500);
lcd.clear();
}
void loop(){
int switch1State_ON=digitalRead(switch1==HIGH);
int switch1State_OFF=digitalRead(switch1==LOW);
updatelcd(); ///here
//if the switch1 is presssed, Mode 1 for detecting and refilling the water:
while (digitalRead(switch1State_ON))
{
int sensorValue = analogRead(waterlevelsensor);
sensorValue = analogRead(waterlevelsensor);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\n");
delay(10);
lcd.clear();
{ if (10<=sensorValue<=100) //Minimum Level
lcd.setCursor(4,0);
lcd.print("Mode 1");
delay(500);
{
int i=0;
lcd.backlight();
lcd.setCursor(1,0);
lcd.print(" Water level:");
delay(500);
lcd.setCursor(2,1);
lcd.print("Minimum...");
digitalWrite(ValveInlet,HIGH);
delay(15000);
digitalWrite(ValveInlet, LOW);
delay(500);
do{
i++;
tone(buzzer, 450);
delay(200);
noTone(buzzer);
delay(100);
}
while(i<2);
delay(200);
lcd.clear();
}
if(101<=sensorValue<=720) //Maximum level
{
int i=0;
lcd.setCursor(1,0);
lcd.print(" Water level:");
delay(500);
lcd.setCursor(2,1);
lcd.print("Maximum...");
digitalWrite(ValveInlet, LOW);
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
delay(1000);
digitalWrite(3,LOW);
delay(100);
digitalWrite(3,HIGH);
delay(100);
digitalWrite(3,LOW);
delay(50);
lcd.clear();
}
}
//if the switch2 is presssed, Mode 2 for draining the water:
while(switch2State==HIGH)
{ lcd.setCursor(4,0);
lcd.print("Mode 2");
digitalWrite(ValveDrain, HIGH); //the draining valve is open
delay(3000);
digitalWrite(ValveDrain, LOW); //the draining valve is closed after 3secs
lcd.setCursor(1,0);
lcd.print(" Draining");
delay(500);
lcd.setCursor(3,1);
lcd.print("water...");
lcd.clear();
}
//if both switches 1&2 are ON:
while (digitalRead(switch1State==LOW)&&digitalRead(switch2State==LOW))
{
lcd.clear();
lcd.print("Finished");
digitalWrite(ledGreen,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(ValveInlet, LOW);
updatelcd();
}
}}```

