Okay,
Here is the situation
I built a Lighting system for my motorcycle using RGB leds I purchased from Radio-shack. (276-0339). I am using an UNO to program the ATMEGA chip, from there I built a small board to house it on the motorcycle.
I used the sketch that Radio-shack had on the web site and spent three months or so modifying and tinkering with the sketch to get it to do what I wanted it to do. I am using a ntc 74hc14 to deal with switch denouncing. I have ground going through a button then through the 74hc14 to Pin 2 to change the "mode" the lights are in (Fade, flash, solid color, breathe). Additionally, I have ground through a button then to the 74hc14 to pin 3 on the arduino to change colors. The problem is when I am changing colors or modes when I have a fresh ATMEGA chip in the board it works fine, then after 5-25 changes the program freezes. If I kill power and restart, it works fine. The more I do this the worse it gets, until it gets to the point that I cant push either button or it will freeze. Trapping me in the color mode until I reprogram the ATMEGA. I can take the ATMEGA off the bike, put it in my UNO, reload the sketch, and it works fine. That is, until I change modes too many times.
Any ideas on why the ATMEGA chip is freezing?
The board on the bike
My Sketch (the colors have been left out to save space)
//
//
//Tyler_Bike_Lights****
//
//Written by Tyler Maggert
//
//
//
//
// *******************************************************************************/
#include <avr/pgmspace.h>
#include <EEPROM.h>
#define DATA_1 (PORTC |= 0X01) // DATA 1 // for UNO
#define DATA_0 (PORTC &= 0XFE) // DATA 0 // for UNO
#define STRIP_PINOUT (DDRC=0xFF) // for UNO
int buttonUp = 0; // pin 2 on uno
int buttonDown = 1; // pin 3 on uno
volatile int mode = (EEPROM.read(2)); // int for switching modes
volatile int p1 = (EEPROM.read(1)); // color int for mode 0
volatile int p2 = (EEPROM.read(3)); // color int for mode 2
volatile int p3 = (EEPROM.read(4)); // color int for mode 3
volatile int p5 = (EEPROM.read(5)); // color int for mode 1
//
//
COLOR INFO IS PROGRAMMED HERE
// ***********************************************************************************************************
// *
// * Power Up Init.
// *
// *
// ***********************************************************************************************************
void setup() {
STRIP_PINOUT; // set output pin - DEBUG: should auto detect which mother board for use
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
attachInterrupt(buttonDown, flash, RISING);
attachInterrupt(buttonUp, color, RISING);
}
//************************************************************************************************************
//
// settup interupt functions
// main changes patterns
// sub changes colors
//
//***********************************************************************************************************
void flash()
{
mode = (mode + 1);
}
void color()
{
if (mode == 0)
p1 = (p1 + 1);
else if (mode == 1)
p5 = (p5 + 1);
else if (mode == 2)
p2 = (p2 + 1);
else if (mode == 3)
p3 = (p3 +1);
}
// ***********************************************************************************************************
// *
// * Main Loop
// *
// *
// ***********************************************************************************************************
void loop()
{
EEPROM.write(1, p1);
EEPROM.write(2, mode);
EEPROM.write(3, p2);
EEPROM.write(4, p3);
EEPROM.write(5, p5);
if (mode == 0) // fade patterns
{
if (p1 == 0)
pattern(fade, 105, 50);
else if (p1 == 1)
pattern(fade, 105, 1000);
else if (p1 == 2)
{
pattern(solid_blue, 5, 200);
pattern(fade_blue_red, 21, 200);
pattern(solid_red, 5, 100);
pattern(fade_red_green, 21, 200);
pattern(solid_green, 5, 200);
pattern(fade_green_blue, 21, 200);
}
else if (p1 >= 3)
p1 = 0;
}
else if (mode == 1) // flash patterns
{
if (p5 == 0)
pattern(lightning, 10, 100);
else if (p5 == 1)
pattern(flash_orange_green, 4, 250);
else if (p5 == 2)
pattern(colorful_flash, 20, 250);
else if (p5 >= 3)
p5 = 0;
}
else if (mode == 2) // solid colors
{
if (p2 == 0)
pattern(solid_green, 1, 100);
else if (p2 == 1)
pattern(solid_blue, 1, 100);
else if (p2 == 2)
pattern(solid_red, 1, 100);
else if (p2 == 3)
pattern(solid_white, 1, 100);
else if (p2 == 4)
pattern(solid_purple, 1, 100);
else if (p2 == 5)
pattern(solid_yellow, 1, 100);
else if (p2 == 6)
pattern(solid_teal, 1, 100);
else if (p2 == 7)
pattern(solid_orange, 1, 100);
else if (p2 == 8)
pattern(solid_bluegreen, 1, 100);
else if (p2 >= 9)
p2 = 0;
}
else if (mode == 3) // breathe patterns
{
if (p3 == 0)
pattern(breathe_blue, 30, 80);
else if (p3 == 1)
pattern(breathe_green, 30, 80);
else if (p3 == 2)
pattern(breathe_red, 30, 80);
else if (p3 == 3)
pattern(breathe_white, 30, 80);
else if (p3 == 4)
{
pattern(breathe_red, 30, 100);
pattern(off, 1, 80);
pattern(breathe_white, 30, 100);
pattern(off, 1, 80);
pattern(breathe_blue, 30, 100);
pattern(off, 1, 80);
}
else if (p3 >= 5)
p3 = 0;
}
else if (mode >= 4) // reset mode
mode = 0;
}
/*******************************************************************************
*******************************************************************************/
void pattern(const unsigned long data[][20], int pattern_no, int frame_rate)
{
int i=0; //pattern number?
int j=0; //frame rate
uint32_t temp_data;
for (i=0;i<pattern_no;i++)
{
int m = (EEPROM.read(2));
int b = (EEPROM.read(1));
int c = (EEPROM.read(3));
int d = (EEPROM.read(4));
int e = (EEPROM.read(5));
if (m == mode && c == p2 && b == p1 && d == p3 && e == p5)
{
noInterrupts();
for (j=0;j<20;j++)
{
temp_data=pgm_read_dword_near(&data*[j]);*
-
send_strip(temp_data);*
-
}*
-
interrupts();*
-
delay(frame_rate);*
-
}*
-
else*
-
{*
-
break;*
-
}*
-
}*
}
/*******************************************************************************
* Function Name : send_strip
* Description : Transmit 24 pulse to LED strip
_* _
* Input : 24-bit data for the strip
_* _
* Output : None
* Return : None
*******************************************************************************/
void send_strip(uint32_t data)
{ -
int i;*
-
unsigned long j=0x800000;*
-
for (i=0;i<24;i++)*
-
{*
-
if (data & j)*
-
{*
-
DATA_1;*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t"); *
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
DATA_0;*
-
}*
-
else*
-
{*
-
DATA_1;*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t");*
-
asm("nop\n\t"); *
-
DATA_0;*
-
}*
-
j>>=1;*
-
}*
}
/*******************************************************************************
* Function Name : reset_strip
* Description : Send reset pulse to reset all color of the strip
_* _
* Input : None
_* _
* Output : None
* Return : None
*******************************************************************************/
void reset_strip()
{
- DATA_0;*
- delayMicroseconds(20);*
}