I have two UNOS and I have wired them correctly for I2C ,the "Hello World" program works fine .
I have a keypad attached to one arduino(slave arduino ) . I get three integers {a,b,c} from the keypad and apply formula
z=(100a)+(10b)+c
I want to send integr z to the master arduino when it requests.
what should be my lines so that as I press keys on keypad on slave, integr z gets to the other Arduino and gets printed in the serial monitor. I am confused in the way the master arduino reads data in bits.
Here are my codes ,
**SLAVE CODE **
#include <Wire.h>
#include<Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
int a = customKeypad.waitForKey()-48 ; // -48 for character form to intger conversion .
int b= customKeypad.waitForKey()-48;
int c= customKeypad.waitForKey()-48;
int z= (100a)+(10b)+c;
Wire.write(z);
Master Code
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
int z = Wire.read(); // receive a byte as character
Serial.print(z); // print the character
}
delay(500);
}