Advertisement
pleasedontcode

**Beeping Sound** rev_02

Dec 26th, 2024
98
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: **Beeping Sound**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-26 20:32:20
  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. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* fix the code */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h> // Include Arduino library for basic functions
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void myPwm(unsigned char duty, float freq); // Function prototype for myPwm
  36.  
  37. int klik = 800; // Initialize klik variable
  38.  
  39. void setup(void)
  40. {
  41.     // Set pin 10 as an output
  42.     pinMode(10, OUTPUT); // Must use D10
  43. }
  44.  
  45. void loop(void)
  46. {
  47.     // Main loop to create a high frequency beeping noise
  48.     for (int klik = 800; klik > 0; klik -= 100) // Decrease klik in steps
  49.     {
  50.         myPwm(127, 2500); // Set PWM to 50% duty cycle at 2500 Hz
  51.         delay(klik); // Wait for the current klik duration
  52.         myPwm(0, 2500); // Turn off PWM
  53.         delay(klik); // Wait for the current klik duration
  54.     }
  55.     myPwm(127, 2500); // Final beep at 2500 Hz
  56. }
  57.  
  58. void myPwm(unsigned char duty, float freq) {
  59.     TCCR1A = 0x21; // Set Timer/Counter Control Register A
  60.     TCCR1B = 0x14; // Set Timer/Counter Control Register B
  61.     OCR1A = (16000000 / (freq * 8)) - 1; // Calculate OCR1A for desired frequency
  62.     OCR1B = OCR1A * (duty / 255.0); // Calculate OCR1B for desired duty cycle
  63. }
  64.  
  65. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement