//#include <TimerOne.h>
#include <IRremote.hpp>
// This example creates a PWM signal with 25 kHz carrier.
//
// Arduino's analogWrite() gives you PWM output, but no control over the
// carrier frequency. The default frequency is low, typically 490 or
// 3920 Hz. Sometimes you may need a faster carrier frequency.
//
// The specification for 4-wire PWM fans recommends a 25 kHz frequency
// and allows 21 to 28 kHz. The default from analogWrite() might work
// with some fans, but to follow the specification we need 25 kHz.
//
// http://www.formfactors.org/developer/specs/REV1_2_Public.pdf
//
// Connect the PWM pin to the fan's control wire (usually blue). The
// board's ground must be connected to the fan's ground, and the fan
// needs +12 volt power from the computer or a separate power supply.
#define PWM_FREQ 40 // 1/25000Hz = 40us
#define IR_RECEIVE_PIN 2 // Signal Pin of IR receiver
const int PIN_PWM_FAN = 9;
void setup(void) {
IrReceiver.begin(IR_RECEIVE_PIN);
//timer1Init();
//Timer1.initialize(PWM_FREQ); // 40 us = 25 kHz
Serial.begin(115200);
uint16_t c = (F_CPU/100000 * PWM_FREQ) / 20;
Serial.println(c);
//Serial.println(TIMER1_A_PIN);
}
void timer1Init() {
TCCR1B = _BV(WGM13); // set mode as phase and frequency correct pwm, stop the timer
TCCR1A = 0; // clear control register A
ICR1 = (F_CPU/100000 * PWM_FREQ) / 20;
TCCR1B = _BV(WGM13) | _BV(CS10);
pinMode(PIN_PWM_FAN, OUTPUT); // set pin 9 as output
TCCR1A |= _BV(COM1A1);
}
void setDutyPercent(uint8_t dc) {
uint32_t dutyCycle = ICR1 * ((dc / 100.0) * 1023);
//dutyCycle *= dc;
OCR1A = dutyCycle >> 10;
//Timer1.pwm(PIN_PWM_FAN, (dc / 100.0) * 1023);
}
void loop(void) {
uint8_t command = 0;
// Checks received an IR signal
if (IrReceiver.decode()) {
command = IrReceiver.decodedIRData.command;
//translateIR();
Serial.println(command);
IrReceiver.resume(); // Receive the next value
}
// slowly increase the PWM fan speed
//
/*for (uint8_t dutyCycle = 30; dutyCycle < 100; dutyCycle++) {
Serial.print("PWM Fan, Duty Cycle = ");
Serial.println(dutyCycle);
setDutyPercent(dutyCycle);
delay(100);
}*/
}