bool fanControlEntered = 0 ; void setup () { Serial . begin (9600) ; // 122Hz @ 50% on pin 3 using T/C0 pinMode ( 3 , OUTPUT ) ; // 0C0B is connected to pin 3 TCCR0A = 0 << COM0A1 | 0 << COM0A0 | 1 << COM0B1 | 0 << COM0B0 | 1 << WGM01 | 1 << WGM00 ; TCCR0B = 1 << WGM02 | 1 << CS02 | 0 << CS01 | 1 << CS00 ; OCR0A = 127 ; OCR0B = 63 ; // 25kHz @ variable duty cycle on pins 9 and 10 using T/C1 pinMode ( 9 , OUTPUT ) ; // 0C1A is connected to pin 9 pinMode ( 10 , OUTPUT ) ; // 0C1B is connected to pin 10 TCCR1A = 1 << COM1A1 | 0 << COM1A0 | 1 << COM1B1 | 0 << COM1B0 | 0 << COM1C1 | 0 << COM1C0 | 1 << WGM11 | 0 << WGM10 ; TCCR1B = 1 << WGM13 | 1 << WGM12 | 0 << CS12 | 1 << CS11 | 0 << CS10 ; ICR1H = 0 ; ICR1L = 79 ; OCR1AH = 0 ; OCR1AL = 39 ; OCR1BH = 0 ; OCR1BL = 39 ; } void loop () { if ( Serial . available () ) { if (fanControlEntered) { int dutyCyclePercent = Serial . parseInt () ; int dutyCycle = map ( dutyCyclePercent , 0 , 100 , 0 , 79 ) ; OCR1AL = dutyCycle ; OCR1BL = dutyCycle ; Serial . read () ; // Consume newline Serial . print ("duty-cycle ") ; Serial . print (dutyCycle) ; Serial . print ('\n') ; } else { String input = Serial . readString () ; if ( input == "enter-fan-control" ) { fanControlEntered = 1 ; Serial . println ("success") ; } else { Serial . println ("error") ; } } } }