/*
This is a program to show anolog values (between 0 and 999) on a LCD screen.
To show values on your pc with Serial.println(int) is easy. However to show these same values on a LCD screen is not. My LCD screen only shows ASCI characters. Therefor values have to be translated to ASCII codes. For example to show the value 123 on the LCD screen this will do:
lcd.print(49); // 49 is the ASCII code for 1
lcd.print(50);
lcd.print(51);
The following program splits al 3 digit value into three digits and than
translates the digits to ASCII code (= adding 48)
I have used, adapted and stripped the code of a LCD4Bit_Temperature.pde supplied as example with the LCD screen from nuelectronics.
Good luck,
Jaap Blacquière, the Netherlands.
*/
#define LEDPIN 13 // status LED pin
#include <LCD4Bit_mod.h> // initialise LCD library
LCD4Bit_mod lcd = LCD4Bit_mod(2);
int val;
void setup()
{
pinMode(LEDPIN, OUTPUT);
Serial.begin(9600); // Serial monitor to check what should be displayed on the LCD screen
lcd.init();
lcd.clear(); // clear screen of old display
delay(1000); // time to see the cleared screen
lcd.printIn("Start"); // visible start after reset
delay(1000); // time to see the "Start" displayed
lcd.clear(); // clear screen before display of first value
delay(1000); // time to see the cleared screen
}
void loop()
{
char value_string[4];
// 4-caracter string to display a 4-digit value (0-1023)
val = analogRead(1);
// input from sensor (range analogRead is 0-1023)
lcd.cursorTo(1, 0); // LCD first line, first position
getCurrentValue(value_string);
// subroutine for translation of value into string (see below)
lcd.printIn(value_string); // show string on LCD screen
Serial.println(value_string); // show string on serial monitor
// (this is not a value, but a string displaying a value)
digitalWrite(LEDPIN,HIGH);
// LED flashes once every loop
delay(100);
digitalWrite(LEDPIN,LOW);
delay(500); // time value is displayed on screen
}
void getCurrentValue(char *value)
// subroutine for translation of value into string
{
if(val>=1000) // for values > 999 the first digit is always 1
{
value[0]= 49; // the first digit is 1, ASCII value 49
val = val - 1000;
// from now on we work with the last three digits only
}
else
{
value[0]= 32; // the first digit is a space, ASCII value 32
}
int val_1; // second digit
int val_2; // third digit
int val_3; // fourth digit
val_1 = val/100;
// from values 100-999 only the first digit remains
// integers forget digits behind the decimal point
// f.e. 123/100 = 1,23 => 1 remains
val_2 = (val-(100val_1))/10;
// f.e. (123-(1001))/10 = 2,3 => 2 remains
val_3 = val-(100val_1)-(10val_2);
// f.e. 123-(1001)-(102)= 3
value[1]= val_1+48;
// character value of 0 = 48
// f.e. ASCII value of 1 = 49
value[2]= val_2+48; // f.e. ASCII value of 2 = 50
value[3]= val_3+48; // f.e. ASCII value of 3 = 51
}