Heardbead mit PWM.h

Heardbead mit PWM.h

Erzeugen eines LED Heardbeat mit Hilfe der Bibliothek PWM.h

/* Headrbead with PWM.h Created: 09.06.2017 Author: Ralf Bohnen - www.Arduinoclub.de This example code is in the public domain. */ #include <PWM.h> //UNO PWM PINS : 3, 5, 6, 9, 10, 11 //Read more for other boards //https://www.arduino.cc/en/Main/Products int frequencyOutputPin = 9; //Heardbead Timing const long settingStepMicrosec = 18000; //<-- play with it volatile unsigned long nextSetPulseWidthMicrosec; volatile int actPulseWidthPercent; volatile bool goToZero = false; void setup() { //Start Serial I/O Serial.begin(115200); delay(500); Serial.println("\nHeadrbead with PWM.h"); Serial.println(F("-------------------------------------------")); //Init Timers InitTimersSafe(); //Set Frequency to 10kHz SetPinFrequency(frequencyOutputPin, 50); //Set Width + to 0% LED off actPulseWidthPercent = 0; pwmWrite(frequencyOutputPin, actPulseWidthPercent * 2.55f); } //end setup void setLEDBrightness() { if (actPulseWidthPercent == 100 && !goToZero) { goToZero = true; } else { if (actPulseWidthPercent == 0 && goToZero) { goToZero = false; } } if (goToZero) { actPulseWidthPercent--; } else { actPulseWidthPercent++; } //Set Pulsewidth+ pwmWrite(frequencyOutputPin, actPulseWidthPercent * 2.55f); nextSetPulseWidthMicrosec = micros() + settingStepMicrosec; } void loop() { if (micros() > nextSetPulseWidthMicrosec) { setLEDBrightness(); } //your Code .... > } //end loop

Das könnte dich auch interessieren …