View Single Post
  #1   Spotlight this post!  
Unread 31-01-2017, 19:29
TheLegend27 TheLegend27 is offline
Registered User
FRC #3260
 
Join Date: Jan 2017
Location: United States
Posts: 10
TheLegend27 is an unknown quantity at this point
Exclamation I2C Connection Issues

Please Help Me!

I am programming LED's to be used alongside the RoboRio, with the Arduino to control them. But I am having issues. I need to use the I2C, I can not use the serial or other ports. I just cant get it to work!

Here is the code I made for the Arduino:
Code:
#include <Wire.h>

int serialData = 0;
int shouldChange = 0;

int SDI = 2; //Red wire (not the red 5V wire!)
int CKI = 3; //Green wire
int ledPin = 13; //On board LED

int a;
int b;

#define STRIP_LENGTH 84
long strip_colors[STRIP_LENGTH];

void setup() {
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);

  pinMode(SDI, OUTPUT);
  pinMode(CKI, OUTPUT);
  pinMode(ledPin, OUTPUT);

  for (int x = 0; x < STRIP_LENGTH; x++){
    strip_colors[x] = 0;

    randomSeed(analogRead(0));
  }
}

void loop() {
if (Serial.available() > 0){
  serialData = Serial.read();

  if (serialData == 1) {
    setStripSolid(0x0000FF);
  }
  else {
    setStripSolid(0);
  }
  delay (1000);
}
  
}

void receiveEvent(int howMany) { //called upon slave recieve
  while(0 < Wire.available()) {
    byte x = Wire.read();
    Serial.println(x);
    a = int(x);
  }
  while(1 < Wire.available()) {
   byte y =  Wire.read();
   Serial.println(y);
   b = int(y);
  }
    switch (a){
      case 1:
        switch (b){
          case 1:
          shouldChange = 1;
          setStripSolid(0x0056e0); //auto mode blue
          break;
          case 2:
          shouldChange = 1;
          setStripSolid(0x15ff00); //teleop green
          break;
          case 3:
          shouldChange = 1;
          setStripSolid(0xfff600); //test yellow
          break;
        }
      break;
      case 2:
      switch (b){
        case 1:
        shouldChange = 1;
        rotateColor(0xe23804); //brown-out rotate red
        break;
        case 2:
        shouldChange = 1; //idle sharp colors
        sharpColors();
        break;
      }
      break;
      case 3:
      switch (b) {
        case 1:
        shouldChange = 1; //gear orangy yellow
        setStripSolid(0xffef11);
        break;
        case 2:
        shouldChange = 1; //presure red solid for now
        setStripSolid(0xff1111);
        break;
        case 3:
        shouldChange = 1; //timer purple
        setStripSolid(0xff11c3);
        break;
      }
    }
  }
void sharpColors() {
  for(int x = STRIP_LENGTH; x > 0; x -= 5){
    strip_colors[x] = 0xff7b00;   //orange
    strip_colors[x-1] = 0xff7b00; //orange
    strip_colors[x-2] = 0x0033ff; //blue
    strip_colors[x-3] = 0x41d12e; //green
    strip_colors[x-4] = 0x41d12e; //green
  }
}

void rotateColor(int color) { //will this work? WHO KNOWS!
  shouldChange = 0;
  while (shouldChange == 0) {
   for(int x = STRIP_LENGTH; x > 0; x--){
     strip_colors[x] = color;
      post_frame();
   }
   for(int y = STRIP_LENGTH; y > 0; y--){
     strip_colors[y] = 0;
     post_frame();
   }
  }
}
void setStripSolid(int color) { //universal color feed, give hexidecimal. (0x...)
  for(int x = STRIP_LENGTH; x > 0; x--){
    strip_colors[x] = color;
  }
  post_frame();
}
void post_frame (void) { //strip.show equivalent
  //Each LED requires 24 bits of data
  //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 
  //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
  //Pulling the clock low for 500us or more causes the IC to post the data.

  for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
    long this_led_color = strip_colors[LED_number]; //24 bits of color data

    for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
      //Feed color bit 23 first (red data MSB)
      
      digitalWrite(CKI, LOW); //Only change data when clock is low
      
      long mask = 1L << color_bit;
      //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
      
      if(this_led_color & mask) 
        digitalWrite(SDI, HIGH);
      else
        digitalWrite(SDI, LOW);
  
      digitalWrite(CKI, HIGH); //Data is latched when clock goes high
    }
  }

  //Pull clock low to put strip into reset/post mode
  digitalWrite(CKI, LOW);
  delayMicroseconds(500); //Wait for 500us to go into reset
}
But it just does not work?
And for the RoboRio:
Code:
toSend[0] = 11;
	i2c.transaction(toSend, 1, null, 0);
I really don't know why it does not work. I have the pins from the RoboRio connected to the Arduino (pin SDA to a4 and SCL to pin A5)

What am I doing wrong? I have looked around the internet and I can't find anything. If anyone has a tutorial or some example working code or can find something I did wrong, please let me know. I just don't know what to do!
Reply With Quote