#include <avr/io.h>
#include <avr/interrupt.h>
// Define pin numbers
#define POT_PIN 2 // Analog input pin for potentiometer
#define PWM_PIN 1 // PWM output pin
void setup() {
// Set PWM pin as output
pinMode(PWM_PIN, OUTPUT);
// Enable ADC and set prescaler to 64 (125kHz)
ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1);
// Set ADC reference voltage to AVCC
ADMUX |= (1 << REFS0);
// Enable global interrupts
sei();
// Start ADC conversion
ADCSRA |= (1 << ADSC);
}
void loop() {
// No need for anything in the loop, the PWM value is updated by interrupts
}
// ADC conversion complete interrupt
ISR(ADC_vect) {
// Read ADC value
uint16_t adcValue = ADC;
// Map ADC value (0-1023) to PWM range (0-255)
uint8_t pwmValue = map(adcValue, 0, 1023, 0, 255);
// Set PWM duty cycle
analogWrite(PWM_PIN, pwmValue);
// Start next ADC conversion
ADCSRA |= (1 << ADSC);
}