Lcd keypad

Hi. i wonder if someone can help me with some programming code?.

i want to control a Led strip with an lcd keypad shield and Arduino Uno R3, if thats possible?

many thanks / E :)

Eriix:
i want to control a Led strip with an lcd keypad shield and Arduino Uno R3, if thats possible?

Depends on the actual type of LED strip.
In many cases: Yes
In some cases: No

It depends on the type of LED strip and how you want to control it.

It is neopixel led.

Like turn it on and off and set brithness?

Yes; with neopixel, you send a datastream of 3 bytes, the LED controller in the strip uses that to control its PWM outputs to its LEDs.

Okey does anyone have an example code? :slight_smile:
Im really a beginner :slight_smile:

Eriix: It is neopixel led.

Like turn it on and off and set brithness?

Neopixels can be controlled using the Adafruit_NeoPixel library.

Do you have installed the Adafruit_NeoPixel library? Did you successfully test one of the example sketches?

Or do you prefer another library that can control WS2812 LEDs?

yes i have the Adafruit library installed, the other library i don’t have.

this is an example code that i have.

// 87876 LCD Shield för Arduino
// 16x2 tecken och knappar
// Kjell & Company

// include the library code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // initialize the library with the numbers of the interface pins

void setup() {
lcd.begin(16, 2); // set up the LCD’s number of columns and rows
lcd.clear(); // Clear LCD
lcd.setCursor(0,0); // Set cursor to x=0 and y=0
lcd.print(“LCD Key Shield”); // Print text on LCD
lcd.setCursor(0,1);
lcd.print(“Kjell & Company”);
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Press any Key:”);
}

void loop() {
int x;
x = analogRead (0); // Read the analog value for buttons
if (x < 100) { // Right button is pressed
lcd.setCursor(0,1);
lcd.print ("Right ");
}
else if (x < 200) { // Up button is pressed
lcd.setCursor(0,1);
lcd.print ("Up ");
}
else if (x < 400){ // Down button is pressed
lcd.setCursor(0,1);
lcd.print ("Down ");
}
else if (x < 600){ // Left button is pressed
lcd.setCursor(0,1);
lcd.print ("Left ");
}
else if (x < 800){ // Select button is pressed
lcd.setCursor(0,1);
lcd.print (“Select”); }
}

and for an example, i want to let the “select” button to turn on the neopixel strip to 100% brihtness.
“Left” for 75% brithness
“down” 50%
“up” 25%
“right” turn off Led.

can anyone help me with this ? many thanks :slight_smile:

Eriix: this is an example code that i have.

Thats the example code for the LCD keypad shield. So I assume, you have tested the keypad shield with this code and it works?

The keypad reading code just reads the key 'state', but you will need 'state change' detecting code. That will need some code changes.

What about the Neopixel library? Have you also installed and tested the Neopixel library? You will need this library installed and working, too!

Eriix: can anyone help me with this ? many thanks :)

I can try and work something out as an example, if I find a little time. Stay tuned.

Here is some code for LCD keypad shield and WS2812/Neopixel LED strip:

#include <Adafruit_NeoPixel.h>
#define LEDPIN A5  // Arduino data pin, connect with R=330 Ohm series resistor to LED strip data pin
#define NUMLEDS 50 // Number of LEDs in strip

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMLEDS, LEDPIN, NEO_GRB + NEO_KHZ800);


#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // LCD keypad shield
// in addition, analogue pin A0 will be used to read the keys

enum {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP};


int readButtonInput() // read the button state change 'pressed' from analog interface
{
  static int lastValidKey=btnNONE;
  static boolean waitForNONE;
  int currentKey;
  int adcValue[3];
  for (int i=0;i<3;i++) // read pin three times
  {
    adcValue[i] = analogRead(A0);   // read the ADC value from A0 
  }
  if (abs(adcValue[1]-adcValue[0])>1 || abs(adcValue[2]-adcValue[0])>1) 
  {
    return btnNONE; // return because invalid input (reading is too much different)
  }
  if (adcValue[0] > 1000) currentKey= btnNONE;
  else if (adcValue[0] < 70)   currentKey= btnRIGHT; 
  else if (adcValue[0] < 235)  currentKey= btnUP;
  else if (adcValue[0] < 410)  currentKey= btnDOWN;
  else if (adcValue[0] < 620)  currentKey= btnLEFT;
  else if (adcValue[0] < 880)  currentKey= btnSELECT;  
  else return btnNONE;  // when all others fail, return because invalid input
  if (currentKey==btnNONE) 
  {
    waitForNONE=false;
    lastValidKey=btnNONE;
    return btnNONE;
  }
  else if (waitForNONE==false)
  {
    waitForNONE=true;
    lastValidKey=currentKey;
    return currentKey;
  }
  return btnNONE;
}

void lcdOutput(char* str1, char* str2)
{ // prints two linex of text to a 16x2 LCD display
  // actual printing is only done, if at least 
  // one character in the display has changed since last function call
  static char lastOutput[2][17];
  static int lastCursorPos;
  strcpy(lastOutput[0],str1);
  strcpy(lastOutput[1],str2);
  lcd.noCursor();
  lcd.setCursor(0,0);
  lcd.print(str1);
  for (int i=0; i<16-strlen(str1);i++) lcd.print(' ');
  lcd.setCursor(0,1);
  lcd.print(str2);
  for (int i=0; i<16-strlen(str2);i++) lcd.print(' ');
}

const byte fadeValues[8]= { 0, 4, 8, 16, 32, 64, 128, 255};
byte fadeValue=0;
boolean needUpdate=true;

unsigned long nowMillis;
int buttonPressed;

void input()
{
  nowMillis=millis();
  buttonPressed=readButtonInput();
}


void processing()
{
  switch (buttonPressed)
  {
    case btnUP:
      if (fadeValue<sizeof(fadeValues)-1) fadeValue++;
      break;
    case btnDOWN:  
      if (fadeValue>0) fadeValue--;
      break;
    case btnSELECT:  
      if (fadeValue==0) fadeValue=5;
      else fadeValue=0;
      break;
  }
  if (buttonPressed==btnNONE) needUpdate=false;
  else needUpdate=true;
} // end of processing() function


void output()
{
  if (needUpdate)
  {
    if (fadeValue==0) lcdOutput("","Hit SELECT key");
    else
    {
      char str[17];
      snprintf(str,sizeof(str),"Brightness %d%%",fadeValue*100/7);
      lcdOutput(str,"Effect: None");
    }
    for(int i=0; i<strip.numPixels(); i++)
      strip.setPixelColor(i, fadeValues[fadeValue],fadeValues[fadeValue],fadeValues[fadeValue]);
    strip.show();  
  }
}


void setup()
{
  Serial.begin(9600);   // starts serial interface with 9600 baud
  strip.begin();        // starts led strip
  lcd.begin(16, 2);     // starts the LCD display (2 lines by 16 chars)
  output();
}

void loop()
{
  input();
  processing();
  output();
}

The code provides a total of 8 brightness steps (including OFF and 100% white) and the key logic is like:

“Select” when off ==> switch on with brightness step 5
“Select” when on ==> switch off
“Up” ==> brightness up one step until max. brightness
“Down” ==> brightness down one step until off

Only “white” in different brightness supported.
No color control.
No special effects control.

Many thanks :D I Will try it out as soon Im home Again. Im really greatfull for all your support :)

Eriix:
Many thanks :smiley: I Will try it out as soon Im home Again. Im really greatfull for all your support :slight_smile:

Just a quick and dirty hack to bring together LCD, analog LCD keypad buttons and Neopixel WS2812 LEDs.

The readButtonInput() function was newly developed yesterday, so you have ‘state change’ detection instead of reading just the ‘state’ (as the LCD keypad shield demo provides), which offers much more possibilities in creating a suitable programming logic when buttons are pressed.

I used the IPO programming model with “Input-Processing-Output” functions to get most flexibility in extending the example. The btnLEFT and btnRIGHT buttons are not yet used. Perhaps next step could be to extend the programming logic to something like that:

btnSELECT ==> Select On / Off / different modes of LED effects
btnUP/btnDOWN ==> brightness control
btnLEFT/btnRIGHT ==> color control in ‘color mode’ or speed control in ‘LED effects mode’

Everything is prepared and ready to create more features from that code base.

Please let me know if you have any trouble or need further assistance.

okey thanks for all that support :) i have tried it and it works :) the only thing is that when I will turn the brightness up the brightness in the lcd display will fade down . and when you turn the brightness down on the led the brightness on the lcd display fades up :P

but many thanks for all your help :)

Eriix: the only thing is that when I will turn the brightness up the brightness in the lcd display will fade down . and when you turn the brightness down on the led the brightness on the lcd display fades up :P

I think that should be easy to debug and fix, whether the program reads the buttons interchanged or whether the buttons are differently connected on your shield.

But that's all how you want it? No colors? No fancy "automatic light show" program using some animated LED color changing?

Until now, only 3 out of 5 available buttons are used at all.

okey , i will look that up :)

if you have time to help me with it you can do whatever cool stuff you want for the other buttons :) suprise me :)

Thanks / E

Eriix:
if you have time to help me with it you can do whatever cool stuff you want for the other buttons :slight_smile:
suprise me :slight_smile:

Here is some advanced code that uses all five buttons and shows some colours, too:

#include <Adafruit_NeoPixel.h>
#define LEDPIN A5  // Arduino data pin, connect with R=330 Ohm series resistor to LED strip data pin
#define NUMLEDS 50 // Number of LEDs in strip

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMLEDS, LEDPIN, NEO_GRB + NEO_KHZ800);

void showRainbowCycle()
{
  static int inc,color;
  inc++;
  if (inc>=10)
  {
    inc=0;
    long pixelColor=Wheel(color);
    for (int i=0;i<strip.numPixels();i++) strip.setPixelColor(i, pixelColor);
    color=(color+1)%256;
    strip.show();
  }
  delay(10);
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

void showColors(byte r, byte g, byte b)
{
  for(int i=0; i<strip.numPixels(); i++)
  {
    strip.setPixelColor(i, r, g, b);
  }
  strip.show();  
}


#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // LCD keypad shield
// in addition, analogue pin A0 will be used to read the keys

enum {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP};


int readButtonInput() // read the button state change 'pressed' from analog interface
{
  static int lastValidKey=btnNONE;
  static boolean waitForNONE;
  int currentKey;
  int adcValue[3];
  for (int i=0;i<3;i++) // read pin three times
  {
    adcValue[i] = analogRead(A0);   // read the ADC value from A0 
  }
  if (abs(adcValue[1]-adcValue[0])>1 || abs(adcValue[2]-adcValue[0])>1) 
  {
    return btnNONE; // return because invalid input (reading is too much different)
  }
  if (adcValue[0] > 1000) currentKey= btnNONE;
  else if (adcValue[0] < 70)   currentKey= btnRIGHT; 
  else if (adcValue[0] < 235)  currentKey= btnUP;
  else if (adcValue[0] < 410)  currentKey= btnDOWN;
  else if (adcValue[0] < 620)  currentKey= btnLEFT;
  else if (adcValue[0] < 880)  currentKey= btnSELECT;  
  else return btnNONE;  // when all others fail, return because invalid input
  if (currentKey==btnNONE) 
  {
    waitForNONE=false;
    lastValidKey=btnNONE;
    return btnNONE;
  }
  else if (waitForNONE==false)
  {
    waitForNONE=true;
    lastValidKey=currentKey;
    return currentKey;
  }
  return btnNONE;
}

void lcdOutput(char* str1, char* str2)
{ // prints two linex of text to a 16x2 LCD display
  // actual printing is only done, if at least 
  // one character in the display has changed since last function call
  static char lastOutput[2][17];
  static int lastCursorPos;
  strcpy(lastOutput[0],str1);
  strcpy(lastOutput[1],str2);
  lcd.noCursor();
  lcd.setCursor(0,0);
  lcd.print(str1);
  for (int i=0; i<16-strlen(str1);i++) lcd.print(' ');
  lcd.setCursor(0,1);
  lcd.print(str2);
  for (int i=0; i<16-strlen(str2);i++) lcd.print(' ');
}

const byte brightSteps[8]= { 0, 4, 8, 16, 32, 64, 128, 255};
int brightStep=0;

const byte colors[7][3]={
  {255,255,255},  // white
  {255,0,0},  // red
  {255,255,0},
  {0,255,0}, // green
  {0,255,255}, 
  {0,0,255}, // blue
  {255,0,255},
};
#define NUMCOLORS sizeof(colors)/sizeof(colors[0])
int colorIndex=0;

enum {MODEOFF,MODECOLOR,MODERAINBOW,NUMMODES};
byte mode=MODEOFF;

boolean needUpdate=true;

unsigned long nowMillis;
int buttonPressed;

void input()
{
  nowMillis=millis();
  buttonPressed=readButtonInput();
}


void processing()
{
  switch (buttonPressed)
  {
    case btnUP:
      if (brightStep<sizeof(brightSteps)-1) brightStep++;
      break;
    case btnDOWN:  
      if (brightStep>0) brightStep--;
      break;
    case btnRIGHT:  
      if (colorIndex<NUMCOLORS-1) colorIndex++;
      break;
    case btnLEFT:
      if (colorIndex>0) colorIndex--;
      break;
    case btnSELECT:
      mode= (mode+1)%NUMMODES;
      if (mode==MODEOFF) brightStep=0;
      else if (mode==MODECOLOR) brightStep=5;
      break;
  }
  if (buttonPressed==btnNONE) needUpdate=false;
  else needUpdate=true;
} // end of processing() function


void output()
{
  byte r,g,b;
  if (needUpdate)
  {
    if (mode==MODEOFF)
    {
      lcdOutput("","Hit SELECT key");
      showColors(0,0,0);
    }
    else if (mode==MODECOLOR)
    {
      r=colors[colorIndex][0] & brightSteps[brightStep];
      g=colors[colorIndex][1] & brightSteps[brightStep];
      b=colors[colorIndex][2] & brightSteps[brightStep];
      char str1[17],str2[17];
      snprintf(str1,sizeof(str1),"Brightness %d%%",brightStep*100/7);
      snprintf(str2,sizeof(str2),"R%d G%d B%d", r, g, b);
      lcdOutput(str1,str2);
      showColors(r,g,b);
    }
    else if (mode==MODERAINBOW)
    {
      lcdOutput("Rainbow","");
    }
  }
  if (mode==MODERAINBOW) showRainbowCycle();

}


void setup()
{
  Serial.begin(9600);   // starts serial interface with 9600 baud
  strip.begin();        // starts led strip
  lcd.begin(16, 2);     // starts the LCD display (2 lines by 16 chars)
  output();
}

void loop()
{
  input();
  processing();
  output();
}

The SELECT button choses between OFF, COLORMODE and RAINBOW effect.

In COLORMODE you can switch colours including WHITE by using LEFT and RIGHT buttons, and you can switch brightness with UP and DOWN buttons. The R, G and B values are shown on the LCD.

If your LED strip shows different colors, try changing the initialisation to NEO_RGB instead NEO_GRB, which changes red and green with each other.

If your button connections are different to my LCD keypad shield, such as UP and DOWN mixed up, you’d have to change the sequence of buttons in the line:

enum {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP};

I think, the effect mode which currently only slowly cycles through the rainbow colors, could be extended to show more different ‘automatic’ lighting effects.

Thanks man :D it works perfect :) now i will fix the problem with the fade down/up brightness in the lcd :) . and again, thanks for all your help (Y) :)

/ E

Eriix:
Thanks man :smiley: it works perfect :slight_smile: now i will fix the problem with the fade down/up brightness in the lcd :slight_smile: .
and again, thanks for all your help (Y) :slight_smile:

I’ve just looked up the source and most likely I was wrong with switching the buttons in the enumeration.

I think this is where you would have to change the buttons:

  if (adcValue[0] > 1000) currentKey= btnNONE;
  else if (adcValue[0] < 70)   currentKey= btnRIGHT; 
  else if (adcValue[0] < 235)  currentKey= btnUP;
  else if (adcValue[0] < 410)  currentKey= btnDOWN;
  else if (adcValue[0] < 620)  currentKey= btnLEFT;
  else if (adcValue[0] < 880)  currentKey= btnSELECT;  
  else return btnNONE;  // when all others fail, return because invalid input

I.e. swap btnUP and btnDOWN to change the button readings.

oh i´m sorry, i think we din´t understand each other :P.

I meant when I press the button up for "brightness up" the led will brightness up but the lcd screen fades down. and when I press the button down for "brightness down" the led fades down but the lcd screen fades up.

Eriix: oh i´m sorry, i think we din´t understand each other :P.

I meant when I press the button up for "brightness up" the led will brightness up but the lcd screen fades down. and when I press the button down for "brightness down" the led fades down but the lcd screen fades up.

Oh, that's somewhat different from what I have understood so far.

If the backlight of the LCD fades up and down, something with the DC power must be wrong.

Do you connect the LED strip to the 5V of the Arduino board? For full brightness, this would only be allowed for up to 10 LEDs. Each colour of each LED pulls up to 20 mA, this is RGB for 1 LED = 3*20mA = 60 mA. 10 LEDs at full bright white is 600 mA, which is max. what you can get from the Arduino board 5V. 100 LEDs at full bright white need 5V / 6 A external power adapter.

Okey then i´m with you :). the leds are on the 5v on the arduino board. just for the moment I just drive the Arduino board with the usb from the computer.. and its 102 leds on the strip for the moment. so with a external power adapter, it should fix the problem?. because the rainbow effect stops working after a few colours, and when i set only white light on 71% brightness it stops working after 1-2min. but other colours works on 71%.

you have to excuse my english, i´m swedish.