Salve,
sto usando due Arduini per un progetto, nel quale comunicano tra loro tramite I2C.
Per fare delle prove sto utilizzando lo sketch di esempio nella pagina di arduino https://www.arduino.cc/en/Tutorial/MasterWriter e il tutto funziona, ma se invece di visualizzare il valore di x su monitor seriale lo voglio visualizzare su lcd questo non funziona, perchè?
Master:
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}
Slave:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,18,19);
void setup() {
lcd.init();
lcd.backlight();
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
lcd.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
lcd.print(x); // print the integer
}
Kraine.