// FanSpeed example from https://github.com/PaulStoffregen/TimerOne/blob/master/examples/FanSpeed/FanSpeed.ino
//
#include <TimerOne.h>
// 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.
const int fanPin = 9;
const int dataPin = 5;
void setup(void)
{
Timer1.initialize(250); // 40 us = 25 kHz
Serial.begin(115200);
pinMode(2,OUTPUT);
}
void loop(void)
{
// slowly increase the PWM fan speed
//
if (1) {
for (float dutyCycle = 10.0; dutyCycle < 100.0; dutyCycle+=80) {
// Serial.print("PWM Fan, Duty Cycle = ");
// Serial.println(dutyCycle);
Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
digitalWrite(2,!digitalRead(2));
delay(10);
}
}
//dutyOut(9,4,LSBFIRST,0xBB);
delayMicroseconds(200);
}
void dutyOut(uint8_t dataPin, uint8_t ledPin, uint8_t bitOrder, uint8_t val)
{
uint8_t i;
digitalWrite(2,HIGH);
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST) {
digitalWrite(ledPin, val & 1);
Timer1.pwm(dataPin,10 * (val & 1));
val >>= 1;
} else {
digitalWrite(ledPin, (val & 128) != 0);
Timer1.pwm(dataPin,1000 * ((val & 128) != 0));
val <<= 1;
}
delayMicroseconds(1);
}
digitalWrite(2,LOW);
}