I managed to get code to send 1 variable working, but I can't find any examples on how to send 2 or more. Does anyone know how to?
There is a 32 byte limit per I2C transaction (on some platforms).
Can you pack all the data into a 32 byte struct ?
Otherwise, you have to break it down into 32 byte chunks.
I only need to send 1s or 0s
Can you post the code you have already written and say what additional variables you would like to transfer between the two Arduinos.
Slave code:
#include <Wire.h>
#define SLAVE_ADDR 9
#define LED 4
#define LED1 13
int rd;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int byt){
rd = Wire.read();
}
void loop() {
Serial.println(rd);
digitalWrite(LED, rd);
}
Master code
#include <Wire.h>
#define SLAVE_ADDR 9
#define LED 4
#define LED1 13
int rd;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int byt){
rd = Wire.read();
}
void loop() {
Serial.println(rd);
digitalWrite(LED, rd);
}
For now I would like to transfer values of button reads.
The master and slave code look remarkably similar.
Maybe you would be better off using a library for I2C:
ok, i'll try that, thanks!
how to install it?
Copy the following file (or make new file with the same name and paste in the contents) into the folder of your current sketch :
I2C_Anything.h
1. Connect UNO-NANO or UNO1-UNO2 as per following diagram (Fig-1) using I2C Bus.
Figure-1:
2. Upload your following revised Master sketch to send data byte 0x12 and data byte 0x45 to Slave.
#include <Wire.h>
#define SLAVE_ADDR 0x23
void setup()
{
Serial.begin(9600);
Wire.begin();
//----------------------
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(0x12); //sending 0x12 to Slave
Wire.write(0x45); //sending 0x45 to Slave
Wire.endTransmission();
}
void loop()
{
}
3. Upload your following revised Slave sketch to receive/print two data bytes from Master.
#include <Wire.h>
#define SLAVE_ADDR 0x23
volatile byte myData[2];
volatile bool flag = false;
void setup()
{
Serial.begin(9600);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
}
void loop()
{
if (flag == true)
{
Serial.println(myData[0], HEX); //shows: 12
Serial.println(myData[1], HEX); //shows: 45
flag = false;
}
}
void receiveEvent(int howMany) //howMany = data bytes received from Amster = 2 here
{
for (int i = 0; i < howMany; i++)
{
myData[i] = Wire.read();
}
flag = true;
}
4. Chcek that Serial Monitor of Slave shows:
12
45
You can more easily communicate over I2C using SerialTransfer.h.
Here's an example:
TX:
#include "I2CTransfer.h"
I2CTransfer myTransfer;
struct STRUCT {
char z;
float y;
} testStruct;
char arr[] = "hello";
void setup()
{
Serial.begin(115200);
Wire.begin();
myTransfer.begin(Wire);
testStruct.z = '$';
testStruct.y = 4.5;
}
void loop()
{
// use this variable to keep track of how many
// bytes we're stuffing in the transmit buffer
uint16_t sendSize = 0;
///////////////////////////////////////// Stuff buffer with struct
sendSize = myTransfer.txObj(testStruct, sendSize);
///////////////////////////////////////// Stuff buffer with array
sendSize = myTransfer.txObj(arr, sendSize);
///////////////////////////////////////// Send buffer
myTransfer.sendData(sendSize);
delay(500);
}
RX:
#include "I2CTransfer.h"
I2CTransfer myTransfer;
struct STRUCT {
char z;
float y;
} testStruct;
char arr[6];
void hi()
{
// use this variable to keep track of how many
// bytes we've processed from the receive buffer
uint16_t recSize = 0;
recSize = myTransfer.rxObj(testStruct, recSize);
Serial.print(testStruct.z);
Serial.print(testStruct.y);
Serial.print(" | ");
recSize = myTransfer.rxObj(arr, recSize);
Serial.println(arr);
}
// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
void setup()
{
Serial.begin(115200);
Wire.begin(0);
configST myConfig;
myConfig.debug = true;
myConfig.callbacks = callbackArr;
myConfig.callbacksLen = sizeof(callbackArr) / sizeof(functionPtr);
myTransfer.begin(Wire, myConfig);
}
void loop()
{
// Do nothing
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
