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