25. September 2012
Eine LED Anzeige mit 6 LEDs, die mit 2 Buttons gesteuert werden kann. Durch drücken beider Buttons, wird der Modus zwischen Aufbau/Helligkeit gewechselt. Der aktuelle Modus wird durch die 2 Status LED's angezeigt. Wird die Klasse "debugfunction" im Loop aufgerufen, werden die Werte über die Schnittstelle ausgegeben. Code:
#define BUTTON_LEFT 4
#define BUTTON_RIGHT 7
#define PWM1 11
#define PWM2 10
#define PWM3 9
#define PWM4 6
#define PWM5 5
#define PWM6 3
#define MODELIGHT 12
#define MODELIGHT2 2
int ledcount = 1;
int brightness = 250;
int buttonleft;
int buttonright;
int alreadypressed;
boolean brightnessmode = false;
boolean buttonpressed = false;
boolean buttonpressedfirstright = true;
boolean buttonpressedfirstleft = true;
void setup()
{
pinMode(BUTTON_LEFT, INPUT);
pinMode(BUTTON_RIGHT, INPUT);
pinMode(PWM1, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(PWM4, OUTPUT);
pinMode(PWM5, OUTPUT);
pinMode(PWM6, OUTPUT);
pinMode(MODELIGHT, OUTPUT);
pinMode(MODELIGHT2,OUTPUT);
Serial.begin(9600);
}
void loop(){
buttonleft = digitalRead(BUTTON_LEFT);
buttonright = digitalRead(BUTTON_RIGHT);
ledcounter(ledcount);
buttonstates();
if (brightnessmode == true){
digitalWrite(MODELIGHT, HIGH);
digitalWrite(MODELIGHT2, LOW);
}
else{
digitalWrite(MODELIGHT, LOW);
digitalWrite(MODELIGHT2, HIGH);
}
debugfunction();
}
//#################################BUTTONSTATES#################################
void buttonstates(){
if(buttonleft == 0 && buttonright == 1 && ledcount<6 && brightnessmode == false){
ledcount=ledcount++;
if (alreadypressed == true){
delay(50);}
else{
delay(200);
alreadypressed = true;
}
}
if(buttonright == 0 && buttonleft == 1 && ledcount>0 && brightnessmode == false){
ledcount=ledcount--;
if (alreadypressed == true){
delay(50);}
else{
delay(200);
alreadypressed = true;
}
}
if(buttonright == 0 && buttonleft == 0){
alreadypressed = false;
}
if(buttonleft == 0 && buttonright == 1 && brightness<=245 && brightnessmode == true){
brightness=brightness+10;
}
if(buttonright == 0 && buttonleft == 1 && brightness>10 && brightnessmode == true){
brightness=brightness-10;
}
if(buttonright == 1 && buttonleft == 1 && buttonpressed == false){
buttonpressed = true;
brightnessmode = !brightnessmode;
delay(100);
}
else if (buttonright != 1 && buttonleft != 1){
buttonpressed = false;
}
}
//#################################LEDCOUNTER#################################
void ledcounter(int ledcounting){
if (ledcounting == 6){
analogWrite(PWM1, brightness);
analogWrite(PWM2, brightness);
analogWrite(PWM3, brightness);
analogWrite(PWM4, brightness);
analogWrite(PWM5, brightness);
analogWrite(PWM6, brightness);
}
else if (ledcounting == 5){
analogWrite(PWM1, brightness);
analogWrite(PWM2, brightness);
analogWrite(PWM3, brightness);
analogWrite(PWM4, brightness);
analogWrite(PWM5, brightness);
analogWrite(PWM6, 0);
}
else if (ledcounting == 4){
analogWrite(PWM1, brightness);
analogWrite(PWM2, brightness);
analogWrite(PWM3, brightness);
analogWrite(PWM4, brightness);
analogWrite(PWM5, 0);
analogWrite(PWM6, 0);
}
else if (ledcounting == 3){
analogWrite(PWM1, brightness);
analogWrite(PWM2, brightness);
analogWrite(PWM3, brightness);
analogWrite(PWM4, 0);
analogWrite(PWM5, 0);
analogWrite(PWM6, 0);
}
else if (ledcounting == 2){
analogWrite(PWM1, brightness);
analogWrite(PWM2, brightness);
analogWrite(PWM3, 0);
analogWrite(PWM4, 0);
analogWrite(PWM5, 0);
analogWrite(PWM6, 0);
}
else if (ledcounting == 1){
analogWrite(PWM1, brightness);
analogWrite(PWM2, 0);
analogWrite(PWM3, 0);
analogWrite(PWM4, 0);
analogWrite(PWM5, 0);
analogWrite(PWM6, 0);
}
}
//#################################DEBUG#################################
void debugfunction(){
Serial.print("Left: ");
Serial.print(buttonleft);
Serial.print(" Right: ");
Serial.print(buttonright);
Serial.print(" LEDCount: ");
Serial.print(ledcount);
Serial.print(" Brightnessmode: ");
Serial.print(brightnessmode);
Serial.print(" Brightness: ");
Serial.println(brightness);
}