Arduino to roborio on i2c

Hello everyone i want to use pixy with arduino because of the easy library but i have an issue i use slave_sender example to send data but i dont know how to receive data with java i2c class.I am going to use readOnly() function to recieve data but the count parameter needs to fill with how many bytes to read from arduino but i dont know how many bytes. The data i want to get is 0-319.I use java iterative.Thanks!https://i.hizliresim.com/YOELAA.jpg

1 Like

I don’t use java but I have gotten the pixy to communicate with arduino over I2C without using the pixy library. If you would like I can post the arduino code and you can use it to figure out how to do it in java with the roboRIO.

If you are sending the data yourself from the Arduino you should know how many bytes of data you are sending. What data type(s) are you sending from the Arduino, as that will show how dictate how many bytes you are sending. In the slave sender example they show that each character in a String will register as a byte for example. If you could post your code for the Arduino in this thread that would also be helpful.

I think it is short should i write 2 bytes right?and the arduino code like this
Wire.write(pixy.blocks[0].x)

That would be so great thanks :slight_smile:


#include <Wire.h>       

byte c[16];
int j = 0;
int i = 0;

void setup() 
{
  pinMode(13, OUTPUT);  //Built in LED
  
  Wire.begin();         // join i2c bus (address optional for master)
  Serial.begin(9600);   // start serial for output

  delay(500);
}

void loop() 
{
  Wire.requestFrom(0x54, 16);   //Ask for 16 bytes from pixy with adress 84 (0x54 in hex)
  
  while (Wire.available())      //If there's data then start the loop
  {
    for (i=0; i<16; i++)        
    {
      c* = Wire.read();       //Store in an array

      if(c* == 85 && c* == 170)     //If we get 170 then 85 then restart because we're out of sync
      {
        i == 1;
      }
    }  
    if(c[2] == 0)               //If the checksum is 0 then we don't have any data so dont print it
    {
    }
    else
    {
      for (j=0; j<16; j++)
      {       
       if (j <= 3)              //Don't print the syncronazation bytes
       {
       }
       else
       {
        Serial.print(c[j]);
        Serial.print("	");    //	 is "tab"
       }

       if(j == 15)
       {
        Serial.println(" ");  //Start a new line at the last byte and turn on an LED
        digitalWrite(13, HIGH);
       }
      }   
    } 
  }

  delay(250);
  digitalWrite(13, LOW);      //Turn off LED and wait because yoou won't even be able to read it if it goes too fast.
  delay(250);
}

This might be helpful for figuring out what the data means http://www.cmucam.org/projects/cmucam5/wiki/Pixy_Serial_Protocol***

We used i2c to send the alliance color, teleop, and auto to the rioduino for LED customization. We used the wpilibib I2C package. You can see the RoboRIO side code on on our Robot.java, with the write method starting on line 316, and our rioduino side code here, with the read method starting on line 126. If the documentation isn’t to par or something doesn’t make sense, just say so.