erst bitte mal selber probieren mit dem BlinkWithoutDelay Beispiel.
...
...
...
Dann habe ich mal mein BlinkBeispiel erweitert um dimmbare LEDs.
Ein neue Klasse DimLed
dazu zwei eigenen Objekte an Pin 10 und 11.
DimLed yourObjectName(Pin, Lowest_Dimmer_Value, Highest_Dimmer_Value, Intervall_Between_Steps_in_milliseconds)
Zwei Schalter mit internem Pullup auf A5 bzw A4.
Diese sind wegen Pullup gegen GND zu schalten.
PIN13 (die eingebaute LED) ... blink halt so einsam vorsich hin.
/* Blink a LED in a specific rhythm
Turns on and off light emitting diodes (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
*/
class DimLed {
byte state = 1; // 0 off, 1 up, 2 hold, 3 down
unsigned long previousMillis; // last blink timestamp
uint16_t lowest = 00; // minimum brightness of PWM LED
uint16_t highest = 255; // maximum brightness of PWM LED
const byte ledPin; // a GPIO for the LED, on UNO only PIN 3 5 6 9 10 11 are PWM
uint16_t intervall = 10; // how fast should the led be dimmed (=milliseconds between steps)
byte actual = 0; // actual PWM
public:
DimLed(byte attachTo, uint16_t _lowest = 0, uint16_t _highest = 255, uint16_t _intervall = 100):
ledPin(attachTo),
lowest (_lowest),
highest (_highest),
intervall (_intervall)
{}
void setup() {
pinMode(ledPin, OUTPUT);
}
void set(uint16_t _lowest, uint16_t _highest) { // modify on/off times during runtime
lowest = _lowest;
highest = _highest;
}
void setState(byte _state) { // set state externally
state = _state;
}
byte getState() { //returns the actual state of the LED
return state;
}
void loop() {
if (state == 1) {
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= intervall) {
// save the last time you blinked the LED
previousMillis = currentMillis;
actual++;
if (actual >= highest) state = 2; // higher end reached. Switch to hold state
analogWrite(ledPin, actual);
//Serial.print(F("D46 actual=");Serial.print(actual); Serial.print(F(" state=")); Serial.println(state);
}
}
if (state == 3) {
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= intervall) {
// save the last time you blinked the LED
previousMillis = currentMillis;
actual--;
if (actual <= lowest) state = 0; // lower end reached. Switch to Idle state
analogWrite(ledPin, actual);
//Serial.print(F("D57 actual=");Serial.print(actual); Serial.print(F(" state=")); Serial.println(state);
}
}
}
};
class BlinkLed {
byte state = 1; // 0 off, 1 blink
unsigned long previousMillis; // last blink timestamp
uint16_t on = 180;
uint16_t off = 320; // 180/320 is according ECE
const byte ledPin; // a GPIO for the LED
public:
BlinkLed(byte attachTo, uint16_t _on = 180, uint16_t _off = 320):
ledPin(attachTo),
on (_on),
off (_off)
{}
void setup() {
pinMode(ledPin, OUTPUT);
}
void set(uint16_t _on, uint16_t _off) { // modify on/off times during runtime
on = _on;
off = _off;
}
void setState(byte _state) { // 1 Switch on blinking; 0 switch off blinking
state = _state;
}
void loop() {
if (state) {
uint32_t currentMillis = millis();
if (digitalRead(ledPin) && currentMillis - previousMillis >= on) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(ledPin, LOW);
}
else if (!digitalRead(ledPin) && currentMillis - previousMillis >= off) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(ledPin, HIGH);
}
}
}
};
// declare your objects (assign GPIOs to you LEDs)
//DimLed yourName(Pin, Lowest_Dimmer_Value, Highest_Dimmer_Value, Intervall_Between_Steps_in_milliseconds)
DimLed myDimLedOne(9, 0, 255, 200);
DimLed myDimLedTwo(10, 20, 255, 500); // use only Pin 3 5 6 9 10 11 on Arduino UNO for PWM!!!
BlinkLed myBlinkLed(13);
//BlinkLed syncronBlinkLed(9, 500, 500); // a simple blink LED on a GPIO with 500ms on, 500ms off
//RhythmLed topRKL(13); // a rhythm blink LED on a GPIO with 4 different on/off times
//RhythmLed outOffSyncRKL(11); // eine Rundumkennleuchte die wegen eines "Massefehlers" am Originalfahrzeugs eine leicht verschobene Blinkfrequenz hat ;-)
// Let's declare some SwitchButtons.
const int SwitchOne_Pin = A5;
const int SwitchTwo_Pin = A4;
void setup() {
// each LED object has to be called once in the setup
myDimLedOne.setup();
myDimLedTwo.setup();
myBlinkLed.setup();
//syncronBlinkLed.setup();
//topRKL.setup();
//outOffSyncRKL.setup();
// // Some example methods you can use in setup() or loop():
//outOffSyncRKL.setIntervall(250, 90, 30, 400); // modify the intervalls for this LED
// syncronBlinkLed.setState(0); // switch off a LED = stopp blinking
// syncronBlinkLed.setState(1); // switch on a LED = start blinking
// we use the internal PULLUP, to close the Switch - connect to GND
pinMode(SwitchOne_Pin, INPUT_PULLUP);
pinMode(SwitchTwo_Pin, INPUT_PULLUP);
Serial.begin(115200); //only used for debug
}
void loop() {
// each LED object has to be called once in the loop
myDimLedOne.loop();
myDimLedTwo.loop();
myBlinkLed.loop();
//syncronBlinkLed.loop();
//topRKL.loop();
//outOffSyncRKL.loop();
// check the switches and act, if we are in the right state
// we used INPUT_PULLUP - so closing a switch to GND give 0/ZERO
byte currentState=myDimLedOne.getState();
if (digitalRead(SwitchOne_Pin) == 0 && (currentState == 0 || currentState == 3)) myDimLedOne.setState(1); // Switch is ON
if (digitalRead(SwitchOne_Pin) == 1 && (currentState == 1 || currentState == 2)) myDimLedOne.setState(3); // Switch is OFF
currentState=myDimLedTwo.getState();
if (digitalRead(SwitchTwo_Pin) == 0 && (currentState == 0 || currentState == 3)) myDimLedTwo.setState(1); // Switch is ON
if (digitalRead(SwitchTwo_Pin) == 1 && (currentState == 1 || currentState == 2)) myDimLedTwo.setState(3); // Switch is OFF
//
// put here other code which needs to run:
}
edit: sketch gekürzt wegen 9000 Zeichen limit ... RhythmLed hat drann glauben müssen - ich hoffe es passt noch.