Hi all I am designing a water level sensor. I used a map function to take the voltage divider input on A(1) from 0-1023 to 0-30. I then used a switch case to (hopefully) print out how many gallons of water are remaining but my LCD will ONLY print a 0 and im not sure why? It does not change with the input that is being read in on the analog input on A1. It does properly print "Water remainging:" and "Gallons" just not the proper number!! Here is my code.
/*
\
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
//int analogPin = 1; // potentiometer wiper (middle terminal) connected to a
int val = analogRead(1); // variable to store the value read
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Water Remaining:");
lcd.setCursor(3, 1 );
// Print a message to the LCD.
lcd.print("Gallons");
int waterLevel = map(val, 0, 1023, 0, 1); // read the input pin
switch(waterLevel){
case 0:
lcd.setCursor(0, 1 );
lcd.print("0");
break;
case 1:
lcd.setCursor(0, 1 );
lcd.print("1");
break;
case 2:
lcd.setCursor(0, 1 );
lcd.print("2");
break;
case 3:
lcd.setCursor(0, 1 );
lcd.print("3");
break;
case 4:
lcd.setCursor(0, 1 );
lcd.print("4");
break;
case 5:
lcd.setCursor(0, 1 );
lcd.print("5");
break;
case 6:
lcd.setCursor(0, 1 );
lcd.print("6");
break;
case 7:
lcd.setCursor(0, 1 );
lcd.print("7");
break;
case 8:
lcd.setCursor(0, 1 );
lcd.print("8");
break;
case 9:
lcd.setCursor(0, 1 );
lcd.print("9");
break;
case 10:
lcd.setCursor(0, 1 );
lcd.print("10");
break;
case 11:
lcd.setCursor(0, 1 );
lcd.print("11");
break;
case 12:
lcd.setCursor(0, 1 );
lcd.print("12");
break;
case 13:
lcd.setCursor(0, 1 );
lcd.print("13");
break;
case 14:
lcd.setCursor(0, 1 );
lcd.print("14");
break;
case 15:
lcd.setCursor(0, 1 );
lcd.print("15");
break;
case 16:
lcd.setCursor(0, 1 );
lcd.print("16");
break;
case 17:
lcd.setCursor(0, 1 );
lcd.print("17");
break;
case 18:
lcd.setCursor(0, 1 );
lcd.print("18");
break;
case 19:
lcd.setCursor(0, 1 );
lcd.print("19");
break;
case 20:
lcd.setCursor(0, 1 );
lcd.print("20");
break;
case 21:
lcd.setCursor(0, 1 );
lcd.print("21");
break;
case 22:
lcd.setCursor(0, 1 );
lcd.print("22");
break;
case 23:
lcd.setCursor(0, 1 );
lcd.print("23");
break;
case 24:
lcd.setCursor(0, 1 );
lcd.print("24");
break;
case 25:
lcd.setCursor(0, 1 );
lcd.print("25");
break;
case 26:
lcd.setCursor(0, 1 );
lcd.print("26");
break;
case 27:
lcd.setCursor(0, 1 );
lcd.print("27");
break;
case 28:
lcd.setCursor(0, 1 );
lcd.print("28");
break;
case 29:
lcd.setCursor(0, 1 );
lcd.print("29");
break;
case 30:
lcd.setCursor(0, 1 );
lcd.print("30");
break;
}
}
If anyone can help that would be awesome! I am also taking any suggestions on how to improve my code or any better ways to do this. Thanks guys and gals.
LCD_display.ino (4.22 KB)