I2c Keypad input

Hi all,
I’m trying to data from an I2C key and store it into an array but I can’t seem to get it to work, How I want it to work is

If I press the # key say 1234 then I want to be able to display what was pressed, Once I have this working I then can add/sort out the other bit for setting the output of a Dac.

This is the example that I’ve got working on my home made I2c keypad following the example is working

 * // I2C Keypad for Arduino
// Venkateswara Rao.E 
// 19-oct-2015
// Credits to  @author Alexander Brevig

#include <Wire.h>
#include <Keypad_I2C.h>
#include <Keypad.h>
#define I2CADDR 0x38

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
const byte keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

// Digitran keypad, bit numbers of PCF8574 i/o port
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6}; //connect to the column pinouts of the keypad

Keypad_I2C kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574 );

void setup(){
    Wire.begin( );
    kpd.begin( makeKeymap(keys) );
    Serial.begin(9600);
    Serial.println( "start" );
}

void loop(){
 byte key = kpd.getKey();
   //  Serial.println(key);
    if (key){
    Serial.println(key);
    }
else{ 
key = 255;
    }
      

}*/

This is the code that I’m trying to get to work, I’ve tried to search but only can see password for normal keypad and not I2c. It’s a mess at the moment as I’ve tried lots to sort out, but now struggling

// I2C Keypad for Arduino
// Venkateswara Rao.E
// 19-oct-2015
// Credits to  @author Alexander Brevig

#include <Wire.h>
#include <Keypad_I2C.h>
#include <Keypad.h>
#define I2CADDR 0x38
int KeypadData;
int KeyOutputControlCh1 = 0;
int KeyOutputControlCh2 = 0;
float KeyVoltageCh1 = 0;
float KeyVoltageCh2 = 0;
int KeyChannelSelect = 1;
int KeypadStatus = 0;
int KeyAccepted = 0;
int KeyCount = 0;
int KeyDataSingle = 0;
int Keyying = 0;
int Booting = 1;
int Tracking = 0;
int KeyDataArray[4] = { 0, 0, 0, 0 };
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
byte key;
const byte keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// Digitran keypad, bit numbers of PCF8574 i/o port
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6}; //connect to the column pinouts of the keypad

Keypad_I2C kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574 );

void setup() {
  Wire.begin( );
  kpd.begin( makeKeymap(keys) );
  Serial.begin(9600);
  Serial.println( "start" );
}

void loop() {
  key = kpd.getKey();

  //   KeypadData = key;
  if (key) {
    Serial.println(key);
    KeypadData = key;
    Keypad1();
    //  Serial.println(key);
  }
  //else{
  //key = 0;
  //  }
  if (KeypadData = 35) {
    Keypad1();
  }
}
void Keypad1() {

  if (KeypadData == 0) { // abort - stops auto-repeat on key, sometimes 255 is returned so capturing duff data here
    KeypadStatus = 0;
  }

  if (KeypadData == 32 && KeyCount == 0 && KeyChannelSelect == 0) { // * key pressed (42)

    Tracking = !Tracking;
    KeyOutputControlCh1 = 0;
    KeyOutputControlCh2 = 0;
    Serial.println(" set to 1 volt");
  }

  if (KeyChannelSelect == 1 && KeypadData == 35) { // Read keypad for setting of Ch.1 output voltage
    Keyying = 1;
    // KeyCount++;

    for (KeyCount = 0; KeyCount <= 3; KeyCount ++) {
      key = kpd.getKey();

      KeyDataArray[KeyCount] = key;
    }
    if (KeyCount == 4) {
      KeyVoltageCh1 = ((KeyDataArray[0] ) + (KeyDataArray[1] ) + (KeyDataArray[2]) + ( KeyDataArray[3]));
      KeyChannelSelect = 0;
      KeyOutputControlCh1 = 1;
      Keyying = 0;
      KeyCount = 0;
      // KeyCount = 0;
      Serial.println(  KeyVoltageCh1);
      //  KeypadData =255;
    }
  }

So I I press the * (which gives 42)key I want it to display Set to 1 volt on the serial monitor, Then Press #(which gives 35) and then I need to be able to enter 1234 then dislay tihs on the Serial monitor once the four digits have been entered, I have lots more to carry out but thought I’d try and get the basics working first.

Any tips ?

tahnks

Steve

why u did not try analogue keypad library instead ,it is more simple

mestek86:
why u did not try analogue keypad library instead ,it is more simple

Because I want to use a ready built keypad/custom made, This is part of a bigger project but getting the basic's done first, I don't like using analogue to read key's.

I’ve managed it to get to display Set 1 Volt,
Here is may latest code.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad_I2C.h>
#include <Keypad.h>
#define I2CADDR 0x38
int KeypadStatus = 0;
int KeyAccepted = 0;
char KeypadData;
int KeyCount = 0;
int KeyDataSingle = 0;
int KeyDataArray[4] = { 0, 0, 0, 0 };
int KeyOutputControlCh1 = 0;
int KeyOutputControlCh2 = 0;
float KeyVoltageCh1 = 1;
float KeyVoltageCh2 = 0;
int KeyChannelSelect = 0;
int Keyying = 0;
int Booting = 1;
int Tracking = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// Digitran keypad, bit numbers of PCF8574 i/o port
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6}; //connect to the column pinouts of the keypad

Keypad_I2C kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574 );

void setup() {
  Wire.begin( );
  lcd.begin(16, 2);                             // Initialize the LCD.
  lcd.clear();
  kpd.begin( makeKeymap(keys) );
  Serial.begin(9600);
  Serial.println( "start" );
}

void loop() {
  KeyPad();
  lcd.setCursor(0, 1);
  lcd.print( KeyVoltageCh1);
}

void KeyPad() {
  KeypadData = kpd.getKey();

  if (KeypadData == 0 && KeyCount == 0 && KeyChannelSelect == 0) { // Tracking
    Tracking = !Tracking;
    KeyOutputControlCh1 = 0;
    KeyOutputControlCh2 = 0;
  }

  if (KeypadData == '1' && KeyCount == 0 && KeyChannelSelect == 0) { // Set 1.00vdc to both outputs for testing
    KeyVoltageCh1 = 250;
    KeyVoltageCh2 = 100;
    KeyOutputControlCh1 = 1;
    KeyOutputControlCh2 = 1;
    Tracking = 0; lcd.setCursor(0, 0);
    lcd.print( "SET 1 VOLT");
  }
  // if (KeypadData) {
  // Serial.println(KeypadData);

  if (KeyChannelSelect == 0 && KeypadData == '#') { // Read keypad for setting of Ch.1 output voltageKeypadData = kpd.getKey();
    Keyying = 1;
    //KeyCount++;
    lcd.setCursor(0, 0);
    lcd.print( "SET  VOLT");
    for (KeyCount = 0; KeyCount < 3; KeyCount++) {
      KeypadData = kpd.getKey();
      KeyDataArray[KeyCount] = KeypadData;
      lcd.setCursor(5, 1);
      lcd.print( KeypadData);
    }
    if (KeyCount == 3) {
      KeyVoltageCh1 = (KeyDataArray[0] * 1000) + (KeyDataArray[1] * 100) + (KeyDataArray[2] * 10) + KeyDataArray[3];
      KeyCount = 0;
      KeyChannelSelect = 0;
      KeyOutputControlCh1 = 1;
      Keyying = 0;


    }

  }
  //  }
}

But I still can’t get it to capture the data array, Using a debugger It seems to be not accepting new key press, Key count increases, The code below does not ever seem to put keycount back to zero.

if (KeyChannelSelect == 0 && KeypadData == '#') { // Read keypad for setting of Ch.1 output voltageKeypadData = kpd.getKey();
    Keyying = 1;
    //KeyCount++;
    lcd.setCursor(0, 0);
    lcd.print( "SET  VOLT");
    for (KeyCount = 0; KeyCount < 3; KeyCount++) {
      KeypadData = kpd.getKey();
      KeyDataArray[KeyCount] = KeypadData;
     // KeyVoltageCh1 = (KeyDataArray[KeyCount] * 1000) + (KeyDataArray[KeyCount] * 100) + (KeyDataArray[KeyCount] * 10) + KeyDataArray[KeyCount];
      lcd.setCursor(5, 1);
      lcd.print( KeypadData);
    }
    if (KeyCount == 3) {
      KeyVoltageCh1 = (KeyDataArray[0] * 1000) + (KeyDataArray[1] * 100) + (KeyDataArray[2] * 10) + KeyDataArray[3];
      KeyCount = 0;
      KeyChannelSelect = 0;
      KeyOutputControlCh1 = 1;
      Keyying = 0;


    }

  }
  //  }

Once I press the ‘#’ key it displays set volt then if I press one it seems to jump to Set 1 volt screen, So I tried to press 2345 the keycounts increase but same again it does not rest back to zero,

Not sure where I’m going wrong ?

    for (KeyCount = 0; KeyCount < 3; KeyCount++) {

Keycount will ALWAYS be three when this statement ends.

    if (KeyCount == 3) {
      KeyVoltageCh1 = (KeyDataArray[0] * 1000) + (KeyDataArray[1] * 100) + (KeyDataArray[2] * 10) + KeyDataArray[3];
      KeyCount = 0;

It’s pointless to test the value in a variable when you KNOW the value in the variable.

It’s pointless to debug by guesswork. A few well places Serial.print() statements will tell you where your expectations regarding what the code is doing are wrong.