View Single Post
  #5   Spotlight this post!  
Unread 18-02-2016, 18:28
codetheweb's Avatar
codetheweb codetheweb is offline
Registered User
FRC #3299 (The Warehouse Crew)
Team Role: Programmer
 
Join Date: Mar 2015
Rookie Year: 2014
Location: Chaska
Posts: 6
codetheweb is an unknown quantity at this point
Re: I2C with Arduino

Thanks, works great! In case anyone else stumbles across this, here's the code I used:

Code:
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

Code:
#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);
}
Reply With Quote