View Single Post
  #11   Spotlight this post!  
Unread 13-06-2013, 13:17
evanperryg's Avatar
evanperryg evanperryg is offline
IT'S THE BUMP N' DUMP
AKA: Evan Grove
FRC #4536 (The Minutebots)
Team Role: Mentor
 
Join Date: Apr 2013
Rookie Year: 2011
Location: Minneapolis, MN
Posts: 657
evanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond reputeevanperryg has a reputation beyond repute
Re: Use Sidecar I2C port to communicate with Arduino?

Thanks to you all, I got the code fixed up. If you guys see any issues in the code, please tell me so I can get them patched up as soon as possible.
Code:
/*REALLY COOL LIGHTS PROGRAM I2C rev 1.3
------------------------
Evan Grove, Team 2338
released 6/13/2013
rev 0.0: 6/5/2013
This code is used for visual effects on the curved part of the shooter.
During autonomous, LEDs light up in a random pattern. During Teleop, the
lights do a chaser pattern when discs are being fired.

DATA OUTPUTS FROM SIDECAR:
Byte(Bin)       Byte(Dec)        Definition
00000000        0                Autonomous, not shooting
00000001        1                Autonomous, shooting
00000010        2                Teleop, not shooting
00000011        3                Teleop, shooting

ARDUINO UNO I2C CONNECTIONS:
Analog 4 on the arduino is SDA
Analog 5 on the arduino is SCL

DIGITAL SIDECAR I2C CONNECTIONS:
Google it.
Connect analog 0 to a long piece of bare wire. This helps make
the random sequences more random.
*/
#include <Wire.h>//I2C library for Arduino

const int leds[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //arrays make everything better
int i = 0;       //arbitrary integer used in the pattern generation. Probably shouldn't change this.
int d = 999;     //arbitrary integer used in debug sequence.
int w = 0;       //arbitrary integer used in debug sequence.
#define ini 30   //speed of the initialization pattern in ms. Default=30
#define ave 90   //speed of autonomous visual pattern in ms. Default=90
#define aws 125  //length of time visual effect for autonomous shooting is displayed in ms. Default=125
#define spd 15   //speed of teleop light effect in ms. Default=15
#define bfr 12   //length of teleop shooter effect buffer. Default=12
#define scm 9600 //rate of serial communication, leave this alone

void setup() {
  Serial.begin(scm);
  Serial.println("Serial: initialized");
  Serial.println("Serial: checking");
  if (Serial.available() > 0) {
    Serial.println("Serial: successfully initialized");
  }
  for (i=0;i<13;i++) {
    pinMode(leds[i], OUTPUT);     //declare pins
    }
  Serial.println("Digital: outputs set");
  randomSeed(analogRead(0));     //make random() randomer using elecrical noise from analog 0.
  Serial.println("Analog: inputs set");
  Wire.begin(2);                  // join i2c bus
  Serial.println("I2C: initialized");
  delay(1000);
  Wire.onReceive(incoming);
  Serial.println("I2C: ready");
  for (i=0;i<13;i++) {            //these 2 for loops are used as a debug tool
    digitalWrite(leds[i], HIGH);  //to make sure all the LEDs work and are
    delay(ini);                   //conected in the proper order.
    } 
  i = 0;
  delay(100);
  for (i=0;i<13;i++) {
    digitalWrite(leds[i], LOW);
    delay(ini);
    }
  Serial.println("Test pattern completed");
  Serial.println("Ready");
}

void loop() {
  delay(100);
}

void incoming(int bct) {
    while(Wire.available()) {
    /* AUTONOMOUS */
//Autonomous while not shooting
    if (dat == 0) {
           if (d != 0) {
           Serial.println("Auto: not shooting");
           d = 0;
           }
           int ran = random(0,13);
           int ranb = random(0,40);
           int ran2 = map(ranb,0,40,0,13);
           digitalWrite(leds[ran], HIGH);
           digitalWrite(leds[ran2], HIGH);
           delay(ave);                             //ooooh pretty light patterns
           digitalWrite(leds[ran], LOW);
           digitalWrite(leds[ran2], LOW);
     }
   
//Autonomous while shooting
     if (dat == 1) {
           if (d != 1) {
           Serial.println("Auto: shooting");
           d = 1;
           }
        for (i=0;i<13;i++) {
          digitalWrite(leds[i], HIGH);
        }
        delay(aws);
        for (i=0;i<13;i++) {
          digitalWrite(leds[i], LOW);
        }
     }
  
    /* TELEOP */
//Teleop while not shooting
    if (dat == 2) {
           if (d != 2) {
           Serial.println("Tele: not shooting");
           d = 2;
           }
          for (i=0;i<13;i++) {
            digitalWrite(leds[i], LOW);          //set all lights off if we are in teleop and trigger is not pressed
          }
        }
  
//Teleop while shooting
    if (dat == 3) {
           if (d != 3) {
           Serial.println("Tele: shooting");
           d = 3;
            for (i=0;i<13;i++) {
              digitalWrite(leds[i], HIGH);         //simple for() loop that makes a 'chaser' effect
              delay(spd);                          //speed of the pattern is set by integer spd
              digitalWrite(leds[i], LOW);
              delay(spd);
            }
           }
      }
   } 
}
__________________
FRCDesigns Contributor | "There is only one corner of the universe you can be certain of improving, and that's your own self." -Aldous Huxley
2012-2016 | FRC Team 2338: Gear it Forward
2013
Wisconsin Regional Winner 2014 Midwest Regional Finalist 2015 Midwest Regional Chairman's Award, Finalist, Archimedes Division Champion, IRI Semifinalist 2016 Midwest Regional Chairman's Award, Finalist, Archimedes Division Gracious Professionalism Award, R2OC Winner
2015 | FTC Team 10266: Mach Speed
2015
Highland Park Qualifier Winner, Motivate Award
2017-???? | FRC Team 4536: The Minutebots

Thanks to the alliances and friends I've made along the way: 33 74 107 111 167 171 234 548 1023 1089 1323 1625 1675 1732 1756 2064 2077 2122 2202 2358 2451 2512 2826 3936 3996 4039 4085 4241 5006 5401 5568 5847 5934

Last edited by evanperryg : 13-06-2013 at 13:20.