Hello,
I am trying to store two values in master via serial monitor and after storing those values i am sending to the slave where i have to perform arithmatic operations between these two values and result will be sent back to the master.My question is I am sending values from master but slave is not receiving the value so please help me out for this query.I am attaching code below.
thanks in advance.
//Master................................................................................
#include <Wire.h>
int a;
int b;
int c;
int d;
void setup(){
Serial.begin(9600);
delay(100);
Serial.println("\n Please enter the values for Operations"); // enter values of a and b via serial Monitor
}
void loop(){
a = 0; // a initiated with 0 first
while(Serial.available() == 0){}
while(Serial.available()){
byte incoming = Serial.read();
a = incoming - '0';
Serial.print("a = ");
Serial.println(a);
delay(1000);
int c = a; // store value of a in c
Serial.print("Now the value in c is = ");
Serial.println(c);
}
b = 0;
while(Serial.available() == 0){}
while(Serial.available()){
byte incoming = Serial.read();
b = incoming - '0';
Serial.print("b = ");
Serial.println(b);
byte d = b;
Serial.print("Now the value in d is = ");
Serial.println(d);
delay(5000);
}
Wire.beginTransmission(1); // begin transmission to slave1
Wire.write("c is ");
Wire.write(c); // sending value of c
Wire.write("d is ");
Wire.write(d);// sending value of d
Wire.endTransmission();
delay(500);
}
Slave.........................................................................
#include <Wire.h>
void setup() {
Wire.begin(1);// slave address is 1
Wire.onReceive(recieveEvent);
Serial.begin(9600);
}
void loop() {
delay(100);
}
void recieveEvent(int howMany){
while(1 < Wire.available()){
char e = Wire.read();
Serial.print(e);
// char f = Wire.read();
// Serial.print(f);
}
int c = Wire.read();// read data from master
Serial.println(c);
//int b = Wire.read();// read data from master
//Serial.println(b);
}