This is the setup I have, and it works in Tinkercad but when I build it the LCD does not do anything. If the slave uno is not connected, the LCD will function but as soon as I connect SLC and SDA to A4 and A5 it stops, even without powering the other slave UNO.
MASTER CODE:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
String room = "";
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
lcd.backlight();
lcd.init();
lcd.print("Enter Room");
Wire.begin();
pinMode(LED_BUILTIN, OUTPUT);
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
char pressedKey = myKeypad.getKey();
switch (pressedKey) {
case '#':
lcd.clear();
Serial.println("Enter : ");
Serial.println(room);
lcd.print("Enter: " + room);
transmit();
room = "";
break;
case '*':
Serial.println("Reset");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reset");
delay(1000);
lcd.clear();
room = "";
break;
default:
if (pressedKey == NO_KEY) break;
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
if (room.length() >= 4) break;
if (room.length() == 0 && !(pressedKey == ('A') || pressedKey == ('B') || pressedKey == ('C') || pressedKey == ('D') )) break;
if (room.length() >= 1 && (pressedKey == ('A') || pressedKey == ('B') || pressedKey == ('C') || pressedKey == ('D') )) break;
room += (pressedKey);
Serial.println(room);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(room);
break;
}
}
void transmit(){
int x = room.length();
for(int y = 0; y<x; y++){
Wire.beginTransmission(9); // transmit to device #9
Wire.write(room[y]);
Wire.endTransmission();
}
}
SLAVE CODE:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
char x;
String room = "";
void setup() {
// Define the LED pin as Output
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int bytes) {
x = Wire.read();
Serial.println("Recieved");
room += x;
Serial.println(room);
// read one character from the I2C
}
void loop() {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
//If value received is 0 blink LED for 200 ms
if(room == "A123"){
resetRoom();
}
if(room == "A321"){
resetRoom();
digitalWrite(12, HIGH);
delay(200);
digitalWrite(12, LOW);
}
}
void resetRoom(){
room = "";
}
Any recommendations?
