Go Down

Topic: [Solved!] I2C to transfer data between two Arduinos Unos (Read 301 times) previous topic - next topic

arusr

Apr 01, 2014, 11:49 pm Last Edit: Apr 02, 2014, 01:40 am by arusr Reason: 1
EDIT:  solve!  I had pin 4 and pin 5 of the digital I/O instead of the analog I/O.  Stupid!!!


Hello,
I'm trying to follow the I2C example from this forum and from a couple of other places.  A "writer" arduino sends a struct with two integers.  A "reader" Arduino prints out the two ints it reads.  Hardware wise, I have pin4, pin5, and ground connected between the two Arduinos.  Pin4 to Pin 4 and Pin5 to Pin5, which are suppose to be the analog pins doing the I2C.  I can verify that the "Writer" Arduino is sending values X:  1, 2, 3, 4...etc.  But the Reader Arduino isn't seeing the data, and is not executing receiveEvent().

I'd appreciate any input on why this might not be working.  

"Writer" Arduino, sending the values
Code: [Select]

// I2C communication between 2 Arduinos
// Analog joystick connected to analog pins A0 and A1
// Values from joystick are sent to the slave

#include <Wire.h>

const byte SLAVE_ADDRESS = 42;

int startx = 0;
int starty = 10;

void setup()
{
 Serial.begin (9600);
 Wire.begin ();
}  // end of setup

void loop()
{
 struct
   {
   int X;
   int Y;
   } XY;
 startx = startx + 1;
 starty = starty + 1;
 XY.X = startx;
 XY.Y = starty;
 if (startx > 100)
 {
   startx = 0;
 }
 if (starty > 100)
 {
   starty = 0;
 }
 Wire.beginTransmission (SLAVE_ADDRESS);
 Wire.write ((byte *) &XY, sizeof XY);
 Wire.endTransmission ();
 Serial.println (XY.X);
 delay(4000);
}  



"Reader" Arduino, reading the values from I2C
Code: [Select]

// I2C communication between 2 Arduinos
// Slave receives the integers X and Y from the master
// Values X and Y are printed to the serial monitor

#include <Wire.h>

const byte MY_ADDRESS = 42;

void setup()
{
 Wire.begin (MY_ADDRESS);
 Serial.begin (9600);
 Wire.onReceive (receiveEvent);
 Serial.println ("hi hi");
}  // end of setup

volatile struct
  {
  int X;
  int Y;
  } XY;
   
volatile boolean haveData = false;

void loop()
{
 if (haveData)
 {
   Serial.print ("Received X = ");
   Serial.println (XY.X);  
   Serial.print ("Received Y = ");
   Serial.println (XY.Y);  
   haveData = false;  
 }  // end if haveData

}  // end of loop

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
 if (howMany < sizeof XY)
   return;
   
 // read into structure
 byte * p = (byte *) &XY;
 for (byte i = 0; i < sizeof XY; i++)
   *p++ = Wire.read ();
   
 haveData = true;
 Serial.println ("I see something!");
}


I got this from Nick Gammon's 4th post in this thread:
http://forum.arduino.cc/index.php?topic=126936.0

arusr

#1
Apr 02, 2014, 12:11 am Last Edit: Apr 02, 2014, 12:18 am by arusr Reason: 1
Ok, I tried another example, one where I'm relying on LED's instead of println statements to debug whether the I2C is working or not.  In addition, I'm also tying the 5V line as well as pin4, pin5, and GND together now.  And the two Arduinos are running off the exact same 5V DC adapter, one being powered off the other.  But still no joy.  The Reader always sees X is 0.


Writer Arduino:
Code: [Select]

#include <Wire.h>

#define LED_PIN 13
byte x = 0;

void setup()
{
 Wire.begin(); // Start I2C Bus as Master
 pinMode(LED_PIN, OUTPUT);
 digitalWrite(LED_PIN, LOW);

}
void loop()
{
 
 Wire.beginTransmission(9); // transmit to device #9
 Wire.write(x);              // sends x
 Wire.endTransmission();    // stop transmitting
 x++;
 if (x > 5) x=0;
 if (x == 0) digitalWrite(LED_PIN, HIGH);
 if (x > 0) digitalWrite(LED_PIN, LOW);
 delay(2000);
}




Reader/Receiver Arduino:
Code: [Select]

#include <Wire.h>

#define LED_PIN 13

int x;

void setup() {
 Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
 Wire.onReceive(receiveEvent); // register event
 
 pinMode(LED_PIN, OUTPUT);
 pinMode(LED_1, OUTPUT);
 pinMode(LED_2, OUTPUT);
 
 digitalWrite(LED_PIN, HIGH);
 digitalWrite(LED_1, LOW);
 digitalWrite(LED_2, LOW);
 
 x = 0;
}

void loop() {
 //If value received is 0 blink LED 1
 if (x == 0) {
   
   digitalWrite(LED_PIN, HIGH);
   delay(200);
   digitalWrite(LED_PIN, LOW);
   delay(200);
 }
}

void receiveEvent(int howMany) {
 x = Wire.read();    // receive byte as an integer
}



Go Up