Advertisement
pleasedontcode

"PWM Control" rev_01

Dec 26th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "PWM Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-26 20:06:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* make a high frequency beeping noise that gets */
  21.     /* quicker and quicker and is steady high pitch when */
  22.     /* drops to zero */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <TimerOne.h> // Include TimerOne library
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void myPwm(unsigned char duty, float freq); // Added function prototype for myPwm
  34.  
  35. // Instantiate TimerOne object
  36. TimerOne timer;
  37.  
  38. void setup(void)
  39. {
  40.     // put your setup code here, to run once:
  41.     pinMode(10, OUTPUT); // Must use D10
  42. }
  43.  
  44. void loop(void)
  45. {
  46.     // put your main code here, to run repeatedly:
  47.     for(int klik = 800; klik > 0; klik -= 50) // Adjusted to go down to 0 for steady high pitch
  48.     {
  49.         myPwm(127, 2500); // e.g. (127/255)50% duty with 2500 Hz
  50.         delay(klik);
  51.         myPwm(0, 2500); // Turn off sound
  52.         delay(klik);
  53.     }
  54.     myPwm(127, 2500); // Maintain high pitch sound when dropping to zero
  55. }
  56.  
  57. void myPwm(unsigned char duty, float freq)
  58. {
  59.     timer.initialize(1000000 / freq); // Initialize Timer1 with frequency
  60.     timer.pwm(10, duty); // Set PWM on pin 10
  61. }
  62.  
  63. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement