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);
}
}
}
}
}