Im trying to get 2 nanos to communicate with each other via I2C but the example code seems to lock up each time I join the 2 Arduinos together. Im using the example code provided with the arduino enviroment (master_writer and slave_reader) they work fine by themselves, but as soon as i hook up my data or clock lines (A4 and A5 if im not mistaken) the code seems to lock up at any time of the code. I am using 10K pull up resistors and both nanos run without a hitch with any other code I use (that dosent use I2C)
Yes I am nick... the attached picture is my setup at the moment (sorry if its not clear enough)...
I am using serial debugging (through the Arduino IDE) and I have a simple Serial.println() to tell me where the code is up to... the master runs fine without the slave hooked up, but as soon as I connect the slave, the whole thing freezes.... I dont know if I'm doing anything wrong or not... I'm just following tutorials and looking at codes on the internet
That looks OK, once I downloaded your 1.4 Megabyte, 3264 x 1952 image. You can always size them down a bit you know. 600 pixels wide is usually plenty.
I'm just following tutorials and looking at codes ...
What code? Can you post it? Or just try the master and slave example on my web page above.
// Written by Nick Gammon
// February 2012
#include <Wire.h>
const byte SLAVE_ADDRESS = 42;
const byte LED = 13;
void setup ()
{
Serial.begin(9600);
Serial.println("Starting Wire Libary");
Wire.begin ();
Serial.println("Setting Pin 13 to Output");
pinMode (LED, OUTPUT);
} // end of setup
void loop ()
{
Serial.println("Starting For Loop");
for (int x = 2; x <= 7; x++)
{
Serial.println("Beginning Wire Transmission");
Wire.beginTransmission (SLAVE_ADDRESS);
Serial.print("Writing ");
Serial.print(x);
Serial.println(" to wire");
Wire.write (x);
Serial.println("Checking End Transmission"); // Displays this on serial than hangs
if (Wire.endTransmission () == 0)
digitalWrite (LED, HIGH);
else
digitalWrite (LED, LOW);
Serial.println("Delay of 200ms");
delay (200);
}
} // end of loop
// Written by Nick Gammon
// February 2012
#include <Wire.h>
const byte MY_ADDRESS = 42;
#define BETA_ARDUINO ARDUINO < 100
void setup ()
{
Serial.begin(9600);
Serial.println("Starting Wire Libary");
Wire.begin (MY_ADDRESS);
Serial.println("Setting Pins 2 to 7 to output");
for (byte i = 2; i <= 7; i++)
pinMode (i, OUTPUT);
Serial.println("Enabling Wire Interrupt"); // Displays this on serial than hangs
Wire.onReceive (receiveEvent);
} // end of setup
void loop()
{
// nothing in main loop
}
// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
Serial.println("Wire Available");
while (Wire.available () > 0)
{
Serial.println("Setting c as a byte");
byte c;
Serial.println("Reading Wire data and setting it as c");
c = Wire.read ();
Serial.print("c = ");
Serial.println(c);
// toggle requested LED
if (digitalRead (c) == LOW)
digitalWrite (c, HIGH);
else
digitalWrite (c, LOW);
}
} // end of receiveEvent
Take those serial prints out of the receiveEvent. You can't do that inside an ISR. Do something else like turn on an LED. Or set a flag which you test in the main loop.