Hello Mike and others,
First of all, Mike, thank you for your help.
It appears that I made a mistake. As told I could make sending from a master work, also I could make receiving from a slave work, but couldn't combine them in one script. I thought I did something wrong. But I got it working.
I'll post my master and slave script here so it will be findable for anyone in the future. It's not the best code ever and there are a few weird jumps but that's because I'm implenting stuff later on. Still, I think it can be very usefull.
The master script has basically two parts. One part to send data to the slaves. Now consisting of just two functions(which can easily be only one) that turns a led on the slave on or off. The other part receives data from a slave on request. Now there's two way communication between the master and a slave.
On the slave side the code consists of two parts as well. One parts to answer to the master asking for data, and one part to recieve. I've built in a case switch already, although it's not of great use at the moment in the script.
If someone is working on this in the future and has as many problems to get it working as me. Feel free to PM me or contact me via my website www.robhebing.nl
Mastercode:
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus
}
void loop()
{
//get data of a analogIn from a slave
requestFromSlave(4);
//turn led 13 on at the master and the slave
digitalWrite(13,HIGH);
slavePinOn(4,13);
delay(500);
//turn led 13 off at the master and the slave
digitalWrite(13,LOW);
slavePinOff(4,13);
delay(500);
}
void requestFromSlave(int slave){
Wire.requestFrom(slave, 2); //request 2 bytes.
while(Wire.available()>1)
{
int pin = Wire.receive(); //in this example the master will receive a pin and a value for an analogWrite
int value = Wire.receive();
analogWrite(pin,value);
}
}
void slavePinOn(int slaveID, int pin){
Wire.beginTransmission(slaveID);
Wire.send('a');
Wire.send(pin);
Wire.send(1);
Wire.endTransmission();
delayMicroseconds(10);
}
void slavePinOff(int slaveID, int pin){
Wire.beginTransmission(slaveID);
Wire.send('b');
Wire.send(pin);
Wire.send(0);
Wire.endTransmission();
delayMicroseconds(10);
}
Slavecode:
#include <Wire.h>
byte analogValue=0;
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onRequest(requestEvent); // register event
Wire.onReceive(receiveEvent);
for(int i=2; i<=13; i++){ //set some pins as output
pinMode(i,OUTPUT);
}
}
void loop()
{
//prepare a analog value
int tempValue = analogRead(0);
analogValue = map(tempValue, 0, 1023, 0, 255);
}
void receiveEvent(int howMany)
{
while(Wire.available() < 2); // Wait for 2 bytes to become available
char caseState = Wire.receive();
switch (caseState){
case 'a':{
int pin = Wire.receive();
int ledValue =Wire.receive();
pinOn(pin);
}
break;
case 'b':{
int pin = Wire.receive();
int ledValue =Wire.receive();
pinOff(pin);
}
break;
}
}
void requestEvent()//When data is requested by the master send a bytearray back.
{
byte data[2];
data[0] = 11;//the pin on the master
data[1] = analogValue;//the value
Wire.send(data,2);
}
void pinOn(int pin){//function to turn pin on
digitalWrite(pin,HIGH);
}
void pinOff(int pin){//function to turn pin off
digitalWrite(pin,LOW);
}