Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Index Fan PWM
- * Controls Noctua fan speed via PWM output @25kHz using a
- * Digispark https://content.instructables.com/ORIG/F75/EOPS/JEST5ZTO/F75EOPSJEST5ZTO.jpg
- * Step through PWM speeds in 10% increments with momentary pushbutton.
- * Derived from https://www.arduino.cc/en/tutorial/pushbutton
- * https://digistump.com/wiki/digispark/tricks#how_to_increase_hardware_pwm_frequency
- * https://www.electroschematics.com/attiny85-pwm-primer-tutorial-using-arduino/
- * Noctua PWM white paper https://noctua.at/pub/media/wysiwyg/Noctua_PWM_specifications_white_paper.pdf
- */
- #include <Arduino.h>
- int speed = 2; //Default starting fan speed to 50%
- unsigned long previousMillis = 0; // will store last time button registered
- long OnTime = 250; // milliseconds of on-time
- long OffTime = 750; // milliseconds of off-time
- // Maybe Add temp based speed changes?
- // int get_temp() {
- // analogReference(INTERNAL1V1);
- // int raw = analogRead(A0+15);
- // /* Original code used a 13 deg adjustment. But based on my results, I didn't seem to need it. */
- // // raw -= 13; // raw adjust = kelvin //this value is used to calibrate to your chip
- // int in_c = raw - 273; // celcius
- // analogReference(DEFAULT);
- // return in_c;
- // }
- void setup() {
- //P0, P1, and P4 are capable of hardware PWM (analogWrite).
- pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs,
- //for analog (PWM) outputs the pin number matches the port number.
- pinMode(5, INPUT); //5 is P5
- analogWrite(0,128); //Set the PWM pin to 50%
- }
- // Loop for button press to increment PWM speed by 10% up to 255
- void loop() {
- // check to see if it's time to change the PWM percentage
- unsigned long currentMillis = millis();
- if((digitalRead(5) == HIGH) && (currentMillis - previousMillis >= OnTime))
- {
- previousMillis = currentMillis; // Remember the time
- }
- else if ((digitalRead(5) == LOW) && (currentMillis - previousMillis >= OffTime))
- {
- previousMillis = currentMillis; // Remember the time
- // Adjust PWM speed based on current speed.
- switch (speed)
- {
- case 4:
- analogWrite(0,64); //Set the PWM pin to 25%
- speed = 1;
- break;
- case 3:
- analogWrite(0,255); ////Set the PWM pin to 100%
- speed = 4;
- break;
- case 2:
- analogWrite(0,192); ////Set the PWM pin to 75%
- speed = 3;
- break;
- case 1:
- analogWrite(0,128); ////Set the PWM pin to 50%
- speed = 2;
- break;
- }
- }
- }
RAW Paste Data