/* First version of nano timer 30/12/2020 nano_sound.ino and https://github.com/Staacks/phyphox-arduino#randomnumbersino are used Second version - b : send only time difference 3d version - c : PDM.end() and print format 2/01/2021 */ #include #include // buffer to read samples into, each sample is 16-bits short sampleBuffer[256]; // number of samples read volatile int samplesRead; unsigned long runs = 0L; short ntime = 0; float mtime[6]; float msound[6]; float art = 422.2; // Arduino threshold float tDelay = 0.5; float intervalMax = 0; short nMax = 0; float t = 0.0; float t1; float dt; void setup() { // put your setup code here, to run once: PhyphoxBLE::start(); //Start the BLE server // PhyphoxBLE::configHandler = &receivedData; // not for here !!?? receivedData(); // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); while (!Serial); // blocks the program untill opening of the Serial Monitor. // It allows simple peparation of the arduino starting condition. runs = 0L; ntime = 0; mtime[0] = 0; msound[0] = 0; dt = 1.0 / 16000; // configure the data receive callback PDM.onReceive(onPDMdata); // initialize PDM with: // - one channel (mono mode) // - a 16 kHz sample rate if (!PDM.begin(1, 16000)) { Serial.println("Failed to start PDM!"); digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH voltage level) while (1); } } // end setup void loop() { // put your main code here, to run repeatedly: PhyphoxBLE::poll(); // ? // Only required for the Arduino Nano 33 IoT, // but it does no harm for other boards. // wait for samples to be read if (samplesRead) { // looking for the maximum intervalMax = 0; nMax = 0; for (int n = 0; n < samplesRead; n++) { if (sampleBuffer[n] > intervalMax) { intervalMax = sampleBuffer[n]; nMax = n; } // end if 2 } // end for (looking for the maximum) t1 = t + nMax * dt; if ((intervalMax > art) && ((t1-mtime[ntime]) > tDelay)) { ++ntime; mtime[ntime] = t1; msound[ntime] = intervalMax; } t += samplesRead * dt; samplesRead = 0; // clear the read count } // end if (samplesRead) ++runs; if (ntime == 2) { // PDM.end(); // added 2/01/2021, may change fonctioning.. Serial.println("--------------"); Serial.println("Ypa !"); // print samples to the serial monitor or plotter Serial.println("time (s) / sound max"); for (int i = 0; i <= ntime; i++) { Serial.print(mtime[i], 4); Serial.print(" / "); Serial.println(msound[i], 0); // printing max sound // PhyphoxBLE::write(mtime[i]); //Send value to phyphox delay(50); //Shortly pause before repeating } float deltaTime = mtime[2] - mtime[1]; Serial.println("--------------"); Serial.print("dt = "); Serial.println(deltaTime, 4); Serial.println("--------------"); Serial.print("runs = "); Serial.println(runs); Serial.print("threshold = "); Serial.println(art, 1); Serial.println("--------------"); PhyphoxBLE::write(deltaTime); delay(50); // remains here for about 11 sec and blinking for (int i = 0; i < 5; i++) { blinking(ntime, 100, 100); delay(2000); // wait for 2 seconds } // end for digitalWrite(LED_BUILTIN, HIGH); // led on, waiting for 2 claps ++ntime; } // end if (ntime == 2) // after 2 more claps returns to original state and restart if (ntime>=5) { t = 0; runs = 0L; ntime = 0; receivedData(); digitalWrite(LED_BUILTIN, LOW); // led off, ready for restart } } // end loop void receivedData() { PhyphoxBLE::read(art); blinking(4, 50, 200); } void onPDMdata() { // query the number of bytes available int bytesAvailable = PDM.available(); // read into the sample buffer PDM.read(sampleBuffer, bytesAvailable); // 16-bit, 2 bytes per sample samplesRead = bytesAvailable / 2; } void blinking(int blinks, int tON, int tOFF) { for (int i = 0; i < blinks; i++) { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(tON); // wait for tON ms digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(tOFF); // wait for tOFF ms } }