Go to Post This whole experience, the entirety of what FIRST represents and promotes, is about inspiring individuals to make something more of themselves and creating a better world; when you consider this, our individual victories seem meaningless. - Dillon Compton [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #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
  #2   Spotlight this post!  
Unread 04-02-2017, 08:41
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
Re: I2C Connection Issues

Also, I don't know if I even connected it right. Is it even worth it to continue with i2c?
Reply With Quote
  #3   Spotlight this post!  
Unread 04-02-2017, 08:44
flemdogmillion's Avatar
flemdogmillion flemdogmillion is offline
Programmer, Builder, Driver...
FRC #3007
 
Join Date: Nov 2016
Rookie Year: 2015
Location: Minnesota
Posts: 138
flemdogmillion will become famous soon enoughflemdogmillion will become famous soon enough
Re: I2C Connection Issues

Are you allowed to use basic DIO?
__________________
Team 4506: 2015-2016
Team 3007: 2017

Jack of all trades except C++ & Java
Reply With Quote
  #4   Spotlight this post!  
Unread 04-02-2017, 08:46
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
Re: I2C Connection Issues

Quote:
Originally Posted by flemdogmillion View Post
Are you allowed to use basic DIO?
I don't even know what that is
Reply With Quote
  #5   Spotlight this post!  
Unread 04-02-2017, 08:48
flemdogmillion's Avatar
flemdogmillion flemdogmillion is offline
Programmer, Builder, Driver...
FRC #3007
 
Join Date: Nov 2016
Rookie Year: 2015
Location: Minnesota
Posts: 138
flemdogmillion will become famous soon enoughflemdogmillion will become famous soon enough
Re: I2C Connection Issues

Quote:
Originally Posted by TheLegend27 View Post
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.
__________________
Team 4506: 2015-2016
Team 3007: 2017

Jack of all trades except C++ & Java
Reply With Quote
  #6   Spotlight this post!  
Unread 04-02-2017, 08:55
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
Re: I2C Connection Issues

How could I use that?
Reply With Quote
  #7   Spotlight this post!  
Unread 04-02-2017, 09:27
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
Re: I2C Connection Issues

Actually, I want to stick with i2c if possible
Reply With Quote
  #8   Spotlight this post!  
Unread 05-02-2017, 16:24
flemdogmillion's Avatar
flemdogmillion flemdogmillion is offline
Programmer, Builder, Driver...
FRC #3007
 
Join Date: Nov 2016
Rookie Year: 2015
Location: Minnesota
Posts: 138
flemdogmillion will become famous soon enoughflemdogmillion will become famous soon enough
Re: I2C Connection Issues

Quote:
Originally Posted by TheLegend27 View Post
Actually, I want to stick with i2c if possible
DIO is possibly the easiest form of inter-processor communication.
__________________
Team 4506: 2015-2016
Team 3007: 2017

Jack of all trades except C++ & Java
Reply With Quote
  #9   Spotlight this post!  
Unread 05-02-2017, 17:09
axton900's Avatar
axton900 axton900 is offline
Programming Co-Captain
FRC #1403 (Cougar Robotics)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: New Jersey USA
Posts: 52
axton900 has a spectacular aura aboutaxton900 has a spectacular aura aboutaxton900 has a spectacular aura about
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
__________________
Team 1403: Cougar Robotics (2015 - present)

Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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