realityloop

Untitled

Jan 23rd, 2021 (edited)
571
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. unsigned long previousMillis = 0; // will store last time button registered
  15. long OnTime = 250;           // milliseconds of on-time
  16. long OffTime = 750;          // milliseconds of off-time
  17.  
  18. // Maybe Add temp based speed changes?
  19. // int get_temp() {
  20. //   analogReference(INTERNAL1V1);
  21. //   int raw = analogRead(A0+15);
  22. //   /* Original code used a 13 deg adjustment. But based on my results, I didn't seem to need it. */
  23. //   // raw -= 13; // raw adjust = kelvin //this value is used to calibrate to your chip
  24. //   int in_c = raw - 273; // celcius
  25. //   analogReference(DEFAULT);
  26. //   return in_c;
  27. // }
  28.  
  29. void setup() {
  30.     //P0, P1, and P4 are capable of hardware PWM (analogWrite).
  31.     pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs,
  32.                         //for analog (PWM) outputs the pin number matches the port number.
  33.     pinMode(5, INPUT); //5 is P5
  34.     analogWrite(0,128); //Set the PWM pin to 50%
  35. }
  36.  
  37. // Loop for button press to increment PWM speed by 10% up to 255
  38. void loop() {
  39.   // check to see if it's time to change the PWM percentage
  40.   unsigned long currentMillis = millis();
  41.  
  42.   if((digitalRead(5) == HIGH) && (currentMillis - previousMillis >= OnTime))
  43.   {
  44.     previousMillis = currentMillis;  // Remember the time
  45.   }
  46.   else if ((digitalRead(5) == LOW) && (currentMillis - previousMillis >= OffTime))
  47.   {
  48.     previousMillis = currentMillis;   // Remember the time
  49.     // Adjust PWM speed based on current speed.
  50.     switch (speed)
  51.     {
  52.       case 4:
  53.         analogWrite(0,64); //Set the PWM pin to 25%
  54.         speed = 1;
  55.         break;
  56.       case 3:
  57.         analogWrite(0,255); ////Set the PWM pin to 100%
  58.         speed = 4;
  59.         break;
  60.       case 2:
  61.         analogWrite(0,192); ////Set the PWM pin to 75%
  62.         speed = 3;
  63.         break;
  64.       case 1:
  65.         analogWrite(0,128); ////Set the PWM pin to 50%
  66.         speed = 2;
  67.         break;
  68.     }
  69.   }
  70. }
RAW Paste Data