ciao a tutti
sto facendo una prova e vorrei che un arduino master manda un comando allo slave e lo slave risponda al master. il problema è che se ci provo al master ritorna dei caratteri illegibili. Per prova sto usando un arduino yun e un uno.
codice master
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(4);
Wire.println("effetto ?");
Wire.endTransmission();
delay(10);
Wire.requestFrom(4, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(1000);
}
codice slave
#include <Wire.h>
int effetto = 0;
void setup() {
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
effetto++;
delay(500);
}
void receiveEvent(int howMany){
String app = "";
while(Wire.available()) // loop through all but the last
{
app += (char)Wire.read(); // receive byte as a character
}
app.trim();
Serial.println(app);
if(app == "effetto ?"){
Wire.write("effet",6);
}
}
dov'è che sbaglio?