Go Down

Topic: Help combining lcd and keypad scripts (Read 620 times) previous topic - next topic

gazz

I'm using an arduino uno as the heart of a replica of a bus ticket machine, basically a 32 buton keypad and an lcd,

I have the lcd part working fine, however i am just using standard button inputs to trigger each message displayed on the lcd (i.e. a pin to ground via a switch)

I need to use the keypad matrix sketch for my 32 buttons, i have that sketch working also, but sending the keypad assignments to the serial monitor,

Can someone advise me what i need to change to merge the 2 scripts, so that it reads the keypad on a 9x4 matrix, and certian keys are assigned to display certain messages on the lcd when pressed.

My lcd sketch :
Code: [Select]
#include <LiquidCrystal.h>

int buttonPin0= 0;    //sets pins buttons are connected to
int buttonPin1= 1;
int buttonPin2= 2;
int buttonPin3= 3;
int buttonPin4= 4;
int buttonPin5= 5;
int buttonPin6= 6;
int buttonPin7= 7;

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); //sets pins lcd is connected to, running in 4 bit mode, RW pin tied to gnd, and i'm using the analog pins as i need the digital pins for the buttons
byte selectionMade = false; //code to only allow print button to work after certain other buttons have been pressed
void setup()

{
  byte selectionMade = false; //sets to no selection made yet, so print button will not respond

  pinMode(buttonPin0, INPUT);
  pinMode(buttonPin1, INPUT);  //assigns button pins as inputs
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);

  digitalWrite(buttonPin0, HIGH);
  digitalWrite(buttonPin1, HIGH);  //sets internal pulldown resistors
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
  digitalWrite(buttonPin4, HIGH);
  digitalWrite(buttonPin5, HIGH);
  digitalWrite(buttonPin6, HIGH);
  digitalWrite(buttonPin7, HIGH);

  lcd.begin(20, 1);  //set up lcd
  lcd.setCursor(20,0);  //put cursor to end of lcd
  lcd.print("Gazz's D92 dashboard for Omsi, Der Omnibussimulator");  //prints text to lcd
  for (int positionCounter = 0; positionCounter < 51; positionCounter++)  { //maths bit for scrolling
    lcd.scrollDisplayLeft();   //scrolls text
    delay(250);
  } //sets speed of scrolling
  lcd.clear(); //clears lcd ready for main use
}

void loop(){
  int buttonState0 = digitalRead(buttonPin0);
  int buttonState1 = digitalRead(buttonPin1); 
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);
  int buttonState5 = digitalRead(buttonPin5);
  int buttonState6 = digitalRead(buttonPin6);
  int buttonState7 = digitalRead(buttonPin7);

  if (buttonState0 == LOW) {
    selectionMade = true;     
    lcd.setCursor(0,0);
    lcd.print("Fahrschein Nor  2.70");  //German for single ticket, stays on the lcd until another buttons is pressed
  }

  else if(buttonState1 == LOW){
    selectionMade = true;
    lcd.setCursor(0,0);
    lcd.print("Kurzstr Norm    1.70");  //German for short trip ticket, stays on lcd untill another button is presed
  } 

  else if (buttonState2 == LOW){
    selectionMade = true;
    lcd.setCursor(0,0);
    lcd.print("Tageskarte Nor  9.00");  //German for day ticket, stays on display untill another button is pressed
  }   

  else if (buttonState3 == LOW){
    selectionMade = true;
    lcd.setCursor(0,0);
    lcd.print("Fahrschein Erm  1.70");  //German for kids ticket

  } 

  else if (buttonState4 == LOW){
    selectionMade = true;
    lcd.setCursor(0,0);
    lcd.print("Kurzstr Erm     1.20");  //German for kids short trip ticket
  }
  else if (buttonState5 == LOW){
    selectionMade = true;
    lcd.setCursor(0,0);
    ;
   lcd.print("Kurf");
   lcd.print(char(245));
   lcd.print("rstendamm  1.00");  //Ku'damm ticket, Berlin shopping street only
  }
  else if(buttonState6 == LOW){
    selectionMade = false;
    lcd.clear();  //cancel button, clears display

  }
  else if   
    (buttonState7 == LOW && selectionMade == true){
    lcd.setCursor(0,0);  //clears display
    lcd.print("   >>Belegdruck<<   ");  //German for 'printing ticket' only displayed after ticket selection made
    delay(350);
    lcd.noDisplay();
    delay(250);
    lcd.display();
    delay(350);
    lcd.noDisplay();
    delay(250);
    lcd.display();
    delay(550);
    lcd.clear();
    selectionMade = false; //resets the selection state to needing ticket preselection again
  }


}


And the keypad sketch:

Code: [Select]
#include <Keypad.h>

const byte ROWS = 4; //4 rows
const byte COLS = 9; //9 columns

char hexaKeys[ROWS][COLS] = {
  {'0','1','2','3','4','5','6','7','8'},
  {'9','A','B','C','D','E','F','G','H'},
  {'I','J','K','L','M','N','O','P','Q'},
  {'R','S','T','U','V','W','X','Y','Z'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10, 9, 8, 7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char customKey = cusomKeypad.getKey();
 
  if (customKey != NO_KEY){
    Serial.println(customKey);
  }
}


I think i will need to keep the button state part of the script, but change it to point to the keypad assignments for the buttons??

Go Up