Go Down

Topic: LCD 1602 Keypad Shield - How to input single characters using up-down buttons (Read 1 time) previous topic - next topic

beugelaar

Hello All,

Is there anybody who has a sample sketch how I can let an user select a character using the up-down buttons like an up-down scrollbar of available characters. By pressing the select button the desired character is displayed and the cursor is moving to the next position on the display to repeat the same procedure?

E.g. Input a mobile number via the up-down buttons.

Thanks in advance,
Erik

HazardsMind

Are you only looking to use 0-9, or 0-9 and a-z / A-Z? They are all ascii characters so, you will use either HEX or decimal to cycle throught the characters.

0-9 => 48-57 DEC, 30-39 HEX
A-Z => 65-90 DEC, 41-5A HEX
a-z => 97-122 DEC, 61-7A HEX

Ascii table

Here is a sample of how to use two buttons to cycle through numbers. This code uses an I2C lcd, some tweaking may be required for your lcd.
Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);

const byte buttonPin1 = 2; // Up button
const byte buttonPin2 = 3; // Down Button
//const int ledPin =  11;

int buttonState1 = 0;
int lastReading1 = 0;
int buttonState2 = 0;
int lastReading2 = 0;
int lastcount =0;
long onTime1=0;
long onTime2=0;
int count = 0;

void setup() {
  //pinMode(ledPin, OUTPUT);     
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

  lcd.init();                      // initialize the lcd
  lcd.backlight();
  Serial.begin(9600); 
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState1 == HIGH && lastReading1 == LOW) {
    onTime1 = millis();
    count++;
  }
  if (buttonState2 == HIGH && lastReading2 == LOW) {
    onTime2 = millis();
    count--;
  }

  //held
  if (buttonState1 == HIGH && lastReading1 == HIGH) {
    if ((millis() - onTime1) > 500 ) {
      delay(200);
      count++;
      lastReading1 = LOW;
    }
  }
  if (buttonState2 == HIGH && lastReading2 == HIGH) {
    if ((millis() - onTime2) > 500 ) {
      delay(200);
      count--;
      lastReading2 = LOW;
    }
  }
  Serial.println(count);
  lcd.setCursor(0,0);
  //lcd.clear();
  if(lastcount < 0 && count >= 0) lcd.clear();
  if(lastcount >=10 && count < 10) lcd.clear();
  if(lastcount >=100 && count < 100) lcd.clear();
  if(lastcount >=1000 && count < 1000) lcd.clear();
  lcd.print(count);
  //lcd.clear();
  lastcount = count;
  lastReading1 = buttonState1;
  lastReading2 = buttonState2;
}


My GitHub:
https://github.com/AndrewMascolo?tab=repositories

beugelaar

Thank you very much for your reply.

I definitely can do something with your sketch!

Cheers,
Erik

Go Up