Some Background:
This project uses ESP32 to record and playback audio.
When the sensor is triggered and a button is pressed it will record audio. And with another button it will play the audio.
I get a compilation error saying I didn't declare a variable. Any help on this will be extremely appreciated. I've tried everything I could, declaring it as a class, a string, an integer etc. it still will not accept it and gives another type of error.
This is the error log:
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino: In function 'void loop()':
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino:113:8: error: 'output' was not declared in this scope
play(output, "/sdcard/tone.wav");
^
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino:115:10: error: 'input' was not declared in this scope
record(input, "/sdcard/recorded.wav");
^
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino: In function 'void Read_Button()':
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino:138:16: error: 'output' was not declared in this scope
{ play(output, "/sdcard/recorded.wav"); }
^
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino: In function 'void Read_Sensor()':
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino:169:17: error: 'output' was not declared in this scope
{ play(output, "/sdcard/standard.wav");
^
C:\Users\Shah\Desktop\New folder (2)\CodeVentxCasefinal\CodeVentxCasefinal.ino:175:17: error: 'output' was not declared in this scope
play(output, "/sdcard/recorded.wav");
^
Using library BLE at version 1.0.1 in folder: C:\Users\Shah\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\BLE
Using library SPIFFS at version 1.0 in folder: C:\Users\Shah\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\SPIFFS
Using library FS at version 1.0 in folder: C:\Users\Shah\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\FS
exit status 1
Compilation error: 'output' was not declared in this scope
Here is the complete code if someone needs it:
#include <Arduino.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <string>
#include "I2SMEMSSampler.h"
#include "ADCSampler.h"
#include "I2SOutput.h"
#include "DACOutput.h"
#include "SDCard.h"
#include "SPIFFS.h"
#include "WAVFileReader.h"
#include "WAVFileWriter.h"
#include "config.h"
//------------------------Variables Initialization---------------------------
bool Lock=true, Lock1=true, Lock2=true, Lock3=true, Lock4=true,
timer=false, Play=false, state=true;
int recordd=0;
unsigned long cm=0, pm=0;
//---------------------------------------------------------------------------
void record(I2SSampler *input, const char *fname)
{
int16_t *samples = (int16_t *)malloc(sizeof(int16_t) * 1024);
ESP_LOGI(TAG, "Start recording");
input->start();
// open the file on the sdcard
FILE *fp = fopen(fname, "wb");
// create a new wave file writer
WAVFileWriter *writer = new WAVFileWriter(fp, input->sample_rate());
// keep writing until the user releases the button
while (recordd==1)
{
int samples_read = input->read(samples, 1024);
int64_t start = esp_timer_get_time();
writer->write(samples, samples_read);
int64_t end = esp_timer_get_time();
ESP_LOGI(TAG, "Wrote %d samples in %lld microseconds", samples_read, end - start);
Read_Stop_Button ();
}
// stop the input
input->stop();
// and finish the writing
writer->finish();
fclose(fp);
delete writer;
free(samples);
ESP_LOGI(TAG, "Finished recording");
}
void play(Output *output, const char *fname)
{
int16_t *samples = (int16_t *)malloc(sizeof(int16_t) * 1024);
// open the file on the sdcard
FILE *fp = fopen(fname, "rb");
// create a new wave file writer
WAVFileReader *reader = new WAVFileReader(fp);
ESP_LOGI(TAG, "Start playing");
output->start(reader->sample_rate());
ESP_LOGI(TAG, "Opened wav file");
// read until theres no more samples
while (true)
{
int samples_read = reader->read(samples, 1024);
if (samples_read == 0)
{
break;
}
if (digitalRead(GPIO_BUTTON_2)==LOW)
break;
ESP_LOGI(TAG, "Read %d samples", samples_read);
output->write(samples, samples_read);
ESP_LOGI(TAG, "Played samples");
}
// stop the input
output->stop();
fclose(fp);
delete reader;
free(samples);
ESP_LOGI(TAG, "Finished playing");
}
void setup()
{
Serial.begin(115200);
pinMode(GPIO_BUTTON_1, INPUT_PULLUP);
pinMode(GPIO_BUTTON_2, INPUT_PULLUP);
pinMode(GPIO_BUTTON_3, INPUT_PULLUP);
pinMode(PIR_Sensor, INPUT);
xTaskCreate(main_task, "Main", 4096, NULL, 0, NULL);
}
void loop()
{
cm = millis();
Read_Button ();
Read_Sensor ();
if (recordd==1)
{
delay(3000);
play(output, "/sdcard/tone.wav");
delay(1000);
record(input, "/sdcard/recorded.wav");
recordd = 0;
}
}
//--------------------------------------------------------------------------
void Read_Button ()
{
if (digitalRead(GPIO_BUTTON_1)==LOW&&Lock1==true)
{ Lock1=false;
Lock=!Lock; }
else if (digitalRead(GPIO_BUTTON_1)==HIGH&&Lock1==false)
{ Lock1=true; }
if (digitalRead(GPIO_BUTTON_2)==LOW&&Lock2==true)
{ Lock2=false;
if (state==true)
{ play(output, "/sdcard/recorded.wav"); }
state=!state;
}
else if (digitalRead(GPIO_BUTTON_2)==HIGH&&Lock2==false)
{ Lock2=true; }
if (digitalRead(GPIO_BUTTON_3)==LOW&&Lock3==true)
{
Lock3=false;
if (recordd == 0)
recordd=1;
else
{recordd=3;
}
}
else if (digitalRead(GPIO_BUTTON_3)==HIGH&&Lock3==false)
{ Lock3=true; }
}
//---------------------------------Looking for PIR detection------------------------------
void Read_Sensor ()
{
if (digitalRead(PIR_Sensor)==HIGH&&Lock4==true)
{
Lock4=false;
if (Lock==true)
{ play(output, "/sdcard/standard.wav");
delay(1000);
play(output, "/sdcard/standard.wav");
}
else if (Lock==false)
{
play(output, "/sdcard/recorded.wav");
delay(1000);
play(output, "/sdcard/recorded.wav");
}
}
else if (digitalRead(PIR_Sensor)==LOW&&Lock4==false)
{
Lock4=true;
}
}
void Read_Stop_Button ()
{
if (digitalRead(GPIO_BUTTON_3)==LOW&&Lock3==true)
{
Lock3=false;
if (recordd == 0)
recordd=1;
else
{recordd=3;
}
}
else if (digitalRead(GPIO_BUTTON_3)==HIGH&&Lock3==false)
{ Lock3=true; }
}
void main_task(void *param)
{
ESP_LOGI(TAG, "Starting up");
#ifdef USE_SPIFFS
ESP_LOGI(TAG, "Mounting SPIFFS on /sdcard");
SPIFFS.begin(true, "/sdcard");
#else
ESP_LOGI(TAG, "Mounting SDCard on /sdcard");
new SDCard("/sdcard", PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CLK, PIN_NUM_CS);
#endif
ESP_LOGI(TAG, "Creating microphone");
#ifdef USE_I2S_MIC_INPUT
I2SSampler *input = new I2SMEMSSampler(I2S_NUM_0, i2s_mic_pins, i2s_mic_Config);
#else
I2SSampler *input = new ADCSampler(ADC_UNIT_1, ADC1_CHANNEL_7, i2s_adc_config);
#endif
#ifdef USE_I2S_SPEAKER_OUTPUT
Output *output = new I2SOutput(I2S_NUM_0, i2s_speaker_pins);
#else
Output *output = new DACOutput(I2S_NUM_0);
#endif
}