I2C Between Gamebuino and Mega 2560

Show us your Gamebuino clone or the last module you made.

I2C Between Gamebuino and Mega 2560

Postby awesome101 » Sat Oct 17, 2015 9:03 pm

Hi everybody! I'm making an 8 bit "mini computer" from the gamebuino. The 2560 is supposed to handle the keyboard, which is a 4x4 tactile button matrix. By the way, if you are wondering how I'm going to write 26 letters with it, 13 buttons will be used to write the first 13 letters of the alphabet, and the other 13 will be used by holding the 14th button and pressing one of the 13 buttons, to write the other half of the alphabet. The remaining 2 buttons will be used for backspace and space. Anyways I am using the wire library, and the gamebuino is the host, and the mega is the slave. When I run the host hex file for the gamebuino, it crashes. The size of the sketch is less than 30,000, and is exactly 12,778 bytes. The mega seems to be running fine. Here is the code:

Host(Gamebuino) file:

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
#include <Wire.h>

int x = 0;

Gamebuino gb;

void setup()
{
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("I2C Arduinos"));
 
  Wire.begin();
}

void loop()
{
  if(gb.update()) {
    Wire.beginTransmission(9); // transmit to device #9
    Wire.write(x);              // sends x
    gb.display.println("Transmitting...");
    Wire.endTransmission();    // stop transmitting
  }
}


Slave(Mega) Code:
Code: Select all
#include <Wire.h>

int x = 0;

void setup()
{
  // put your setup code here, to run once:
  Wire.begin(9);
  Wire.onReceive(receiveEvent);
 
  Serial.begin(9600);
}

void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

void loop()
{
  Serial.println(x);
}


Help, as always, is appreciated.
Thanks! :D
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: I2C Between Gamebuino and Mega 2560

Postby Sutchig » Sun Oct 18, 2015 6:10 pm

Hi, according to https://www.arduino.cc/en/Reference/WireWrite you should use a byte to transmit, or give a length (2 byte for int) i don't know if automatically casting will work.
Second: you may put some delay in the inner loop (at the end). Only for testing.
And use a different value than 0, as this is already on the mega ;)

The wire library will give return codes. You may look at them ( E.g. https://www.arduino.cc/en/Reference/WireEndTransmission ) and break current transmission on error.
Hope, this is some help :)
Sutchig
 
Posts: 67
Joined: Sat May 23, 2015 3:48 pm

Re: I2C Between Gamebuino and Mega 2560

Postby jonnection » Mon Oct 19, 2015 7:50 am

Actually, the int is cast to uint8_t automatically

From Wire.h:

Code: Select all
    inline size_t write(unsigned long n) { return write((uint8_t)n); }
    inline size_t write(long n) { return write((uint8_t)n); }
    inline size_t write(unsigned int n) { return write((uint8_t)n); }
    inline size_t write(int n) { return write((uint8_t)n); }
    using Print::write;


But what you are doing is sending a null (0) character. I do not know how the Wire lib handles that. In ascii character streams in general null means end of string and I am not sure if the recieving end should actually do anything other than abort when a null character appears. It is possible that your code is doing exactly what it is supposed to, but you keep sending a string termination character, so nothing happens.

I would declare char x = 'x'; and send that instead.
User avatar
jonnection
 
Posts: 317
Joined: Sun May 04, 2014 8:21 pm


Return to Hardware Gallery

Who is online

Users browsing this forum: No registered users and 1 guest

cron