Created
September 29, 2012 07:42
-
-
Save theterg/3803443 to your computer and use it in GitHub Desktop.
LED VU BT firmware with animation upgrades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUFFLEN 200 | |
#define NUMLEDS 5 | |
#define MAXVALUES 3 | |
#define ACT_FADEUP 1 | |
#define ACT_FADEDOWN 2 | |
#define ACT_BLINK 3 | |
int leds[] = {3, 5, 6, 9, 10}; | |
int act[NUMLEDS]; | |
unsigned char vals[NUMLEDS]; | |
unsigned char act_dest[NUMLEDS]; | |
unsigned long act_start[NUMLEDS]; | |
unsigned long act_dur[NUMLEDS]; | |
unsigned long period = 800; | |
char line[BUFFLEN]; | |
int chan; | |
long values[MAXVALUES]; | |
int battPin = A0; | |
char animate = 0; | |
unsigned long lastanimation = 0; | |
int animation = 0; | |
int animationrate = 0; | |
int idx = 0; | |
int dir = -1; | |
void setup(){ | |
memset(vals, 0, NUMLEDS); | |
memset(act, 0, NUMLEDS); | |
memset(act_dur, 0, NUMLEDS); | |
memset(act_dest, 0, NUMLEDS); | |
memset(act_start, 0, NUMLEDS); | |
Serial.begin(57600); | |
for (int i=0;i<NUMLEDS;i++){ | |
digitalWrite(leds[i],HIGH); | |
pinMode(leds[i], OUTPUT); | |
} | |
Serial.println("START"); | |
} | |
void parseLine(){ | |
char inputchan = line[1]; | |
chan = atoi(&inputchan); | |
int idx = 2; | |
int val = 0; | |
int valpos = 0; | |
char valbuff[BUFFLEN]; | |
char c = '\0'; | |
while ((idx < BUFFLEN)&&(val < MAXVALUES)){ | |
c = line[idx++]; | |
if (c == '\r') | |
continue; | |
if ((c == ',')||(c == '\0')) { | |
values[val++] = atol(valbuff); | |
memset(valbuff, '\0', BUFFLEN); | |
valpos = 0; | |
if (c == '\0') | |
break; | |
} else { | |
valbuff[valpos++] = c; | |
} | |
} | |
} | |
void loop(){ | |
if (Serial.available() > 0){ | |
readline(0); | |
parseLine(); | |
if ((chan < 0)||(chan > NUMLEDS-1)){ | |
Serial.println("ERRCH"); | |
return; | |
} | |
int batVal = 0; | |
switch(line[0]){ | |
case 'S': | |
case 's': | |
Serial.println("OK"); | |
setLed(chan,values[0]); | |
vals[chan] = values[0]; | |
act[chan] = 0; | |
break; | |
case 'F': | |
case 'f': | |
Serial.println("OK"); | |
act_dur[chan] = values[0]; | |
act_dest[chan] = (char)values[1]; | |
if (act_dest[chan] < vals[chan]) | |
act[chan] = ACT_FADEDOWN; | |
else if (act_dest[chan] > vals[chan]) | |
act[chan] = ACT_FADEUP; | |
act_start[chan] = millis(); | |
break; | |
case 'C': | |
case 'c': | |
Serial.println("OK"); | |
for (int i=0;i<5;i++){ | |
setLed(i,0); | |
vals[i] = 0; | |
act[i] = 0; | |
} | |
animate = 0; | |
break; | |
case 'B': | |
case 'b': | |
batVal = analogRead(battPin); | |
//full = 665 | |
//UVLO = 442 | |
Serial.print("VAL:"); | |
//Serial.println((int)ceil((((float)(batVal-422))/243.0)*100.0)); | |
//Serial.println((float)batVal/158.7); | |
Serial.println(batVal); | |
break; | |
case 'P': | |
case 'p': | |
Serial.println("ACK"); | |
break; | |
case 'A': | |
case 'a': | |
Serial.println("OK"); | |
if (animate) | |
animate = 0; | |
else | |
animate = 1; | |
animation = chan; | |
if (values[0] == 0){ | |
animationrate = 300; | |
} else { | |
animationrate = values[0]; | |
} | |
dir = -1; chan = 0; idx = 0; | |
break; | |
default: | |
Serial.print("ERRCO"); | |
Serial.println(line[0],HEX); | |
} | |
} | |
if (animate){ | |
doAnimate(); | |
} | |
doBlink(); | |
} | |
void fade(unsigned long dur, char dest){ | |
act_dur[chan] = dur; | |
act_dest[chan] = dest; | |
if (act_dest[chan] < vals[chan]) | |
act[chan] = ACT_FADEDOWN; | |
else if (act_dest[chan] > vals[chan]) | |
act[chan] = ACT_FADEUP; | |
act_start[chan] = millis(); | |
} | |
void doAnimate(){ | |
if (animation == 0) { | |
if (millis()-lastanimation > animationrate+20){ | |
if (idx == 0){ | |
chan = 5; | |
fade(animationrate, 0); | |
chan = 0; | |
fade(animationrate, 255); | |
} else { | |
chan = idx-1; | |
fade(animationrate,0); | |
chan = idx; | |
fade(animationrate,255); | |
} | |
if (idx >= 5) | |
idx = 0; | |
else | |
idx++; | |
lastanimation = millis(); | |
} | |
} else if (animation == 1) { | |
if (millis()-lastanimation > animationrate+20){ | |
chan = idx-dir; | |
fade(animationrate,0); | |
chan = idx; | |
fade(animationrate,255); | |
if (idx == 0) { | |
dir = 1; | |
} else if (idx == 4){ | |
dir = -1; | |
} | |
idx = idx + dir; | |
lastanimation = millis(); | |
} | |
} | |
} | |
void doBlink(){ | |
for (int i=0;i<NUMLEDS;i++){ | |
if (act[i] != 0){ //First check if there is anything to do at all | |
switch (act[i]){ | |
case ACT_FADEUP: | |
if (millis() > act_start[i]+act_dur[i]){ | |
act[i] = 0; | |
setLed(i, act_dest[i]); | |
vals[i] = act_dest[i]; | |
break; | |
} | |
setLed(i,((((millis()-act_start[i])*(act_dest[i]-vals[i])))/act_dur[i])+vals[i]); | |
break; | |
case ACT_FADEDOWN: | |
if (millis() > act_start[i]+act_dur[i]){ | |
act[i] = 0; | |
setLed(i, act_dest[i]); | |
vals[i] = act_dest[i]; | |
break; | |
} | |
setLed(i,((((act_start[i]+act_dur[i])-millis())*(vals[i]-act_dest[i]))/act_dur[i])+act_dest[i]); | |
break; | |
case ACT_BLINK: | |
break; | |
} | |
} | |
} | |
} | |
void setLed(int chan, int value){ | |
//int val = 1/(1+exp(((value/21)-6)*-1))*255; | |
int val = value; | |
analogWrite(leds[chan],255-val); | |
} | |
void readline(char echo){ | |
int i=0; char c = 0x0; | |
memset(line,'\0',BUFFLEN); | |
while((i < BUFFLEN-1)){ | |
while (Serial.available() < 1) {} //Block until character is available | |
c = Serial.read(); | |
if (echo) | |
Serial.print(c); | |
//if (c == '\r') | |
// continue; | |
//if (c == '\n') | |
if ((c == '\n')||(c == '\r')||(c == 'M')) | |
break; | |
line[i++] = c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment