Ho fatto qualche prova di comunicazione tra due arduino nano , uso la libreria wire.h.
Volevo riuscire a passare più variabili tra un arduino e l'altro, per provare ho messo un potenziometro collegato al master, che doveva scrivere le letture in una variabile per poi inviarle allo slave.
Il problema è che il master scrive un dato per poi bloccarsi.
/*
* Master send
*/
#include <Wire.h>
void setup() {
Wire.begin();
}
byte x =0;
void loop() {
int y = analogRead(A0);
x = map(y,0,1023,0,255);
buswrite(x,11,12,13);
}
void buswrite (unsigned char int0, unsigned char int1, unsigned char int2, unsigned char int3){
Wire.beginTransmission(1);
Wire.write(int0);
Wire.write(int1);
Wire.write(int2);
Wire.write(int3);
Wire.endTransmission();
}
/*
* Slave recive
*/
#include <Wire.h>
int int0 = 0;
int int1 = 0;
int int2 = 0;
int int3 = 0;
void setup() {
Serial.begin(9600);
Wire.begin(1);
Wire.onReceive(receiveTemp);
}
void loop() {
delay (100);
Serial.print(int0);
Serial.print(" , ");
Serial.print(int1);
Serial.print(" , ");
Serial.print(int2);
Serial.print(" , ");
Serial.println(int3);
}
int receiveTemp(){
while(Wire.available()){
int0 = Wire.read();
int1 = Wire.read();
int2 = Wire.read();
int3 = Wire.read();
}
return int0;
return int1;
return int2;
return int3;
}