I am writing a program that requires the user to enter a number using the buttons on the LCD keypad shield. I need the number to be entered and stored as a variable so that it can be used in a math equation and then used later in the void loop.
The way that I’m thinking is by setting up a while statement that continues until the user is finished entering the number and presses SELECT (x < 800). Once SELECT is pressed, the number is used in an equation and then the code proceeds to the next section of the void loop, exiting the while loop.
My code is correct for the entering of the number and the usage of the buttons but when I go to convert the number so that it can be used in an equation I’m not sure what to do. I set conv = targ and then subject conv to an equation but it gives me weird numbers that have no relation (ex: 10000 = 464, 12345 = 420, 20000 = 416).
If anyone has any guidance it would be greatly appreciated!
#include <LiquidCrystal.h>
int i = 0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int a = 0; //used for moving the cursor left or right.
int conv = 0;
byte numbr[5]; // This will hold 4 bytes, and if global, will init to all zeros
byte indx; //This will hold an index to the array. If global, it will init to 0
byte targ; // target value
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Enter target kg:");
}
void loop() {
while ( i < 4 ){ //I don't think this is right here. It might be x < 800?
int x;
x = analogRead (0);
lcd.setCursor(a,1);
if (x < 60) {
//right button
if (indx < 4 ) {
indx++;
a++;
}
}
else if (x < 200) {
//up button
if (numbr[indx] < 9 ) {
numbr[indx]++;
lcd.print(numbr[indx]);
}
}
else if (x < 400){
//down button
if (numbr[indx] > 0 ) {
numbr[indx]--;
lcd.print(numbr[indx]);
}
}
else if (x < 600){
//left button
if (indx > 0) {
indx--;
a--;
}
}
else if (x < 800){
targ = 0;
for (int i = 0; i < 4; i++) {
targ = (targ * 10) + numbr[i];
lcd.setCursor(0,1);
lcd.print("Value saved");
delay(2000);
}
conv = targ; //split into two equations for reference. What do I do here?
conv = 2 * targ;
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(conv); //it gives me weird numbers here
}
delay(100);
}
//I will put another function here that will use the conv number and loop continuously.
}
Your displayed number looks weird because you are not converting the value to ASCII for display.
You need to break up the value into single digits and convert them to ASCII to send to the display.
Value = 25
First digit = value/10 = 2, add 48 = 50, ASCII for "2". Send this value to the LCD.
Second digit = value%10 = 5, add 48 = 53, ASCII for "5". Send this to the display.
just to make sure that you are using lcd.print() correctly. I don't think it should be necessary to convert your number to ascii characters, I think print() should do that.
If that works correctly your problem is with converting the input digits to the number you want.
I think your code uses the up and down buttons to increase or decrease the current digit and the right button to move to the next digit.
It looks like your calculation of targ is correct - with a possible problem that you may be starting from the wrong end. That should be obvious from the numerical errors you get.
It's a good idea to post examples of the data you expect to see and the data you actually see.
Robin2:
just to make sure that you are using lcd.print() correctly. I don't think it should be necessary to convert your number to ascii characters, I think print() should do that.
...R
I am still learning! Thanks for that, I will try it on my sketch.
So I got it to work as well as I need it to. What I did was change the variable targ from a byte to an int.
I then changed my arbitrary conversion equation (conv = 2 * targ) to conv = 10 * targ.
In the end this gave me numbers which were the same as what I inputted BUT it truncated the value of the last integer inputted to a value of 0.
ex: 12345 shows on the LCD as 12340
ex: 10000 shows on the LCD as 10000
Since I'm dealing with such large numbers this is close enough for me to work with.
ONE MORE THING, does anyone know what I would have to put in the while loop to make it loop until I press select? If anyone needs pictures of the characters on the screen or anything else I can post them.
rather than a while loop, look into a state change method like this, partial code...
void loop()
{
if (state == 0)
{
if (!printed)
{
lcd.setCursor(0,0);
lcd.print("enter your value:)
printed = true;
}
lcd.setCursor(1,0);
lcd.print(analogRead(A0);
lcd.print(" ");
delay(100);
if (buttonState == LOW && lastButtonState ==HIGH)
{
savedValue = analogRead(A0);//<<<<<<<<<< save the variable
}
state = 1;// move on to main body of the program
printed = false;// reset the flag
}
else if (state == 1)
{
//the rest of your code...
// add an event to set the state back to zero if you want to edit
So I ended up with a solution to my original problems! To solve the solution to the while loop I made an arbitrary constant (int y = 0) with the while loop being: while ( y == 0). when the user pressed select (analogRead < 800), it went through what I have posted earlier and then had y++ at the end. This then terminated the while loop allowing my code to continue.
Thanks for all the help!
void loop(){
while ( y == 0) {
//do the button check state stuff
else if( x < 800){
//the select button is pressed
targ = 0;
for (int i = 0; i < 4; i++) {
targ = (targ * 10) + numbr[i];
lcd.setCursor(0,1);
lcd.print("Value Saved");
delay(1000);
}
weight = 10 * targ;
//number is converted and saved as weight. the last digit reverts to 0
y++; //this will terminate the while loop by making y != 0
}
}
//continue with the void loop, the while loop has been exited.
}