diff --git a/Arduinoscript/Arduinoscript.ino b/Arduinoscript/Arduinoscript.ino new file mode 100644 index 0000000..395cd0a --- /dev/null +++ b/Arduinoscript/Arduinoscript.ino @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#define SAMPLE_BUFFER_SIZE 512 +short sampleBuffer[SAMPLE_BUFFER_SIZE]; +volatile int samplesRead = 0; +static const char channels = 1; +static const int frequency = 16000; + +// Ultrasonic instructions +byte triggerPin = 11; +byte echoPin = 12; + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!HS300x.begin()) { + Serial.println("Failed to initialize humidity temperature sensor!"); + while (1); + } + PDM.onReceive(onPDMdata); + if (!PDM.begin(channels, frequency)) { + Serial.println("Failed to start PDM!"); + while (1); + } + HCSR04.begin(triggerPin, echoPin); +} + +void loop() +{ + float temperature = HS300x.readTemperature(); + int volume = getVolume(); + double* distances = HCSR04.measureDistanceCm(); + String output = String(temperature) + "|" + String(distances[0]) + "|" + String(volume); + Serial.println(output); + delay(1000); +} + +int getVolume() +{ + int maxVolume = 0; // Initialize variable to store the maximum volume + int sum = 0; + + if (samplesRead > 0) { + for (int i = 0; i < samplesRead; i++) { + int sampleAbs = abs(sampleBuffer[i]); + if (sampleAbs > maxVolume) { + maxVolume = sampleAbs; // Update max volume if a higher value is found + } + } + samplesRead = 0; // Reset after processing + } + + return maxVolume; +} + +void onPDMdata() +{ + int bytesAvailable = PDM.available(); + if (bytesAvailable) { + PDM.read(sampleBuffer, bytesAvailable); + samplesRead = bytesAvailable / 2; // Update the number of samples read + } +} \ No newline at end of file diff --git a/README.md b/README.md index 510de95..99553b0 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,7 @@ JS library (server): - socket-io (permet de communiquer entre serveur et client grace a des events) - socket-io-client - express (un framework basés sur les applications web) -- node:http \ No newline at end of file +- node:http + + +Cablage arduino : ![alt text](./img/cable.png) \ No newline at end of file diff --git a/img/cable.png b/img/cable.png new file mode 100644 index 0000000..70a9945 Binary files /dev/null and b/img/cable.png differ