Code help needed for project

Hi all,
I am new to Arduino.
I am trying to code a program with a liquid level sensor controlled by a toggle switch. So if the liquid is low a buzzer will alarm.There is an LCD showing the state of sensor.and LEDs showing state of sensor. If the switch is low LCD should display bypassed and only blue Led should lit. If liquid level is good geen Led should lit and LCD display Monitoring.
But my code is broke somewhere. It loops around lighting all leds and showing both monitoring and bypassed status irrespective of the switch status.
I am posting code here.
Plese help

#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);

int buzzer = 2;
int Liquid;
int switch_pin = 11;
int redPin = 3;                        
int greenPin = 4;                       
int bluePin = 5; 
int switch_pinstate = 0;


void setup() {
 Serial.begin(9600);
 pinMode(9,INPUT);
 pinMode(switch_pin, INPUT);
 lcd.init();                      // initialize the lcd 
 lcd.backlight();
 pinMode(redPin, OUTPUT);              
 pinMode(greenPin, OUTPUT);            
 pinMode(bluePin, OUTPUT);   
 }

void loop() {
 switch_pinstate = digitalRead(switch_pin);
  Liquid=digitalRead(9);
 Serial.print("lqd= ");
 Serial.println(lqd,DEC);
 delay(200);


 if(Liquid == 1)   {
 digitalWrite(greenPin,HIGH);
 digitalWrite(bluePin,LOW);
 digitalWrite(redPin,LOW);
 Serial.println("Monitoring");
 lcd.clear();
 lcd.setCursor(1,0);
 lcd.print("Monitoring");
 noTone(2);
 }

if(Liquid == 0)
{
 Serial.println("lqd Low ");
 lcd.clear();
 lcd.setCursor(1,0);
 lcd.print("lqd Low!");
 tone (2,261,250);
 digitalWrite(redPin,HIGH);
 digitalWrite(greenPin,LOW);
 digitalWrite(bluePin,LOW);
}


  else{
  (switch_pinstate == LOW);
  digitalWrite(bluePin,HIGH);
  digitalWrite(redPin,LOW);
  digitalWrite(greenPin,LOW);
  Serial.println("Bypasssed!");
  noTone(2);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Bypasssed!");
  delay (500);
}}
  

 

Thanks guys

Hello
Insert some Serial.println() at POI to see what happens.

 if (Liquid == 1)   {
    ...
  }

 if (Liquid == 0)   {
    ...
  }
  else
  {
    ...
  }

If Liquid equals 1, the first if evaluates to true so that block will be executed; next the second if evaluates to false so the associated else block will be executed.

What sterretje said makes sense, but also, what kind of sensor do you have connected to pin 9?

Thank you for the reply
so instead of else should i try with another "if statement?

No. Which values can Liquid have? In your code, you seem to be trying to cater for 3 states; Liquid can only display 2 states and your description also only mentions 2 states (s far as I understand it.

So that part of the code could be

  if (Liquid == 1)   {
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
    Serial.println("Monitoring");
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Monitoring");
    noTone(2);
  }
  else
  {
    (switch_pinstate == LOW);
    digitalWrite(bluePin, HIGH);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    Serial.println("Bypasssed!");
    noTone(2);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Bypasssed!");
    delay (500);
  }

But you have another input pin that you don't use; maybe you should make use of that as well to influence the flow of your program.

Note:
Seeing digitalRead(), are you using pull-up or pull-down resistors? If they are not on the 'sensors', your inputs can be floating and just give random values.

Hi @projectexplorer
there is a basic error in your code.
It gives an error in the compilation, because the variable "lqd" has not been defined:

RV mineirin

Your post was MOVED to its current location as it is more suitable.

A post was split to a new topic: Can I2C and SPI be used together within the same sketch?

This makes no sense: the test between parentheses isn't caught by anything; it's like writing true; on it's own line. Perchance you meant:

else if (switch_pinstate == LOW)
{
  // instructions
}

or maybe

switch_pinstate = LOW;

if you wanted to modify the variable?

Hi
Thanks for the reply. I dont have resistors connected to the sensors. its has inbuilt resistors. You are right i am using 2 states only. 1 and 0.
I will try this code and let you know. Thanks alot

Yea. The issue is different. even if the switch pin is off the loop continues.
Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.