Thread: Pixy I2C
View Single Post
  #2   Spotlight this post!  
Unread 02-11-2015, 06:18 PM
Rakusan2 Rakusan2 is offline
Registered User
AKA: Tomas Rakusan
FRC #3571 (Milton Mustangs)
Team Role: Programmer
 
Join Date: May 2014
Rookie Year: 2011
Location: Milton, ON, Canada
Posts: 23
Rakusan2 is an unknown quantity at this point
Re: Pixy I2C

My team got the Arduino communicating with the roborio over I2C using the following code on the roborio
Code:
//this is outside a method
I2C Wire = new I2C(Port.kOnboard, 4);

//this is inside a method
if (Global.driver.Buttons.Back.changedDown) {
	String WriteString = "go"; //this is being sent to the Arduino
	char[] CharArray = WriteString.toCharArray();
	byte[] WriteData = new byte[CharArray.length];
	for (int i = 0; i < CharArray.length; i++) {
		WriteData[i] = (byte) CharArray[i];
	}
	Wire.transaction(WriteData, WriteData.length, null, 0);
}
And this code on the Arduino which was made to turn on an LED when it received the message "go"
Code:
#include <Wire.h>

void setup()
{
  pinMode (13, OUTPUT);
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  String LED = "";
 
  while ( Wire.available() > 0 )
  {
    char n=(char)Wire.read();
    if(((int)n)>((int)(' ')))
   LED += n; 
  }
  
  if (LED == "go") 
  {
    
    digitalWrite (13, HIGH);
  
    
  }
}

Last edited by Rakusan2 : 02-11-2015 at 06:19 PM. Reason: spelling mistake
Reply With Quote