/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: Die Helligkeit einer LED wird durch ein Potentiometer mit Pulsweitenmodulation (PWM-Frequenz = ca. 2 kHz, Auflösung 10 Bit = 1024 Stufen) gesteuert. ************************************************************************************************** Version: 09.03.2022 ************************************************************************************************** Board: UNO ************************************************************************************************** C++ Arduino IDE V1.8.13 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json ************************************************************************************************* **************************************************************************************************/ #define potiPin 2 //Eingang Poti ist A2 int potiWert; bool pwmStatus = true; void setup() { Serial.begin(115200); //Löschen der Timer/Counter Control Register A und B TCCR1A = 0; TCCR1B = 0; //Modus Fast PWM-Mode 10 Bit einstellen TCCR1A |= (1 << WGM10) | (1 << WGM11); TCCR1B |= (1 << WGM12); //Vorteiler auf 8 setzen TCCR1B |= (1 << CS11); //Nichtinvertiertes PWM-Signal setzen TCCR1A |= (1 << COM1A1); //PWM-Pin 9 als Ausgang definieren DDRB |= (1 << DDB1); } void loop() { potiWert = analogRead(potiPin); //Potiwert einlesen (Auflösung Analogeingang = 10 Bit) //Ist die Auflösung des PWM-Signals z.B 8 Bit, muss der potiWert angepasst werden: //potiWert = map(potiWert, 0, 1023, 0, 255); if (potiWert == 0 && pwmStatus) { TCCR1A &= ~(1 << COM1A1); //COM1A1-Bit loeschen pwmStatus = false; } else if (potiWert > 0 && !pwmStatus) { TCCR1A |= (1 << COM1A1); //COM1A1-Bit setzen pwmStatus = true; } OCR1A = potiWert; // Setzen des Impuls-Pausenverhältnis Serial.println(potiWert); delay(15); }