Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Pin 2 to PC fan "Tacho" line (pin 3 on standard connectors) with 10k pullup to 5v.
- //pin 11 to PC fan PWM/Input line (pin 4 on standard connectors, absent on 3 pin fans).
- unsigned int counter;
- unsigned int lastCounter;
- unsigned long timeToReset;
- void setup() {
- counter = 0;
- lastCounter = 0;
- // put your setup code here, to run once:
- pinMode(2, INPUT);
- attachInterrupt(0, rpm, RISING);
- pinMode(13, OUTPUT);
- //attachInterrupt(0, toggleLED, RISING);
- pinMode(11, OUTPUT);
- setPwmFrequency(11, 1);
- Serial.begin(9600);
- timeToReset = 1000;
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if (millis() > timeToReset) {
- timeToReset = millis() + 1000;
- lastCounter = counter;
- counter = 0;
- Serial.print("RPM: ");Serial.println(lastCounter * 60);
- }
- if (Serial.available()) {
- char input = Serial.read();
- switch (input) {
- case '0':
- analogWrite(11, 0);
- Serial.println("11, 0");
- break;
- case '5':
- analogWrite(11, 128);
- //Serial.println("11, 128");
- break;
- case '8':
- analogWrite(11, 204);
- //Serial.println("11, 204");
- break;
- case '9':
- analogWrite(11, 255);
- //Serial.println("11, 255");
- break;
- case 'f':
- //Serial.print("RPM: ");Serial.println(lastCounter);
- default:
- break;
- }
- }
- }
- void toggleLED() {
- digitalWrite(13, !digitalRead(13));
- }
- void rpm() {
- counter++;
- }
- void setPwmFrequency(int pin, int divisor) {
- byte mode;
- if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
- switch(divisor) {
- case 1: mode = 0x01; break;
- case 8: mode = 0x02; break;
- case 64: mode = 0x03; break;
- case 256: mode = 0x04; break;
- case 1024: mode = 0x05; break;
- default: return;
- }
- if(pin == 5 || pin == 6) {
- TCCR0B = TCCR0B & 0b11111000 | mode;
- } else {
- TCCR1B = TCCR1B & 0b11111000 | mode;
- }
- } else if(pin == 3 || pin == 11) {
- switch(divisor) {
- case 1: mode = 0x01; break;
- case 8: mode = 0x02; break;
- case 32: mode = 0x03; break;
- case 64: mode = 0x04; break;
- case 128: mode = 0x05; break;
- case 256: mode = 0x06; break;
- case 1024: mode = 0x7; break;
- default: return;
- }
- TCCR2B = TCCR2B & 0b11111000 | mode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement