Hi, how would I accomplish the following?
I'm setting up a slave device using ATtiny85, the master is a Raspberry Pi and I can't edit code on it.
To start I used an Arduino Due as the slave as it's easier to upload and have it working with the master.
The master sends an R character and the slave responds with 31 bytes. This is the code the Due slave is using which is good.
#include <Wire.h> //enable I2C.
#define address 45 //default I2C ID number for EZO pH Circuit.
char computerdata[32]; //we make a 20 byte character array to hold incoming data from a pc/mac/other.
byte in_char = 0;
char inData[20]; //used as a 1 byte buffer to store inbound bytes from the pH Circuit.
byte w = 0; //counter used for ph_data array.
void setup() {
Serial.begin(115200);
Serial.println("startup: ");
Wire.begin(45);
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent); // register event
}
void requestEvent() // send data to controller
{
if ((inData[0] == 'R'))
{
computerdata[0] = '7';
computerdata[1] = '.';
computerdata[2] = '6';
computerdata[3] = '1';
Wire.write(1);
Wire.write(computerdata,30);
}
}
void receiveEvent(int howMany) // controller is asking for something
{
while (Wire.available()) { //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
inData[w] = in_char; //load this byte into our array.
w += 1; //incur the counter for the array element.
if (in_char == 0) { //if we see that we have been sent a null command.
w = 0; //reset the counter i to 0.
break; //exit the while loop.
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}
On the ATtiny85 I have to use TinyWireS library and it doesn't allow me to specify how many bytes are sent.
Wire.write(computerdata,30);
has to be
TinyWireS.send(computerdata);
for it to compile but that isn't sending the data the same way as it gives parsing errors on the master.
So how can I do the equivalent of Wire.write(computerdata,30); on the ATtiny85?
I've tried sending the array in a for loop but it doesn't send it correctly either.
Thanks