Huh?
pinMode(SDA, INPUT);
pinMode(SCL, INPUT);
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
You don't need to do that, Wire.begin() does it.
And this?
boolean SDA = 20; // I2C - serial data pin
boolean SCL = 21; // I2C - serial clock pin
Booleans are true or false, you don't stuff numbers into them. And in any case, each chip has dedicated I2C lines, you don't need to tell the Wire library which ones to use.
Your code doesn't look anything like the example on the page you linked to. I'll reproduce it here because it was pretty unreadable:
#include <Wire.h> // i2c
#include <byvackeypad.h> // library for byvac i2c keypad
// default is 0x62 which is 8 bits, 0x31 is 7 bit address
ByVacKeypad keypad = ByVacKeypad(0x31);
void setup() {
keypad.init(); // initialize the keypad. just clears buffer
Serial.begin(9600);
}
void loop(){
Serial.print("Keys in Buffer: ");
int numkeys;
Serial.print(numkeys);
delay(2000);
if(keypad.numkeys() < 0){
Serial.print("Key Pushed: ");
int getkey = keypad.getkey();
Serial.print(getkey);
}
delay(2000);
if(keypad.numkeys() < 0){
Serial.print("Key Down: ");
int keydown = keypad.keydown();
Serial.print(keydown);
// will return 1 if key is down and 0 otherwise
}
}
Even if you don't use his library, you don't check the SCL line to see if the "device is busy".
Instead of this:
void Getkey()
{
if (digitalRead(SCL) == HIGH){ //Check if keypad is busy, LOW = busy, HIGH = good to go
key = 255; //Reset value "key"
Wire.beginTransmission(0x31); //Keypad Adress
Wire.send(4); //Getkey-function in keypad
Wire.endTransmission();
delayMicroseconds(45); //Let keypad think for 45us
Wire.beginTransmission(0x31); //Reastablish comunication
Wire.requestFrom(int(0x31), 1); //Request one bit at keypad adress
if (Wire.available()) { //Value avalible?
key = Wire.receive(); //Get value into "key"
}
Wire.endTransmission(); //End transmission
}
}
It would be much more like this (untested):
//ByVac Keypad
#include <Wire.h> // I2C
#define DEVICE_ADDRESS 0x31
void setup() {
Wire.begin(); // join I2C bus as master
// initialize the serial communication:
Serial.begin(9600);
Serial.println("initialization done");
}
byte Getkey()
{
Wire.beginTransmission(DEVICE_ADDRESS); //Keypad Adress
Wire.send(4); //Getkey-function in keypad
Wire.endTransmission();
Wire.requestFrom(DEVICE_ADDRESS, 1); //Request one byte from keypad
if (Wire.available()) //Value avalible?
return Wire.receive(); //Get value into "key"
return 0xFF; // no response
}
void loop() { //main loop
byte key = Getkey(); //run function "Getkey"
if(key != 0xFF) //If value has changed (key pressed and stored) serialprint it
{
digitalWrite(13, HIGH);
Serial.print("key ");
Serial.println(key, DEC);
digitalWrite(13, LOW);
}
}