I’m making a device to measure the level of water in a tank. I’m using the MaxSonar EZ1 rangefinder and an LCD keypad shield from RobotShop.com
The problem is, I’m using the pulse width function of the sensor to get the distance, and reading that value into digital port 3 on the arduino. Now when I try to print the calculated distance to the LCD screen, it prints the ASCII value, and not the int value.
//example use of LCD4Bit_mod library
#include <LCD4Bit_mod.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);
//Key message
char msgs[5][15] = {" Menu 1 ",
"Up Key OK ",
"Down Key OK ",
" Menu 2 ",
"Select Key OK" };
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
const int pwPin = 3;
long pulse, inches, cm;
int level;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.printIn("Ultrasonic Level");
}
void loop()
{
//char val[4];
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 >=0)
{
lcd.clear();
lcd.cursorTo(1, 0); //line=2, x=0
lcd.printIn(msgs[key]);
Serial.print(key);
Serial.print(msgs[key]);
Serial.println();
}
if (key == 0)
{
pulse = pulseIn(pwPin, HIGH);
level = pulse / 147;
Serial.print(level);
lcd.cursorTo(2,0); // I can't print the value
lcd.printIn("Level: "); // 'level' to the LCD
lcd.print(level); // here
}
}
}
}
// Convert ADC value 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;
}
I tried using the atoi() function with no success. Any help would be appreciated… I’m trying to make an LCD with a menu which is what most of the code is.
Please don't post code snippets that do not describe the whole problem. When I look at that snippet, the error clearly refers to the itoa line, but the key part of the message refers to the first argument, newLevel, and its type, but you haven't shown the declaration for newLevel.
//example use of LCD4Bit_mod library
#include <LCD4Bit_mod.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);
//Key message
char msgs[5][15] = {" Menu 1 ",
"Up Key OK ",
"Down Key OK ",
" Menu 2 ",
"Select Key OK" };
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
const int pwPin = 3;
long pulse, inches, cm;
int level;
int newLevel;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.printIn("Ultrasonic Level");
}
void loop()
{
//char val[4];
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 >=0)
{
lcd.clear();
lcd.cursorTo(1, 0); //line=2, x=0
lcd.printIn(msgs[key]);
Serial.print(key);
Serial.print(msgs[key]);
Serial.println();
}
if (key == 0)
{
pulse = pulseIn(pwPin, HIGH);
level = pulse / 147;
itoa(newLevel, level, 10);
Serial.print(level);
lcd.cursorTo(2,0); // I can't print the value
lcd.printIn("Level: "); // 'level' to the LCD
lcd.print(newLevel); // here
}
}
}
}
// Convert ADC value 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;
}
I know the data type for level is wrong, since itoa() needs a char…