Created
August 31, 2011 19:11
-
-
Save thiagoalz/1184430 to your computer and use it in GitHub Desktop.
Código inicial do SK8Led do Tony :)
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
/* | |
SK8Led | |
*/ | |
//pins | |
int pins[] = {2,3,4}; | |
//{red, green, blue} | |
int configPins[] = {8,9,11,12}; | |
//{varUpPin, varDownPin, valueUpPin, valueDownPin} | |
//initial values | |
int varAtual=0; | |
int state[] = {LOW,LOW,LOW}; | |
int delayOn[] = {100,100,100}; | |
int delayOff[] = {100,100,100}; | |
//Last time turned on or of | |
long lastOn[] = {0,0,0}; | |
long lastOff[] = {0,0,0}; | |
void setup() { | |
//Seting color pins | |
pinMode(pins[0], OUTPUT); | |
pinMode(pins[1], OUTPUT); | |
pinMode(pins[2], OUTPUT); | |
//Seting button pins | |
for(int i=0; i<4; i++){ | |
pinMode(configPins[i], INPUT); | |
digitalWrite(configPins[i], HIGH); | |
} | |
} | |
void loop() { | |
//Check if any pin has to be turned on | |
for(int i=0; i<3;i++){ | |
checkOn(i); | |
} | |
//Check if any pin has to be turned off | |
for(int i=0; i<3;i++){ | |
checkOff(i); | |
} | |
//Check if any config button was pressed | |
checkButtons(); | |
} | |
//Check if any pin has to be turned on | |
void checkOn(int collor){ | |
if(state[collor]==LOW){ | |
long time=millis(); | |
if( (time-lastOff[collor]) >= delayOff[collor]){ | |
digitalWrite(pins[collor], HIGH); | |
lastOn[collor]=time; | |
state[collor]=HIGH; | |
} | |
} | |
} | |
//Check if any pin has to be turned off | |
void checkOff(int collor){ | |
if(state[collor]==HIGH){ | |
long time=millis(); | |
if( (time-lastOn[collor]) >= delayOn[collor]){ | |
digitalWrite(pins[collor], LOW); | |
lastOff[collor]=time; | |
state[collor]=LOW; | |
} | |
} | |
} | |
//Check if any config button was pressed | |
void checkButtons(){ | |
if(readButton(0)){//VarUp | |
varAtual = (varAtual+1)%6; | |
showVarAtual(); | |
} | |
if(readButton(1)){//VarDown | |
varAtual = (varAtual+5)%6; | |
showVarAtual(); | |
} | |
if(readButton(2)){//ValueUp | |
changeVarAtual(1); | |
} | |
if(readButton(3)){//ValueDown | |
changeVarAtual(-1); | |
} | |
} | |
boolean readButton(int pos){ | |
return !digitalRead(configPins[pos]); | |
} | |
void changeVarAtual(int quant){ | |
if(varAtual<3){ | |
delayOn[varAtual] = (delayOn[varAtual]+quant) > 0? (delayOn[varAtual]+quant) : 0; | |
}else{ | |
delayOff[varAtual-3] = (delayOff[varAtual-3]+quant) > 0? (delayOff[varAtual-3]+quant): 0; | |
} | |
delay(10); | |
} | |
void showVarAtual(){ | |
//Turn off all | |
for(int i=0; i<3; i++){ | |
digitalWrite(pins[i], LOW); | |
} | |
if(varAtual<3){ | |
digitalWrite(pins[varAtual],LOW); | |
delay(250); | |
digitalWrite(pins[varAtual],HIGH); | |
delay(500); | |
digitalWrite(pins[varAtual],LOW); | |
delay(250); | |
}else{ | |
digitalWrite(pins[varAtual-3],LOW); | |
delay(250); | |
digitalWrite(pins[varAtual-3],HIGH); | |
delay(150); | |
digitalWrite(pins[varAtual-3],LOW); | |
delay(50); | |
digitalWrite(pins[varAtual-3],HIGH); | |
delay(150); | |
digitalWrite(pins[varAtual-3],LOW); | |
delay(250); | |
} | |
//Back to last state | |
for(int i=0; i<3; i++){ | |
digitalWrite(pins[i], state[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment