LCD Shield - Getting different modes with the keypad

I'm writing a project where a user will use the keypad to select a number (up and down) and then the (select) button to change to a new question
How do I do this?
And also I want the Arduino to remember the number selected to use later in the program.
Any help is greatly appreciated
Thanks
This is the code so far, am I on the right track?
#include <LiquidCrystal.h>

LiquidCrystal lcd(8,7,6,5,4,3);

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

byte percentage [8] = {
0b00000,
0b00000,
0b11001,
0b11010,
0b00100,
0b01011,
0b10011,
0b00000,
};

byte litres [8] = {
0b00000,
0b00000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b11111,
};

void setup() {
lcd.begin(16,2);
int readkey = 0;

lcd.createChar(0,percentage);
lcd.createChar(1,litres);
}

void loop() {

lcd.setCursor(0,0);
lcd.print("Hello");
delay(1000);

lcd.setCursor(0,1);
lcd.print("Welcome");
delay(4000);

lcd.clear();

lcd.setCursor(0,0);
lcd.print("Select Machine");

lcd.setCursor(4,1);
lcd.print("1");
int number = 1;
int readkey

else if(readkey<195) { //up button
lcd.print(number++);
delay(5)

number = number + 1;

}
int readkey
else if(readkey<380) { // down button
lcd.print(number--);
delay(5)

number = number - 1

}
int readkey
else if(readkey<790){ // select button
digitalRead(number)

number = Machine

lcd.clear();
lcd.setCursor(0,0);
lcd.autoscroll();
lcd.print("Select Current Concentration");
}

lcd.setCursor(6,1)
lcd.print(byte(0));

lcd.setCursor(4,1);
lcd.print("5 ")
int perc = 5
int readkey

if(readkey<195) {
lcd.print(perc++)
delay(5)

perc = perc + 1

}
int readkey
else if(readkey<380){
lcd.print(perc--);
delay(5)

perc = perc - 1

}
int readkey;
if(readkey <790) {
digitalRead(perc)

perc = CC

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select current volume");

lcd.setCursor(8,1);
lcd.print(byte(1));

lcd.setCursor(4,1)
lcd.print("200 ");
int litres = 200

if(readkey<195){
lcd.print(litres++)
delay(5)

litres = litres + 1

}

You are not likely to get much useful help until you provide more information.

If you want help fixing up the code you have already written then you should post that code.

If you are trying to get someone to write the code for you then you will have to give specific information about the hardware that you are using.

Don

Ok thanks Don
I've added the code, what do you think?
I'm using the arduino uno and ide to write the code, the lcd shield I got from Maplin electronics.
Does that help?

I've added the code, what do you think?

I think you should have
(1) put it in a new reply and
(2) put it in a 'code box' as outlined in step 7 of the thread titled 'How to use this forum - please read.'

You have not provided enough information to identify your display. Can you verify that you have connected it properly and are using an appropriate library. In other words have you been able to display a simple message on your LCD?

Some comments, in no specific order, about your code:

You should avoid clearing the display if it is not necessary. If you do clear the display then you do not have to set the cursor to (0,0) since that is done as part of the clearing process.

You should identify what the 'magic numbers' (195, 380, 790 etc.) represent. That is what comments are for.

You might want to avoid location '0' for one of your special characters as that location presents some problems when trying to display the character using the Arduino IDE.

You don't need to create a special character for '%' as the character set already includes one at 0x25.
You don't need a special character for 'L' either unless you have a really strange LCD controller.

Are you sure you want to turn autoscrolling on? If you want it on all the time then do so in setup(). If not then your loop() code should probably turn it off somewhere.

Don

No Don
I have not got anything to work on my display yet.
I take on board what you said about setting the cursor
The numbers I research where the signal from each of the buttons on the display
Do you understand what I'm trying to do? if so could you help me write a program for it?
thanks

Hi Don
I've tested some of the example programs and they run fine
could you (or someone else) write me a small code for the up/down buttons and then when you press select the board remembers the number.
Thanks
Thomas

#include <LiquidCrystal.h>

LiquidCrystal lcd(8,7,6,5,4,3);





#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

byte percentage [8] = {
  0b00000,
  0b00000,
  0b11001,
  0b11010,
  0b00100,
  0b01011,
  0b10011,
  0b00000,
};

byte litres [8] = {
  0b00000,
  0b00000,
  0b10000,
  0b10000,
  0b10000,
  0b10000,
  0b10000,
  0b11111,
};


void setup() { 
lcd.begin(16,2);
int readkey = 0;

lcd.createChar(0,percentage);
lcd.createChar(1,litres);
}



void loop() {

lcd.setCursor(0,0);
lcd.print("Hello");
delay(1000);

lcd.setCursor(0,1);
lcd.print("Welcome");
delay(4000);

lcd.clear();

lcd.setCursor(0,0);
lcd.print("Select Machine");

lcd.setCursor(4,1);
lcd.print("1");
int number = 1;
int readkey

 else if(readkey<195) { //up button
     lcd.print(number++);
     delay(5)
     
 number = number + 1;
 
   }
   int readkey
   else if(readkey<380) { // down button
     lcd.print(number--);
     delay(5)
     
 number = number - 1
 
   }
int readkey
else if(readkey<790){ // select button
digitalRead(number)

number = Machine

lcd.clear();
lcd.setCursor(0,0);
lcd.autoscroll();
lcd.print("Select Current Concentration");
}

lcd.setCursor(6,1)
lcd.print(byte(0));

lcd.setCursor(4,1);
lcd.print("5  ")
int perc = 5
int readkey

if(readkey<195) {
lcd.print(perc++)
delay(5)

perc = perc + 1

}
int readkey
else if(readkey<380){
lcd.print(perc--);
delay(5)

perc = perc - 1

}
int readkey;
 if(readkey <790) {
  digitalRead(perc)

  perc = CC
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Select current volume");

  lcd.setCursor(8,1);
  lcd.print(byte(1));

  lcd.setCursor(4,1)
  lcd.print("200  ");
  int litres = 200

  if(readkey<195){
    lcd.print(litres++)
    delay(5)

    litres = litres + 1
    
    
 }

You still have not identified your LCD shield although I gather it is one of those that also has several push buttons but not what I would call a keypad which typically has 12 or 16 push buttons.

That being the case then you should really read the other 'sticky' thread that accompanies the one on 'How to use the forum'. It is called 'Warning to users of some vendors LCD keypad shields'.

Again, assuming you are dealing with such a device your program code has to actually read an analog value from the shield which will signify which key has been pressed. I don't see where your program does that.

I suppose that if I knew which device you were using I could go to the appropriate web page and maybe find a link to some sample code. It probably looks something like this: To visit the website support page please click here

Don

Don I found this code online which helps me count up and down, how do I 'select' the number then?

Thanks

//Using LiquidCrystal library
#include <LiquidCrystal.h>

/*******************************************************

This program will add a value and display it.  To increment
use the UP and DOWN button.

Please make sure what version of the LCD Keypad Shield you are using and uncomment
the correct threshold area.


//George Ferrao
//george.ferrao@scarabware.com

*******************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnUP     1
#define btnDOWN   2


// declair funtion to read the buttons

int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor

 // For V1.1 us this threshold
 /*
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
*/ 

 // For V1.0 comment the other threshold and use the one below:
  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN;
}

void setup()
{
 lcd.begin(16, 2); // start the library
}
  
void loop()
{

 lcd_key = read_LCD_buttons();
 static int value = 0;
 if(lcd_key == 1)
  {
    value = constrain(value+1,-30,30);  // I only wanted it to count from (-30)-(+30) and back
  }
 else if (lcd_key == 2)
  {
    value = constrain(value-1,-30,30);  // I only wanted it to count from (-30)-(+30_)and back
  }
  
  lcd.setCursor(4,0);
  lcd.print("COUNT: ");
  lcd.setCursor(11,0);
  lcd.print(value); // displays the count
  lcd.print(" ");  //This space is here to clear the double digit area.
  
  delay(150);
}

I'm not prepared to spend much more time on this until I know what I am dealing with. Is there some reason why you refuse to identify your shield?

Don

Its a 16x2 LCD display from Maplin with 5 buttons code (NO7DH), I appreciate you taking up your time to help me, could you show me how to use the select button to save a value e.g
Arduino LCD displays number 7
Machine = 7
And then I could use the Machine command later in my program.
Thanks

Why don't you try the example code provided by Maplin? I believe it does essentially what you are asking for.

I suggest that you start with the 'Minimal Display Example' before you try to deal with reading the push buttons.

I won't attempt to provide any code for you because I am not particularly proficient in 'C' and won't be able to test it. I don't have a shield like yours and really don't feel like building up the circuit on a breadboard.

Don

Where do I find the example code?

Arduino7:
Where do I find the example code?

Right where I guessed it would be back in reply # 7. At that time I wasn't sure if I was looking at the right device so I didn't provide an active link.

Don

Hi.

I played around some 3 years ago with a LCD Keyshield.
At that time i saw someone ask a question over here about characters and their codes for LCD displays.
So i changed a standard example code for that LCD Keyshield, so it would help you find a character and it's ASCII code, and posted it.
After getting the taste of it, i wondered if i could create some kind of menu.
So i changed the sketch again.
It now offers a mode where you can set some stuff like language and some backlight options, and the standard mode where you can look for certain ASCII characters.
It's not a great sketch to some world domination device (in fact it is a bit messy), but it was great fun to sort it out and learn.

I can't put the sketch in code tags, because it is too big.
So i'll try to attach it.
See if this is similar to what you want, and if you understand what's going on in there.

Also, check for changes you need to do before uploading it to your Arduino.
For instance, my shield has the LCD connected to the Arduino in a slightly different order, you need to correct it for your display.

If this is not what you are looking for, i guess i have trouble understanding what you want too.

LcdKeyShieldDX_Menu_Dim_Lang_Flash.ino (11.4 KB)

Sorry MAS3 I couldn't download your file.
The project I have I working with a local factory on an automated cooling system. I want workers to select things like (Machine, Concentration etc.) in order with my program. I'm struggling to find out how to ensure the program stalls whilst a user is pressing a number.
Any way you could help?
Thanks

I'm not sure what you're asking here.
The display and the buttons can be handled whilst the other parts are still running on the background.
If you need your user to stop whatever is running on the background, then you can have him select some option that's supposed to be available on any screen, and hit the enter/select key.

But if you have to ask questions like these, do you think it is a good idea to have your solution running in some factory environment ?
Better do some real good debugging before you go live with whatever you are building.

By the way, seen my sign off ?
The code i attached has some delays in it, because it is a proof of concept and not some serious code.
You should get rid of them anyway, and use the advice given in my sign off.
I have worked with another user in the Dutch forum, where we have extended the menu built this way to a few levels and sublevels.
The process he has been working on, still runs during all of this.
You decide whether the display of some menu, or the process is most important.
If there is no time to handle the display and key presses, display some acknowledge of a key being pressed (and prevent the user to press 100 keys because he doesn't see a reaction) and a wait... message.

At the moment I can select which machine I want fine. But as I press Select the concentration and machine run over each other. I want to be able to finish with the machine selection and focus on concentration.
How do I ensure the Machine part of the program stops whilst I then go on to the concentration section?

Hi.
Are you still with the code you posted in your 1st. post ?
If not, post your most recent code, and do so in [code] [/code] tags, as pointed out in these guidelines (click !).

The problem in that code, is that you have put almost everything in void(loop).
That means that once the sketch is done with that part, it will forget all variables created in that part, and start that part over again.
So you will be welcomed again, will be waiting (read wasting valuable time) a lot and do nothing next to that.
You are planning to leave void(loop) and read a key, but you aren't doing that the way it is supposed to be done.
This might be due to some incorrect copy/paste action, because some stuff is missing in that code you posted.

So lots of stuff to be addressed.
Let's first get rid of the reported compile errors of your first code.
Or post your present code to work on where you are now.
And let's take step by step after that, there's lots of steps to be taken yet.

By the way, why couldn't you download my sketch ?
There have been 6 attempts or downloads, and i tested the download just now and it works.
I'm on another pc and another location at the moment so i'm sure it's OK.

Hi MAS3 I did download your file in the end, thanks for that. Yes I have a lot of questions and as you said they all run inside void() loop. This is my most recent code

#include <EEPROM.h>


//Using LiquidCrystal library
#include <LiquidCrystal.h>


/*******************************************************

  This program will add a value and display it.  To increment
  use the UP and DOWN button.

*******************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
int Machine = 0;
int CONC = 0;
int DESVOL = 0;
int CURVOL = 0;



#define btnUP     1
#define btnDOWN   2
#define btnSELECT 3


// declair funtion to read the buttons

int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor

  // For V1.1 us this threshold
  /*
    if (adc_key_in < 250)  return btnUP;
    if (adc_key_in < 450)  return btnDOWN;
  */

  // For V1.0 comment the other threshold and use the one below:

  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 790)  return btnSELECT;
}

void setup()
{
  lcd.begin(16, 2); // start the library
  Serial.begin(9600);
  
  lcd.clear();
 
  lcd.setCursor(0,0);
  lcd.print("Welcome to FSG");
  delay(3000);
  lcd.setCursor(0,3);
  lcd.print("Tool & Die LTD");
  delay(2000);
  
 
  lcd.clear();
  
}

void loop()
{

  
  (lcd_key = read_LCD_buttons());
  static int value1 = 1;
  if (lcd_key == 1)
  {
    value1 = constrain(value1 + 1, 1, 7); // I only wanted it to count from (-30)-(+30) and back
  }
  else if (lcd_key == 2)
  {
    value1 = constrain(value1 - 1, 1, 7); // I only wanted it to count from (-30)-(+30_)and back
  }

  lcd.setCursor(0, 0);
  lcd.print("Select Machine: ");
  lcd.setCursor(0, 3);
  lcd.print(value1); // displays the count
  lcd.print(" ");  //This space is here to clear the double digit area.

  delay(150);

  if(lcd_key ==3){
    EEPROM.put(Machine,value1);
    delay(300);
    Serial.println(value1);

    lcd.print("Done");
    delay(2000);
    
   int lcd_key     = 0;
    int adc_key_in  = 0;
    (lcd_key = read_LCD_buttons());
    
    static int value2 = 1;
    if (lcd_key == 1)
    {
     value2 = constrain(value2 + 1, 1, 30); // I only wanted it to count from (-30)-(+30) and back
    }
    else if (lcd_key == 2)
    {
     value2 = constrain(value2 - 1, 1, 30); // I only wanted it to count from (-30)-(+30_)and back
    }
    else (0);{
    }
    
    lcd.setCursor(0, 0);
    delay(100);
    lcd.print("Oil Percentage:");
    lcd.setCursor(0, 3);
    lcd.print(value2); // displays the count
    lcd.print(" %");  //This space is here to clear the double digit area.

    delay(150);
  
    if(lcd_key ==3){
    EEPROM.put(CONC,value2);
    delay(300);
    Serial.println(value2);

    lcd.print("Done");
    delay(1000);
    }
  }
}