hallo Leute, bin neu in den Thema Arduino programmieren!
Mein vorhaben ist, mit 3 Tastern eine LED ein/aus zuschalten, und zu dimmen.
Also,
Taster1--> LED ein auf 5%
Taster2--> LED runter dimmen
Taster3--> LED hoch dimmen
aber wenn Taster1 nicht gedrückt wurde, also aus ist, dan darf auch nicht gedimmt werden.
ja ich weiß, die Programmierung ist nicht die Beste,.....aber naja
int brightness = 5;
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(9,OUTPUT); // Set pin 9 to be in output mode
pinMode(6,OUTPUT); // Set pin 6 to be in output mode
pinMode(2,INPUT); // Set pin 7 to be in input mode EIN/AUS
pinMode(3,INPUT); // Set pin 3 to be in input mode REDUCE Brightness
pinMode(4,INPUT); // Set pin 8 to be in input mode INCREASE Brightness
digitalWrite(2, lastButtonState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(2);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(2, lastButtonState);
if(lastButtonState && digitalRead(3) == HIGH) brightness--; // If button on pin 3 is pressed, reduce brightness
if (brightness < 5) brightness = 5; // Don't let brightness drop below 10
else if(lastButtonState && digitalRead(4) == HIGH) brightness++; // If button on pin 4 is pressed, increase brightness
brightness = constrain(brightness,5,255); // Don't let brightness get above 255
analogWrite(9, brightness); // Set pin 9 to the new brightness level
analogWrite(6, brightness); // Set pin 6 to the new brightness level
delay(20);
}