I see that on this board there is an i2c port and then there are i2c pins separately mentioned on D11 and D12, when I connect two mkrZeros together with using the pins I can't get anything to happen with and without pullup resistors. Googling does not help!?!?!?! where is all the info? where is all the documentation?
Seems I had one line missing from sender setup, but that does not explain why receiver didn't get the first message and do stuff with that.
Wire.onReceive(receiveEvent); // function that executes whenever data is received from writer
wiring was done using Vcc pin for pullup voltage through 4k7ohm resistors D11@C1->D11@C2 and D12@C1->D12@C2 for comms according to image below
That image is for reference how I used the resistors. Wiring was done in MKRZero differently, namely using Vcc pin for pullup voltage through 4k7ohm resistors D11@C1->D11@C2 and D12@C1->D12@C2 for comms
Sender (C1)
#include <Wire.h>
int var;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Set onboard LED as an output
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.onReceive(receiveEvent); // function that executes whenever data is received from writer
Wire.write("0"); // send order once to start ping ponging orders between C1 and C2
}
void loop()
{
}
void receiveEvent(int howMany)
{
char c = Wire.read(); // receive a character
if(c == '1')
{
var = 0;
while (var < 10)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)or do something else
delay(50);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW or do something else
delay(50);
var++;
}
Wire.write("0"); // send order to blink LED in C2
}
if(c == '0')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.write("1"); // send order to blink LED in C2
}
}
REceiver(C2)
#include <Wire.h>
int var; //init
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Set LED_BUILTIN as an output
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.onReceive(receiveEvent); // function that executes whenever data is received from writer
}
void receiveEvent(int howMany)
{
char c = Wire.read(); // receive a character
if(c == '0')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED_BUILTIN off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.write("1"); // send order to blink LED_BUILTIN in C1
}
if(c == '1')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED_BUILTIN off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.write("0"); // send order to blink LED_BUILTIN in C1
}
delay(500);
}
void loop()
{
}
hmmm I had found some oversights in code and fixed em...to no avail
C1
#include <Wire.h>
int var;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Set onboard LED as an output
Wire.begin(1); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.onReceive(receiveEvent); // function that executes whenever data is received from writer
var = 0;
while (var < 10)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)or do something else
delay(50);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW or do something else
delay(50);
var++;
}
Wire.beginTransmission(2); //send to master
Wire.write("0"); // send order to blink LED_BUILTIN in C2
Wire.endTransmission(2); //stop sending to master
}
void loop()
{
}
void receiveEvent(int howMany)
{
char c = Wire.read(); // receive a character
if(c == '1')
{
var = 0;
while (var < 10)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)or do something else
delay(50);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW or do something else
delay(50);
var++;
}
Wire.beginTransmission(2); //send to master
Wire.write("0"); // send order to blink LED_BUILTIN in C2
Wire.endTransmission(2); //stop sending to master
}
if(c == '0')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.beginTransmission(2); //send to master
Wire.write("1"); // send order to blink LED_BUILTIN in C2
Wire.endTransmission(2); //stop sending to master
}
}
C2
#include <Wire.h>
int var; //init
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Set LED_BUILTIN as an output
Wire.begin(2); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.onReceive(receiveEvent); // function that executes whenever data is received from writer
}
void receiveEvent(int howMany)
{
char c = Wire.read(); // receive a character
if(c == '0')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED_BUILTIN off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.beginTransmission(1); //send to master
Wire.write("1"); // send order to blink LED_BUILTIN in C1
Wire.endTransmission(1); //stop sending to master
}
if(c == '1')
{
var = 0;
while (var < 3)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)or do something else
delay(100);
digitalWrite(LED_BUILTIN, LOW); // turn the LED_BUILTIN off by making the voltage LOW or do something else
delay(100);
var++;
}
Wire.beginTransmission(1); //send to master
Wire.write("0"); // send order to blink LED_BUILTIN in C1
Wire.endTransmission(1); //stop sending to master
}
delay(500);
}
void loop()
{
}
The beginTransmission()/endTransmission() calls are for the master only. In the onReceive callback you must only get the data the master sent to you, you cannot send something actively back, the master controls the bus!
If the master wants data from the slave it uses the requestFrom() method and the slave implements the code in the onRequest callback. There you can only use the write() method, the callback should be fast (no calculations and such stuff) and it should write exactly that many bytes the master requested (that's why I2C communication always follows an exact protocol).
I2C is not a replacement for an UART communication! It's not bidirectional! The master dictates everything.