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%
- // constants won't change. They're used here to set pin numbers:
- const int buttonPin = 5; // the number of the pushbutton pin
- const int pwmPin = 0; // the number of the LED pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = HIGH; // the previous reading from the input pin
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- void setup() {
- //P0, P1, and P4 are capable of hardware PWM (analogWrite).
- pinMode(pwmPin, 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(buttonPin, INPUT_PULLUP); //5 is P5
- analogWrite(pwmPin,64); //Set the PWM pin to 25%
- }
- // Loop for button press to increment PWM speed by 10% up to 255
- void loop() {
- // read the state of the switch into a local variable:
- int reading = digitalRead(buttonPin);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited long enough
- // since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH) {
- // Adjust PWM speed based on current speed.
- switch (speed)
- {
- case 5:
- analogWrite(pwmPin,64); //Set the PWM pin to 25%
- speed = 1;
- break;
- case 4:
- analogWrite(pwmPin,0); //Set the PWM pin to 0%
- speed = 5;
- break;
- case 3:
- analogWrite(pwmPin,255); ////Set the PWM pin to 100%
- speed = 4;
- break;
- case 2:
- analogWrite(pwmPin,192); ////Set the PWM pin to 75%
- speed = 3;
- break;
- case 1:
- analogWrite(pwmPin,128); ////Set the PWM pin to 50%
- speed = 2;
- break;
- }
- }
- }
- }
- // save the reading. Next time through the loop, it'll be the lastButtonState:
- lastButtonState = reading;
- }
Add Comment
Please, Sign In to add comment