Go Down

Topic: Show analogRead(values) on a LED screen (Read 4656 times) previous topic - next topic

Jaaptina

/*
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-(100*val_1))/10;        
         // f.e. (123-(100*1))/10 = 2,3 => 2 remains
       val_3 = val-(100*val_1)-(10*val_2);  
         // f.e. 123-(100*1)-(10*2)= 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      
}      


mem

hi Jaap,

There are easier ways of converting a number to a string. Also I think you are not explicitly adding a null at the end of the string.

There is a brief writeup in the playground that shows how to use the itoa C library function to create a string from a number: http://www.arduino.cc/playground/Code/PrintingNumbers

And another suggestion was in this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210730025

The Arduino release 0012 has just been made available and this has a built in routine to print decimal numbers to the lcd screen that works just like the Serial.print commands.

Jaaptina

#2
May 26, 2009, 10:22 pm Last Edit: Jul 17, 2011, 09:54 am by Jaaptina Reason: 1
// Small math game based on Nuelectronics LCD screen with keypad
// Printing values on LCD screen with "Itoa()" function
Code: [Select]

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

char msgs[2][5] = {" * ", " + " };  
int  adc_key_val[5] ={30, 150, 360, 535, 760 }; // Analog in defines witch key is pressed
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int value0;   // new random value every loop
int value1;   // first number
int value2;   // second number
int value3;   // 1*2
int value4;   // a value close to value3 is answer proposal (to reduce key presses)
int x;        // choose char * or + to display
char buf[12]; // "-2147483648\0" a probabely far to big buffer


void setup() {
 lcd.init();
 lcd.clear();
 lcd.printIn("Start Game");    // multiplication game
 delay(1000);    
}

void loop() {
 value0 = random(1,99);        // without this you get the same "random" numbers each reset
 adc_key_in = analogRead(0);   // read the value from the sensor  
 key = get_key(adc_key_in);      // convert into key press
 if (key != oldkey){            // if keypress is detected
   delay(50);                    // wait for debounce time
   adc_key_in = analogRead(0); // read the value from the sensor  
   key = get_key(adc_key_in);      // convert into key press
   if (key != oldkey){                  
     oldkey = key;
     if (key == 4){            // most left button starts a new sum
       if (value0 < 50){          // multiply when random value0 is below 50
         value1 = random(1,9);    // values used are below ten
         value2 = random(1,9);
         value3 = value1*value2;
         x = 0;                   // choose char * to display
       }
       else{                      // addition when random value0 is >= 50
         value1 = random(1,50);   // values used are below 50
         value2 = random(1,49);
         value3 = value1+value2;  // add up to 99 maximum
         x = 1;                   // choose char + to display
       }
       value4 = value3 + random(-5,+5); // proposed answer near correct answer
       if (value4 <= 0){
         value4 = value4 + 5;  //
       }      
       lcd.clear();
       delay(500);
       lcd.cursorTo(1, 0);        
       lcd.printIn(itoa(value1, buf, 10));  // display first number
       lcd.printIn(msgs[x]);                // display char * or +
       lcd.printIn(itoa(value2, buf, 10));  // display 2nd number
       delay(1000);
       lcd.cursorTo(2, 0);      
       lcd.printIn(itoa(value4, buf, 10)); // display proposed answer
     }
     else{
       if (key == 1){
         value4 = value4 + 1;  // up-button adds 1 to the answer
         lcd.cursorTo(2, 0);      
         lcd.printIn(itoa(value4, buf, 10));
       }
       else{
         if (key == 2){
           if (value4 == 0){
             value4 = value4 +1;
           }
           else{
             value4 = value4 - 1;  // down-button subtracts 1 from answer
           }
           lcd.cursorTo(2, 0);      
           lcd.printIn("    ");    // to remove trailing zero below ten
           lcd.cursorTo(2, 0);
           lcd.printIn(itoa(value4, buf, 10));
         }
         else{
           if (key == 0){          // right-button to check the answer
             lcd.cursorTo(2, 6);
             if (value4 == value3){
               lcd.printIn("Okay");
               delay(1500);
               lcd.clear();
               delay(500);
               lcd.cursorTo(1, 0);
               lcd.printIn("New game");
             }
             else{
               lcd.printIn("False");  // wrong answer, new chance
               delay(1500);
               lcd.cursorTo(2, 6);
               lcd.printIn("     ");
             }
           }
         }
       }
     }
   }
 }
}

// Convert analog in to key number
int get_key(unsigned int input){
 int k;
 for (k = 0; k < NUM_KEYS; k++){
   if (input < adc_key_val[k]){
     return k;
   }
 }
 if (k >= NUM_KEYS){
   k = -1;                  // No valid key pressed
   return k;
 }
}


Go Up