I was wondering if somone might take a look at my code and give me a general overview of anything I am doing "wrong"?
More of a best practices/more experienced eye to see if I am doing easy things the "hard way". I don't need specific code solutions, more of a "Hey dummy, don't use an array that way! Look at the ixnay on the rraynay function."
Overall project: A custom lightsaber driver that interfaces with a "Toy" soundcard and controls, via PWM a 4 color high power LED driver. The idea is to select a color then maintain that color through a fading in and fading out process. When it fades in it also turns on a sound card. When it fades out it turns off the sound card. It also checks for a signal from an Impact sensor and "flickers" the Amber LED for a "clash effect" Lastly, it selects one of two soundbanks from the soundcard.
I tried to be pretty verbose in my comments...
I appreciate any input!
Part one: Setup and Loop
/* Lightsaber test sketch controlling a "Joe Jedi" SW616 sound card
Modified by Troy Ollom
Feb. 10, 2010
Based in part on the "Four button sketch" by Jeff Saltzman.
To keep a physical interface as simple as possible, this sketch demonstrates generating three output events from a single push-button.
1) Click: rapid press and release
2) Double-Click: two clicks in quick succession
3) Press and Hold: holding the button down
Normal mode:
Click: SaberOn (Ramps up LED, turns on sound)
Double-click: (Ramps off LED, turns off sound
Click-Hold: Enters MenuMode 1
Menu Mode 1:
Click: Cycle through RGB colors
Double Click: Skip to next hue of RGB colors
Click-Hold: Enters Menu Mode 2
Menu Mode 2:
Click: Select "Jedi" sound bank
Double Click: Select "Sith" sound bank
Click-Hold: Cycle back to menu mode 0 AKA "Normal"
*/
#include <EEPROM.h> // Set up the EEPROM library so I can read/write from permenant memory
#define buttonPin 2 // analog input pin to use as a digital input
#define redPin 3 // PWM output pin for Red LED 1
#define sndOnPin 4// Digital out to turn on soundcard (Joe Jedi, one side of switch.)
#define grnPin 5 // PWM output pin for Green LED 2
#define bluPin 6// PWM output pin for Blue LED 3
#define impactPin 7// Input pin for impact sensor (Joe Jedi, second yellow wire on plug)
#define sndSelectPin 8// Digital out to select sound True is Sith, False is Jedi (Joe Jedi, left most pad on RGB select switch)
#define flashPin 9 // FoC LED Pin PWM Pin connected to Amber die of RGBA LED driver
// RGB LED variables
byte rgbArray [54][3]={ //RGB Array 3 wide 54 tall, stores RGB values (There's gotta be a prettier way to do this!)
{
100,0,0 }
,
{
100,17,0 }
,
{
100,43,0 }
,
{
100,70,0 }
,
{
100,100,0 }
,
{
70,100,0 }
,
{
43,100,0 }
,
{
17,100,0 }
,
{
0,100,0 }
,
{
0,100,17 }
,
{
0,100,43 }
,
{
0,100,70 }
,
{
0,100,100 }
,
{
0,70,100 }
,
{
0,43,100 }
,
{
0,17,100 }
,
{
0,0,100 }
,
{
17,0,100 }
,
{
43,0,100 }
,
{
70,0,100 }
,
{
100,0,100 }
,
{
100,0,70 }
,
{
100,0,43 }
,
{
100,0,17 }
,
//Hue one, a lighter set starts at Array pointer 24
{
100,17,17 }
,
{
100,43,17 }
,
{
100,70,17 }
,
{
100,100,17 }
,
{
70,100,17 }
,
{
43,100,17 }
,
{
17,100,17 }
,
{
17,100,43 }
,
{
17,100,70 }
,
{
17,100,100 }
,
{
17,70,100 }
,
{
17,43,100 }
,
{
17,17,100 }
,
{
43,17,100 }
,
{
70,17,100 }
,
{
100,17,100 }
,
{
100,17,70 }
,
{
100,17,43 }
,
//Hue two, Lightest starts at Array pointer 42
{
100,43,43 }
,
{
100,70,43 }
,
{
100,100,43 }
,
{
70,100,43 }
,
{
43,100,43 }
,
{
43,100,70 }
,
{
43,100,100 }
,
{
43,70,100 }
,
{
43,43,100 }
,
{
70,43,100 }
,
{
100,43,100 }
,
{
100,43,70 }
,
};
byte rgbFactor = 100; // Value to factor by.. IE, 100 would provide 100 steps 255 is Max
byte randFlicker = 41; // Value to store a random value used to make the LEDs flicker
int fadeValue = 0; //tracks the current state of the fade value
boolean fadeDirection = false; // in this case, true means fade up, false means fade down
byte colorMode = 0; // Stores the current color
byte sndMode = 0; // Store the current sound mode
byte menuMode = 0; // Stores the current menuMode
byte impactRead = 0; // Place to store the last read of the impactPin
byte flashOn = 0; // Place to store the count for flashOn
//=================================================
// RGB Timing Variables
long rgbMillis = 0; // Store millis to keep track of the last time rgb was updated
int rgbInterval = 30; // How often to update the rgb values
//==========================================
void setup()
{
// Set button input pin
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH );
pinMode(impactPin, INPUT);
digitalWrite(impactPin, LOW);
// Set LED output pins
pinMode(redPin, OUTPUT);
digitalWrite(redPin, 0);
pinMode(grnPin, OUTPUT);
digitalWrite(grnPin, 0);
pinMode(bluPin, OUTPUT);
digitalWrite(bluPin, 0);
pinMode(flashPin, OUTPUT);
digitalWrite(flashPin, 0);
colorMode = EEPROM.read(0); // Read in the stored colorMode
sndMode = EEPROM.read(1); // Read in the stored sndMode
digitalWrite (sndSelectPin,sndMode);// Use the stored sndMode to set the sndSelectPin
}
void loop()
{
// Get button event and act accordingly
int b = checkButton();
if (b == 1) clickEvent();
if (b == 2) doubleClickEvent();
if (b == 3) holdEvent();
impactRead = digitalRead (impactPin); // Read the impact pin and dump it in to impactRead
if (fadeDirection == true && impactRead == HIGH && flashOn == 0){ // Three way check, see if the LED's are on if we have an impact and that we aren't already in impact mode//
flashOn = 10; // if all those conditions are true set flashOn to 10. flashOn * 30 MS = total flash time
}
// Main LED ramp/fade routine
if (millis () - rgbMillis >= rgbInterval){ // if it's been 30 ms since last time, lets update the LED
rgbMillis = millis (); // reset so we know the last time
if (fadeDirection == true){ // This is gonna happen if we are fading UP
if (fadeValue <=249) { // If fadeValue is less than or equalls 249
fadeValue = fadeValue +6; // Lets add 6 to it (This number effects how fast it ramps the LED UP)
}
else {
randFlicker = random(30);
fadeValue = 255 - randFlicker; // or else if it's already there, just make it 255 and flicker it there
}
}
if (fadeDirection == false){ //This is gonna happen if we are fading down
if (fadeValue >=7) { // if its over or = to 7
fadeValue = fadeValue - 7; // subtract 7 from it (This number effects how fast it ramps the LED DOWN)
}
else {
fadeValue = 0; // if it's less than 7 hold it at zero
}
}
if (flashOn >= 1){ // If we have a flashOn value lets do some flashing
analogWrite (flashPin, 255 -randFlicker * 8); // Write the flashPin high and 9X the flicker for crazy flicker madness
flashOn --; // Decrement by one so we only do this 10 times in a row
}
else{
analogWrite (flashPin, 0);
}
updateLED ();
}
}