Advertisement
pleasedontcode

Audio Control rev_01

Oct 30th, 2024
75
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: Audio Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-31 04:48:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When the tail is twisted more than 100 degrees */
  21.     /* back or forth, one of three random audio files */
  22.     /* needs to play through Processing. The ability to */
  23.     /* twist the tail backwards or forwards to play one */
  24.     /* of the three random audio files needs to reset */
  25.     /* each time */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Audio.h>  // Include the Audio library for audio playback
  30. #include <Random.h> // Include the Random library for random number generation
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF ANALOG INPUT PINS *****/
  37. const uint8_t tail_Potentiometer_Vout_PIN_A0 = A0;
  38.  
  39. // Audio file names
  40. const char* audioFiles[] = {"audio1.wav", "audio2.wav", "audio3.wav"}; // Array of audio file names
  41.  
  42. // Audio object
  43. AudioPlayMemory playMem; // Create an audio play object
  44. AudioOutputI2S audioOutput; // Create an audio output object
  45. AudioConnection patchCord(playMem, 0, audioOutput, 0); // Connect the audio play object to the output
  46.  
  47. // Variable to store the last position of the potentiometer
  48. int lastPotValue = 0;
  49.  
  50. void setup(void)
  51. {
  52.     // put your setup code here, to run once:
  53.     pinMode(tail_Potentiometer_Vout_PIN_A0, INPUT); // Set the potentiometer pin as input
  54.     AudioMemory(8); // Allocate memory for audio processing
  55. }
  56.  
  57. void loop(void)
  58. {
  59.     // Read the potentiometer value
  60.     int potValue = analogRead(tail_Potentiometer_Vout_PIN_A0);
  61.  
  62.     // Map the potentiometer value to an angle (0-1023 to 0-180 degrees)
  63.     float angle = map(potValue, 0, 1023, 0, 180);
  64.  
  65.     // Check if the angle exceeds 100 degrees
  66.     if (angle > 100 && lastPotValue <= 100) {
  67.         // Play a random audio file
  68.         int randomIndex = random(0, 3); // Generate a random index (0 to 2)
  69.         playMem.play(audioFiles[randomIndex]); // Play the selected audio file
  70.     }
  71.  
  72.     // Reset the last potentiometer value if the angle is less than or equal to 100
  73.     if (angle <= 100) {
  74.         lastPotValue = angle; // Update the last potentiometer value
  75.     }
  76.  
  77.     // Allow audio processing to run
  78.     delay(100); // Small delay to avoid rapid triggering
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement