Hello to all
I am having great difficulty communicating between my Pixy2 to Arduino Uno, then to the RoboRIO.
I have read several posts on this matter, but they are so way over my head. I have no problem with the electrical hookup between all 3 devices, and I have programmed the Arduino to read the Pixy2 Line following device, but that’s as far as I got.
I have a very basic knowledge of Java programming, so, is there a very, very basic program that communicates with the Arduino using the I2C port? I just want to use the x, y, coordinates from the pixy2 to tell our operator that the robot is on the tape.
I would need a detailed and complete description on how to program this. If this requires some advanced training on my part, I’m not adverse to be told to pack it in.
Thanks,
If you want to send information from the arduino to the roborio (or vice-versa) over I2C you need to connect 3 wires from the roborio to the arduino. Ground (both sides), SDA (Analog 4 on the Arduino Uno) and SCL(Analog 5 on the Arduino). After you connect them electrically, you can use the I2C library in WPI library for the Roborio and the Wire library for the Arduino Uno.
We have wrote code that transfers data from the Roborio to the Arduino(In java), i can send it tomorrow if you need it.
I think this should work. I assume you use command based java code
public class YourCommandName extends Command {
byte[] dataGet;
byte[] dataSend;
I2C com;
int x,y;
@Override
protected void initialize() {
dataGet = new byte[2];//1 byte is between 0 and 255. 2 bytes are between 0 and 65535
dataSend = new byte[1];//0 send x 1 send y
com = new I2C(I2C.Port.kOnboard, 4);//4 is the roborio ID in the i2c communication
}
@Override
protected void execute() {
dataSend[0]=0;
this.com.transaction(dataSend, dataSend.length, dataGet ,dataGet.length);
x=((dataGet[0] & 0xff) << 8) | (dataGet[1] & 0xff);
dataSend[0]=1;
this.com.transaction(dataSend, dataSend.length, dataGet ,dataGet.length);
y=((dataGet[0] & 0xff) << 8) | (dataGet[1] & 0xff);
}
}
and this is the Arduino code
#include <Wire.h>
int x=0;
int y=0;
int state=0;
void setup()
{
Wire.begin();
Wire.onReceive(receive);
Wire.onRequest(push);
}
void receive(int bytesSent)
{
while(Wire.available()){
value = Wire.read();
}
void push()
{
if(state==0)
Wire.write(x,2);
else//state is 1
Wire.write(y,2);
}
void loop()
{
//Add your pixy code that changes x and y
delay(10);
}
Hello OmerZ7
Thanks for the code. I got some of it working, but have issues with parts of it.
I’m having multiple arduino errors when I try and run the code.
I notice you have a curly bracket after line, ‘void receive(int bytesSent)’ , but a closing bracket is missing. Does it go just before the line, ‘void push()’ ?
I also get an error from line, ‘else Wire.write(y,2);’ It says "No matching function call to "write(int, int);