Go Down

Topic: Arduino Yun and Wire Library (Read 792 times) previous topic - next topic

PineCone

Jan 29, 2016, 07:01 am Last Edit: Jan 29, 2016, 05:40 pm by PineCone
Hello - I am trying to do some very basic Arduino to Arduino communication.  I am using some code straight out of a book.  However, the Yun's IDE eventually hangs after trying to send a few messages to the other Arduino, a Mega 2560.  I have checked the TX and RX wires and there is a common ground.  The IDE for the Yun is 1.6.6.

The Yun is supposed to send data with this code:

Code: [Select]
#include <Wire.h>
const int address = 4; // the address to be used by the communicating devices

void setup()
{
  Wire.begin();
}

void loop()
{
  char c;
  if(Serial.available() > 0 )
  {
    // send the data
    Wire.beginTransmission(address); // transmit to device
    Wire.write(c);
    Wire.endTransmission();
  }
}


and the Mega is supposed to receive it and echo it to the serial monitor with:
Code: [Select]
#include <Wire.h>
const int address = 4; // the address to be used by the communicating devices

void setup()
{
 Serial.begin(9600);
 Wire.begin(address); // join I2C bus using this address
 Wire.onReceive(receiveEvent); // register event to handle requests
}

void loop()
{
 // nothing here, all the work is done in receiveEvent
}

void receiveEvent(int howMany)
{
 while(Wire.available() > 0)
 {
   char c = Wire.read(); // receive byte as a character
   Serial.write(c); // echo
 }
}


Does anyone have any idea why the IDE would lock up with something pretty basic?  Thanks for any help.

ShapeShifter

Please use code tags when posting: click the </> button, then paste your code between the tags. It makes it much easier to read.

I think you pasted the slave code twice: both sets of code are waiting for data to come in, but nobody is sending anything?

PineCone

Thanks, I have fixed everything.  I appreciate the suggestion.

ShapeShifter

Thanks, I have fixed everything.
Do you mean you fixed the post (which I see is true) or you fixed your actual problem?

If you fixed your problem, what was your solution?

One thing that jumps out at me with the sender code is that it doesn't call Serial.begin(). Because of the nature of the Yun's USB serial port, the port may be functional without calling Serial.begin(), but perhaps some internal buffers of the Serial class aren't set up properly if Serial.begin() isn't called. If so, perhaps it eventually hits a point where any more data causes problems? (Just a wild guess - a stab in the dark.)

PineCone

#4
Jan 30, 2016, 02:01 am Last Edit: Jan 30, 2016, 02:14 am by PineCone
Sorry, I meant I fixed my post.

I have spent hours and hours with this code from a book without success.  However I was able to get the Wire library to work in minutes using the examples in the IDE and an UNO as master.  So there is something I don't understand about the book code or else it is missing something as you mention or else it is a Yun issue as master.  The slave code works but the master code does not.  Regardless, I wanted to log in to thank you again and to say we can close this thread.

Go Up