SHARE
    TWEET
    ferrybig

    Arduino PWM ATX 4pin fan

    Sep 3rd, 2020
    572
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.28 KB | None | 0 0
    1. /*
    2. Fade
    3. This example shows how to fade an LED on pin 9 using the analogWrite()
    4. function.
    5. The analogWrite() function uses PWM, so if you want to change the pin you're
    6. using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
    7. are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
    8. This example code is in the public domain.
    9. http://www.arduino.cc/en/Tutorial/Fade
    10. */
    11. #include <PWM.h>
    12. //use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
    13. int led = 3; // the pin that the LED is attached to
    14. // how many points to fade the LED by
    15. int32_t frequency = 25000; //frequency (in Hz)
    16. const int sensePin = 2;
    17. void setup()
    18. {
    19. Serial.begin(9600);
    20. //initialize all timers except for 0, to save time keeping functions
    21. InitTimersSafe();
    22. //sets the frequency for the specified pin
    23. bool success = SetPinFrequencySafe(led, frequency);
    24. //if the pin frequency was set successfully, pin 13 turn on
    25. if(success) {
    26. pinMode(13, OUTPUT);
    27. digitalWrite(13, HIGH);
    28. }
    29. pinMode(sensePin, INPUT_PULLUP);
    30. attachInterrupt(digitalPinToInterrupt(sensePin), sense, RISING);
    31. }
    32. int brightness = 5; // how bright the LED is
    33. int fadeAmount = 5; // how many points to fade the LED by
    34. unsigned int skipAdjust = 0;
    35. unsigned long lastPulse = 0;
    36. volatile unsigned long pulsus = 0;
    37. void sense() {
    38. pulsus++;
    39. }
    40. // the loop routine runs over and over again forever:
    41. void loop() {
    42. // set the brightness of pin 9:
    43. pwmWrite(led, brightness);
    44. // change the brightness for next time through the loop:
    45. if(skipAdjust != 0) {
    46. skipAdjust--;
    47. } else {
    48. brightness = brightness + fadeAmount;
    49. // reverse the direction of the fading at the ends of the fade:
    50. if (brightness <= 5 || brightness >= 255) {
    51. fadeAmount = -fadeAmount;
    52. skipAdjust = 10;
    53. }
    54. }
    55. unsigned long currentTime = millis();
    56. unsigned long pulses = pulsus;
    57. unsigned long freq = currentTime == lastPulse ? 0 : ( pulses * 1000 / (currentTime - lastPulse));
    58. pulsus -= pulses;
    59. lastPulse = currentTime;
    60. Serial.print(brightness);
    61. Serial.print(' ');
    62. Serial.print(freq);
    63. Serial.println();
    64. // wait for 50 milliseconds to see the dimming effect
    65. delay(1000);
    66. }
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /