realityloop

Untitled

Jan 23rd, 2021 (edited)
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Index Fan PWM
  3.  * Controls Noctua fan speed via PWM output @25kHz using a
  4.  * Digispark https://content.instructables.com/ORIG/F75/EOPS/JEST5ZTO/F75EOPSJEST5ZTO.jpg
  5.  * Step through PWM speeds in 10% increments with momentary pushbutton.
  6.  * Derived from https://www.arduino.cc/en/tutorial/pushbutton
  7.  * https://digistump.com/wiki/digispark/tricks#how_to_increase_hardware_pwm_frequency
  8.  * https://www.electroschematics.com/attiny85-pwm-primer-tutorial-using-arduino/
  9.  * Noctua PWM white paper https://noctua.at/pub/media/wysiwyg/Noctua_PWM_specifications_white_paper.pdf
  10.  */
  11. #include <Arduino.h>
  12.  
  13. int speed = 2; //Default starting fan speed to 50%
  14.  
  15.  
  16. // constants won't change. They're used here to set pin numbers:
  17. const int buttonPin = 5;    // the number of the pushbutton pin
  18. const int pwmPin = 0;      // the number of the LED pin
  19.  
  20. int buttonState;             // the current reading from the input pin
  21. int lastButtonState = HIGH;   // the previous reading from the input pin
  22.  
  23. // the following variables are unsigned longs because the time, measured in
  24. // milliseconds, will quickly become a bigger number than can be stored in an int.
  25. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  26. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  27.  
  28. void setup() {
  29.     //P0, P1, and P4 are capable of hardware PWM (analogWrite).
  30.     pinMode(pwmPin, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs,
  31.                         //for analog (PWM) outputs the pin number matches the port number.
  32.     pinMode(buttonPin, INPUT_PULLUP); //5 is P5
  33.    
  34.     analogWrite(pwmPin,64); //Set the PWM pin to 25%
  35. }
  36.  
  37. // Loop for button press to increment PWM speed by 10% up to 255
  38. void loop() {
  39.   // read the state of the switch into a local variable:
  40.   int reading = digitalRead(buttonPin);
  41.  
  42.   // check to see if you just pressed the button
  43.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  44.   // since the last press to ignore any noise:
  45.  
  46.   // If the switch changed, due to noise or pressing:
  47.   if (reading != lastButtonState) {
  48.     // reset the debouncing timer
  49.     lastDebounceTime = millis();
  50.   }
  51.  
  52.   if ((millis() - lastDebounceTime) > debounceDelay) {
  53.     // whatever the reading is at, it's been there for longer than the debounce
  54.     // delay, so take it as the actual current state:
  55.  
  56.     // if the button state has changed:
  57.     if (reading != buttonState) {
  58.       buttonState = reading;
  59.  
  60.       // only toggle the LED if the new button state is HIGH
  61.       if (buttonState == HIGH) {
  62.         // Adjust PWM speed based on current speed.
  63.         switch (speed)
  64.         {
  65.           case 5:
  66.             analogWrite(pwmPin,64); //Set the PWM pin to 25%
  67.             speed = 1;
  68.             break;
  69.           case 4:
  70.             analogWrite(pwmPin,0); //Set the PWM pin to 0%
  71.             speed = 5;
  72.             break;
  73.           case 3:
  74.             analogWrite(pwmPin,255); ////Set the PWM pin to 100%
  75.             speed = 4;
  76.             break;
  77.           case 2:
  78.             analogWrite(pwmPin,192); ////Set the PWM pin to 75%
  79.             speed = 3;
  80.             break;
  81.           case 1:
  82.             analogWrite(pwmPin,128); ////Set the PWM pin to 50%
  83.             speed = 2;
  84.             break;
  85.         }
  86.       }
  87.     }
  88.   }
  89.   // save the reading. Next time through the loop, it'll be the lastButtonState:
  90.   lastButtonState = reading;
  91. }
Add Comment
Please, Sign In to add comment