Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   I2C Connection Issues (http://www.chiefdelphi.com/forums/showthread.php?t=154544)

TheLegend27 31-01-2017 19:29

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!

TheLegend27 04-02-2017 08:41

Re: I2C Connection Issues
 
Also, I don't know if I even connected it right. Is it even worth it to continue with i2c?

flemdogmillion 04-02-2017 08:44

Re: I2C Connection Issues
 
Are you allowed to use basic DIO?

TheLegend27 04-02-2017 08:46

Re: I2C Connection Issues
 
Quote:

Originally Posted by flemdogmillion (Post 1640228)
Are you allowed to use basic DIO?

I don't even know what that is

flemdogmillion 04-02-2017 08:48

Re: I2C Connection Issues
 
Quote:

Originally Posted by TheLegend27 (Post 1640231)
I don't even know what that is

Digital Input/Output. Pins on the RIO that can be turned on and off as well as receive on/off signals, just like the digital pins on the Arduino.

TheLegend27 04-02-2017 08:55

Re: I2C Connection Issues
 
How could I use that?

TheLegend27 04-02-2017 09:27

Re: I2C Connection Issues
 
Actually, I want to stick with i2c if possible

flemdogmillion 05-02-2017 16:24

Re: I2C Connection Issues
 
Quote:

Originally Posted by TheLegend27 (Post 1640240)
Actually, I want to stick with i2c if possible

DIO is possibly the easiest form of inter-processor communication.

axton900 05-02-2017 17:09

Re: I2C Connection Issues
 
Take a look at how we accomplished this (in terms of code) here on our repo: https://github.com/FRC-Team-1403/RioDuinoIMUCode


All times are GMT -5. The time now is 09:55.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi