Advertisement
pleasedontcode

Audio Control rev_02

Nov 1st, 2024
102
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: ESP32 DevKit V1
  14.     - Source Code created on: 2024-11-01 16:35:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* the robot I talked with. so all I need is to talk */
  21.     /* with the ai from the microphone and listen to the */
  22.     /* answer from the speaker so I have > breadboard and */
  23.     /* esp32 and wire and microphone and speaker and */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t speaker_PushButton_PIN_D4 = 4; // Button pin
  35. const uint8_t microphone_PIN = 34; // Microphone pin (analog input)
  36. const uint8_t speaker_PIN = 25; // Speaker pin (digital output)
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Instance of the button with the defined pin and default parameters
  40. EasyButton button(speaker_PushButton_PIN_D4); // Initializing EasyButton object
  41.  
  42. void setup(void)
  43. {
  44.     // put your setup code here, to run once:
  45.     Serial.begin(115200); // Initialize Serial for debugging purposes
  46.     Serial.println(); // Print a new line
  47.     Serial.println(">>> EasyButton example <<<"); // Print example header
  48.  
  49.     button.begin(); // Initialize the button
  50.     button.onPressed([]() { // Define the callback for button press
  51.         Serial.println("Button pressed"); // Print message on button press
  52.         // Here you can add code to handle microphone input and speaker output
  53.         // For example, read from the microphone and play a sound through the speaker
  54.     });
  55.  
  56.     // Initialize microphone and speaker pins
  57.     pinMode(microphone_PIN, INPUT); // Set microphone pin as input
  58.     pinMode(speaker_PIN, OUTPUT); // Set speaker pin as output
  59. }
  60.  
  61. void loop(void)
  62. {
  63.     // put your main code here, to run repeatedly:
  64.     button.read(); // Continuously read the button state
  65.  
  66.     // Here you can add code to read from the microphone and control the speaker
  67.     // For example, you could read the microphone value and play a sound based on it
  68.     int micValue = analogRead(microphone_PIN); // Read microphone value
  69.     Serial.print("Microphone Value: "); // Print microphone value
  70.     Serial.println(micValue); // Print the value read from the microphone
  71.  
  72.     // Add logic to control the speaker based on microphone input
  73.     if (micValue > 1000) { // Example threshold
  74.         digitalWrite(speaker_PIN, HIGH); // Turn on the speaker
  75.     } else {
  76.         digitalWrite(speaker_PIN, LOW); // Turn off the speaker
  77.     }
  78. }
  79.  
  80. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement