Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <driver/i2s.h>
- int goToPosition = 0;
- #define I2S_WS 15
- #define I2S_SD 13
- #define I2S_SCK 2
- #define I2S_PORT I2S_NUM_0
- #define bufferLen 64
- size_t bytesIn = 0;
- int16_t sBuffer[bufferLen];
- void setup() {
- Serial.begin(115200);
- Serial2.begin(115200);
- Serial.println("Setup I2S ...");
- delay(1000);
- i2s_install();
- i2s_setpin();
- i2s_start(I2S_PORT);
- delay(500);
- }
- void loop() {
- readSoundSensors();
- //-------------------------MoveCar
- /*
- * goToPosition == 0 STOP
- *
- * goToPosition == 1 Right
- * delay(1000)
- * Move Forward
- * delay(500)
- * STOP
- * goToPosition = 0
- *
- *
- * goToPosition == 2 Left
- * delay(1000)
- * Move Forward
- * delay(500)
- * STOP
- * goToPosition = 0
- *
- */
- //--------------------------------
- }
- void readSoundSensors() {
- if (Serial2.available()) {
- String receivedData = Serial2.readStringUntil('\n');
- Serial.println("Received data: " + receivedData);
- goToPosition = 1;//1 = Go Right
- }
- bytesIn = 0;
- esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
- if (result == ESP_OK) {
- int samples_read = bytesIn / 8;
- if (samples_read > 0) {
- float mean = 0;
- for (int i = 0; i < samples_read; ++i) {
- mean += (sBuffer[i]);
- }
- mean /= samples_read;
- if (mean < 500) {
- }
- else {
- Serial.println("L");//"S2:");
- goToPosition = 2;//2= Go To Left
- //Serial.println(mean);
- //Serial.print(",");
- }
- }
- }
- }
- void i2s_install() {
- const i2s_config_t i2s_config = {
- .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
- .sample_rate = 44100,
- .bits_per_sample = i2s_bits_per_sample_t(16),
- .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
- .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
- .intr_alloc_flags = 0, // default interrupt priority
- .dma_buf_count = 8,
- .dma_buf_len = bufferLen,
- .use_apll = false
- };
- i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
- }
- void i2s_setpin() {
- const i2s_pin_config_t pin_config = {
- .bck_io_num = I2S_SCK,
- .ws_io_num = I2S_WS,
- .data_out_num = -1,
- .data_in_num = I2S_SD
- };
- i2s_set_pin(I2S_PORT, &pin_config);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement