Log in

View Full Version : I2C with Arduino


codetheweb
15-02-2016, 17:01
I'm having some trouble communicating with the Arduino over I2C.

We're planning to use it for LED strips, since almost all our PWM slots are taken. But most examples I find online use Java, not Python. Is there any way to easily write a string over I2C? For example, writing something like "rgb,10,0,255" would turn on the RGB strip.

virtuald
15-02-2016, 17:06
I'm having some trouble communicating with the Arduino over I2C.

We're planning to use it for LED strips, since almost all our PWM slots are taken. But most examples I find online use Java, not Python. Is there any way to easily write a string over I2C? For example, writing something like "rgb,10,0,255" would turn on the RGB strip.

Can you post a link to java code that you think will solve your problem?

codetheweb
15-02-2016, 17:10
Here. (http://www.chiefdelphi.com/forums/showpost.php?p=1441280&postcount=13)

My team got the Arduino communicating with the roborio over I2C using the following code on the roborio

static I2C Wire = new I2C(Port.kOnboard, 4);

if (Global.driver.Buttons.Back.changedDown) {
String WriteString = "go";
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
#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);


}
}

virtuald
15-02-2016, 17:23
First part (http://robotpy.readthedocs.org/en/latest/wpilib/I2C.html#wpilib.i2c.I2C), should be pretty straightforward.


wire = wpilib.I2C(wpilib.I2C.Port.kOnBoard, 4)


Second part (http://robotpy.readthedocs.org/en/latest/wpilib/I2C.html#wpilib.i2c.I2C.transaction)-- I admit the docs aren't super clear here, but it wants you to pass it a list of things that can be converted to bytes... so either integers or a bytestring.


wire.transaction(b'go', 0)


I'm pretty sure that'll work.

It's weird that the java code isn't trying to receive any data (maybe writeBulk is more appropriate?).

codetheweb
18-02-2016, 18:28
Thanks, works great! In case anyone else stumbles across this, here's the code I used:


import wpilib

class MyRobot(wpilib.IterativeRobot):

def robotInit(self):
self.stick = wpilib.Joystick(0)
self.arduino = wpilib.I2C(wpilib.I2C.Port.kOnboard, 4)

def teleopPeriodic(self):
if (self.stick.getTrigger()):
try:
data = self.arduino.transaction(b'go', 0)
except:
pass
else:
try:
data = self.arduino.transaction(b'stop', 0)
except:
pass

if __name__ == "__main__":
wpilib.run(MyRobot)


And


#include <Wire.h>

void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
Serial.print("Hello");

}

void loop()
{
delay(100);
}

void receiveEvent(int howMany)
{
String rxString = "";

while ( Wire.available() > 0 )
{
char n=(char)Wire.read();
if(((int)n)>((int)(' ')))
rxString += n;
}

Serial.print(rxString);
}

virtuald
18-02-2016, 20:38
Awesome, I like how much shorter it is than the Java version: