Hi all
I use I2C code but now I had problem.
How to show Master send char in Slave Monitor ?
use the code // Serial.print(incomingByte); //
It's only show one time.
How to do in Void loop can save in buffer and When I want can reprint the char ?
under is my code file
//Master code
// master_sender.ino
// Refer to the "slave_receiver" example for use with this
#include <Wire.h>
const int SLAVE_ADDRESS = 1;
char incomingByte = 0;
void setup() {
Wire.begin(); // join I2C bus as a Master
Serial.begin(9600);
Serial.println("Type something to send:");
}
void loop() {
}
void serialEvent()
{
// read one byte from serial port
incomingByte = Serial.read();
// send the received data to slave
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(incomingByte);
Wire.endTransmission();
}
//Slave code
// slave_receiver.ino
// Refer to the "master_sender" example for use with this
#include <Wire.h>
const int SLAVE_ADDRESS = 1;
char incomingByte = 0;
void setup() {
Wire.begin(SLAVE_ADDRESS); // join I2C bus as a slave with address 1
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
Serial.println("Received data:");
}
void loop() {
}
void receiveEvent(int howMany)
{
while (Wire.available())
{
// receive one byte from Master
incomingByte = Wire.read();
Serial.print(incomingByte);
}
}
Try the following codes - a slight modification of your sketch:
#include <Wire.h>
const int SLAVE_ADDRESS = 0x08;//1;
char incomingByte = 0;
void setup()
{
Wire.begin(); // join I2C bus as a Master
Serial.begin(9600);
Serial.println("Type something to send:");
}
void loop()
{
//void serialEvent()
//{
// read one byte from serial port
byte n = Serial.available();
if (n != 0)
{
incomingByte = Serial.read();
// send the received data to slave
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(incomingByte);
Wire.endTransmission();
}
}
//Slave code
// slave_receiver.ino
// Refer to the "master_sender" example for use with this
#include <Wire.h>
volatile bool flag1 = false;
const int SLAVE_ADDRESS = 0x08;//1;
char incomingByte = 0;
void setup()
{
Wire.begin(SLAVE_ADDRESS); // join I2C bus as a slave with address 1
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
Serial.println("Received data:");
}
void loop()
{
if (flag1 == true)
{
//while (Wire.available())
//{
// receive one byte from Master
incomingByte = Wire.read();
Serial.print(incomingByte); //never issue print() command in receive() interrupt handler
flag1 = false;
}
}
void receiveEvent(int howMany)
{
flag1 = true;
}
Thanks a lot, is really helpful.
but another problem , if I want in the slave monitor to reprint data again
how should I do ?
Hsieh13:
but another problem , if I want in the slave monitor to reprint data again
how should I do ?
You mean --
You want print the old data (that one received a while ago by the Slave) again.
Or
You want to print the next data byte to be received from Master.
In fact, your query is not clear to me.
like you say i want print the old data again
Hsieh13:
like you say i want print the old data again
Old (previous) data is already in the variable incomingByte; just print it:
Serial.print(incomingByte);