Advertisement
safwan092

RX_imnp441_SA

Apr 8th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include <driver/i2s.h>
  2.  
  3. int goToPosition = 0;
  4.  
  5.  
  6. #define I2S_WS 15
  7. #define I2S_SD 13
  8. #define I2S_SCK 2
  9. #define I2S_PORT I2S_NUM_0
  10.  
  11. #define bufferLen 64
  12.  
  13. size_t bytesIn = 0;
  14. int16_t sBuffer[bufferLen];
  15. void setup() {
  16. Serial.begin(115200);
  17. Serial2.begin(115200);
  18. Serial.println("Setup I2S ...");
  19.  
  20. delay(1000);
  21. i2s_install();
  22. i2s_setpin();
  23. i2s_start(I2S_PORT);
  24. delay(500);
  25. }
  26.  
  27. void loop() {
  28.  
  29. readSoundSensors();
  30. //-------------------------MoveCar
  31. /*
  32. * goToPosition == 0 STOP
  33. *
  34. * goToPosition == 1 Right
  35. * delay(1000)
  36. * Move Forward
  37. * delay(500)
  38. * STOP
  39. * goToPosition = 0
  40. *
  41. *
  42. * goToPosition == 2 Left
  43. * delay(1000)
  44. * Move Forward
  45. * delay(500)
  46. * STOP
  47. * goToPosition = 0
  48. *
  49. */
  50. //--------------------------------
  51. }
  52.  
  53.  
  54.  
  55.  
  56. void readSoundSensors() {
  57. if (Serial2.available()) {
  58. String receivedData = Serial2.readStringUntil('\n');
  59. Serial.println("Received data: " + receivedData);
  60. goToPosition = 1;//1 = Go Right
  61. }
  62. bytesIn = 0;
  63.  
  64. esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
  65.  
  66. if (result == ESP_OK) {
  67. int samples_read = bytesIn / 8;
  68. if (samples_read > 0) {
  69. float mean = 0;
  70. for (int i = 0; i < samples_read; ++i) {
  71. mean += (sBuffer[i]);
  72. }
  73. mean /= samples_read;
  74. if (mean < 500) {
  75.  
  76. }
  77. else {
  78. Serial.println("L");//"S2:");
  79. goToPosition = 2;//2= Go To Left
  80. //Serial.println(mean);
  81. //Serial.print(",");
  82. }
  83.  
  84. }
  85. }
  86. }
  87. void i2s_install() {
  88. const i2s_config_t i2s_config = {
  89. .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
  90. .sample_rate = 44100,
  91. .bits_per_sample = i2s_bits_per_sample_t(16),
  92. .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  93. .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
  94. .intr_alloc_flags = 0, // default interrupt priority
  95. .dma_buf_count = 8,
  96. .dma_buf_len = bufferLen,
  97. .use_apll = false
  98. };
  99.  
  100. i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  101. }
  102.  
  103. void i2s_setpin() {
  104. const i2s_pin_config_t pin_config = {
  105. .bck_io_num = I2S_SCK,
  106. .ws_io_num = I2S_WS,
  107. .data_out_num = -1,
  108. .data_in_num = I2S_SD
  109. };
  110.  
  111. i2s_set_pin(I2S_PORT, &pin_config);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement