I have tried to send and receive some data over I2C. It works perfectly, if I send data inside the loop and receive the data on the other side with Wire.onReceive() and send some new data inside the loop.
But now, I have tried to send the received data directly back to the master instead of write inside the loop a new sending command. Why this does not work? With the attached code it sends and receives nothing anymore!
Master sender:
#include <Wire.h>
const byte serialBuffer = 10;
char receivedData[serialBuffer];
char sentData[serialBuffer];
boolean newData = false;
byte DPangle = 13;
byte A = 0;
byte B = 0;
byte C = 0;
byte D = 0;
byte BUT = 0;
void setup() {
Wire.begin(10);
//Wire.setClock(300000);
Wire.onReceive(readCommand);
Serial.begin(38400);
}
void loop() {
//readCommand(Wire);
//command('G',122, 9);
//command('E',2, 9);
command('#',DPangle, 9);
//command('F',189, 9);
//command('H',51, 9);
}
void command(const char command, const byte value, const byte address){
sprintf(sentData, "\n%c%d\r", command,value); //generates a formatted string output with the format <character|decimal> from the char command and value with start- and end-marker and stores it to buffer
const size_t characters {strlen(sentData)};
//Serial.print("length: "); Serial.println(characters);
Wire.beginTransmission(address);
Wire.write(sentData, characters);
Wire.endTransmission();
delay(12);
}
void readCommand(int bytes){
readIncomingData();
storeReceivedData();
}
void readIncomingData(){
static boolean InProgress = false;
static byte i = 0;
char startMarker = '\n';
char endMarker = '\r';
char rc;
while (Wire.available() > 0 && newData == false){
rc = Wire.read();
if (InProgress == true){
if (rc != endMarker){
receivedData[i] = rc;
i++;
if (i >= serialBuffer){
i = serialBuffer - 1;
}
}
else {
receivedData[i] = '\0'; // terminate the string
InProgress = false;
i = 0;
newData = true;
}
}
else if (rc == startMarker){
InProgress = true;
}
}
}
void storeReceivedData(){
char var;
int y = 0;
if (newData == true){
var = receivedData[0];
y = atoi(&receivedData[1]); //making the integer part
for(byte i=0; i < serialBuffer; i++){
receivedData[i] = '\0';
}
switch(var){
case 'A' : A = y;
break;
case 'B' : B = y;
break;
case 'C' : C = y;
break;
case 'D' : D = y;
break;
case '@' : BUT = y;
break;
}
Serial.print("A = ");
Serial.println(A);
Serial.print("B = ");
Serial.println(B);
Serial.print("C = ");
Serial.println(C);
Serial.print("D = ");
Serial.println(D);
Serial.print("BUT = ");
Serial.println(BUT);
newData = false;
}
}
Slave receiver:
#include <Wire.h>
const byte serialBuffer = 10;
char receivedData[serialBuffer];
char sentData[serialBuffer];
boolean newData = false;
int E = 0;
int F = 0;
int G = 0;
int H = 0;
int BUT = 0;
void setup() {
Wire.begin(9);
//Wire.setClock(300000);
Wire.onReceive(readCommand);
Serial.begin(38400);
}
void loop() {
//readCommand(Wire);
//command('@',255, 10);
//command('C',122, 10);
//command('A',05, 10);
//command('D',242, 10);
}
void command(const char command, const byte value, const byte address){
sprintf(sentData, "\n%c%d\r", command,value); //generates a formatted string output with the format <character|decimal> from the char command and value with start- and end-marker and stores it to buffer
const size_t characters {strlen(sentData)};
//Serial.print("length: "); Serial.println(characters);
Wire.beginTransmission(address);
Wire.write(sentData, characters);
Wire.endTransmission();
delay(12);
}
void readCommand(int bytes){
readIncomingData();
storeReceivedData();
}
void readIncomingData(){
static boolean InProgress = false;
static byte i = 0;
char startMarker = '\n';
char endMarker = '\r';
char rc;
while (Wire.available() > 0 && newData == false){
rc = Wire.read();
if (InProgress == true){
if (rc != endMarker){
receivedData[i] = rc;
i++;
if (i >= serialBuffer){
i = serialBuffer - 1;
}
}
else {
receivedData[i] = '\0'; // terminate the string
InProgress = false;
i = 0;
newData = true;
}
}
else if (rc == startMarker){
InProgress = true;
}
}
}
void storeReceivedData(){
char var;
int y = 0;
if (newData == true){
var = receivedData[0];
y = atoi(&receivedData[1]); //making the integer part
for(byte i=0; i < serialBuffer; i++){
receivedData[i] = '\0';
}
switch(var){
case 'E' : E = y;
break;
case 'F' : F = y;
break;
case 'G' : G = y;
break;
case 'H' : H = y;
break;
case '#' : BUT = y;
command('A',BUT,10);
break;
}
Serial.print("E = ");
Serial.println(E);
Serial.print("F = ");
Serial.println(F);
Serial.print("G = ");
Serial.println(G);
Serial.print("H = ");
Serial.println(H);
Serial.print("BUT = ");
Serial.println(BUT);
newData = false;
}
}