I was wandering if anyone could help with making an I2C connection between the nxt and arduino.
I know mattallen37 already wrote a library for it but I thought it was a bit too complicated for what I think should be a simple? task of sending information from the nxt and recieving it on the arduino via I2C, especially since the wire library has already been written for such a purpose.
http://mattallen37.wordpress.com/arduino-libraries/
So here is my attempt to perform a simple? master send, slave receive operation.
I have used the 82k resistors required for the sda and scl lines and I used port 1 on the nxt.
I have borrowed code from these examples which show communication between 2 arduinos, and tried to recreate the master arduino code in nxc;
http://arduino.cc/en/Tutorial/MasterReader
http://arduino.cc/en/Tutorial/MasterWriter
I got a bit confused along the way as to how to use some of the functions. I have put some of my questions and problems within stars in the code ******* but I'll repost them here;
1. which of the statements/groups of statements below is best or correct to use in this case?
SetSensorLowspeed(S1);
// this sets the specified port to SENSOR_TYPE_LOWSPEED_9V, so I assume I shouldn't use it since I didnt connect to the 9v line on the nxt?
OR
SetSensorLowspeed(S1, false);
// I assume the false in the above statment just means, I didn't connect the arduino to the 9V line so I'm not using 9v to power it?
OR
SetSensorType(S1, SENSOR_TYPE_LOWSPEED);
SetSensorMode(S1, SENSOR_MODE_RAW);
ResetSensor(S1); // must do this if you change a sensor type or mode
2. when using I2CWrite;
what is the device register for arduino I2C communication? I did find something about TWI being the register but I believe I need a value for its address or something so I can input it here?
anyways, for now I just put 0 and this leads to the following;
this doesn't work:
byte outbuffer[]={2, 0, 1}; // device address = 2, device register = 0 (dont know what it should be), data to send = 1
rtncode1=I2CWrite(S1, 0, outbuffer);
// I get the error; error parsing expression: outbuffer
// considering this is what the API reference says we should do, why doesn't it work?
BUT this compiles fine, why?:
rtncode1=I2CWrite(S1, 0, 1)
NXC CODE
Code: Select all
int rtncode1;
int rtncode2;
sub send_data();
task keep_alive();
task do_stuff();
task main()
{
//schedule tasks
Precedes(keep_alive,do_stuff);
//******question; which of the statements/groups of statements below is best or correct to use in this case?******
//initialise arduino port
//SetSensorLowspeed(S1); // this sets the specified port to SENSOR_TYPE_LOWSPEED_9V,
// I think it also uses the default mode as RAW
// it calls ResetSensor to perform the InvalidDataField reset loop
// OR
// SetSensorLowspeed(S1, false);
// OR
SetSensorType(S1, SENSOR_TYPE_LOWSPEED);
SetSensorMode(S1, SENSOR_MODE_RAW);
ResetSensor(S1); // must do this if you change a sensor type or mode
Wait(6000); // wait a little 6 secs for the arduino to be ready to start receiving
TextOut(0, LCD_LINE1, "nxt ready");
}
task keep_alive()
{
while(1)
{
//do nothing
}
}
task do_stuff()
{
//byte outbuffer[]={2, 0, 1}; // *******problem; what is the device register for writing datat to the arduino?
send_data(); // I just put 0 because i didn't know what else to put
} // device address = 2 and data to send = 1*********
sub send_data()
{
TextOut(0, LCD_LINE2, "sending");
//rtncode1=I2CWrite(S1, 0, outbuffer); // ******problem, I'm not sure how to use IsCWrite because; it gives the error;
// error parsing expression: outbuffer****
rtncode1=I2CWrite(S1, 0, 1) // ******this compiles fine, why??******
rtncode2=I2CCheckStatus(S1);
NumOut(0, LCD_LINE3, rtncode1);
NumOut(0, LCD_LINE4, rtncode2);
}
nxt ready
sending
0
32
ARDUINO CODE
Code: Select all
#include <Wire.h>
#define LEDpin 13 // Built in LED
void setup()
{
Serial.begin(9600); // start serial for output
pinMode(LEDpin, OUTPUT); // initialize LED pin
blinkLED13(); // blink built in LED to indicate the program has booted
// also gives time 3 secs for nxt to initialise port for I2C
Wire.begin(2); // join i2c bus with address #2
Wire.onReceive(read_data); // register event
Serial.println("Arduino ready");
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void read_data(int howMany) // **not sure what the point of howMany is?**
{
Serial.println("receiving");
int x = Wire.read(); // receive data
Serial.println(x); // print the data
}
void blinkLED13()
{
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
digitalWrite(LEDpin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
LED blinks
Arduino ready
Clearly the read_data function never runs and the arduino doesn't recieve anything