I was going to try to continue a thread from the old system, but it looks like i must start a new one.
I have been playing with i2c multiplexers with some success, and want to use a slave arduino as an intelligent multiplexer/ io system.
Typically with mcp 23008 you send a "command byte" to either read or write a specific register, and then if writing, send a byte (or bytes) to write; or if reading, request a byte (or bytes) to get your answer.
I want to have some basic commands to ask my slave to read values (either the result of it's calculations or input readings).
I also want to be able to pass values to the slave to tell it what to do (maybe write to outputs, or run a predetermined subroutine)
I understand that in the setup i must put: Wire.onReceive(receiveEvent); which interrupts and runs receiveEvent when somebody on the i2c bus is trying to send the slave something.
(As an aside, i noticed in most examples, void receiveEvent(int howMany) without any further reference to howMany . Is that just sloppy that they left provision for howMany, but didn't use it, or is it required?)
What i did was to have receiveEvent read the first value as a Register, and read the rest into an array data[] which keeps collecting until the transmitter stops transmitting.
The writing routines seem simple in that, i can use switch /case based on the Register sent and decide where to write the data (i may need to work out global variables for that...)
void receiveEvent(int howMany)
{
int data[];
byte beer;
byte i=0;
byte Register = Wire.receive();
while( Wire.available()) // loop through all incoming data
{
data= Wire.receive();
i++;
}
Wire.endTransmission();
switch(Register()){
case 0x00: // write a/d 0
digitalWrite(A0, data[0]);
break;
case 0x01: // write a/d 1
digitalWrite(A1, data[0]);
break;
I am concerned about the reading routines. If i had a case statement for the reading routine that recognizes the master is looking for something. i need to find the easiest way to send it. if the master were to follow it's write with a read request: Wire.requestFrom(address, bytes); then the slave would jump into Wire.onRequest(requestEvent); subroutine .
can i
1. not have a Wire.onRequest(requestEvent); so that the next statement in my case executes and sends the data back.
2. set a flag that tells the requestEvent which register was requested, and what data to send.
3. send the data automatically, and set my master up with a Wire.onRequest(requestEvent); to jump out and receive the data.
thanks for the help
doug