Hey Guys, after what seemed a million try to make an arduino theremin with two ultrasonic sensor, I found out that I could use the Volume library with the NewPing library without getting an error.
What the problem was before, was that I couldn't control volume and get the distance from the ultrasonic sensors at the same time. Some library worked together but the sound was horrible. But I saw on the Newping wiki that we could avoid the vector7 error by modifying the library. I did it. No mistakes while verifying the code.. No arror while televersing. But it doesn't work.
I tried some things, like removing the vol.begin() at the beginning, and then I could get the distance but no longer play sound.
I've got two grove ultrasonic sensor, one grove speaker, an arduino uno with a grove base shield.
Here's my code :
#include "Volume.h" // Include the Volume library
#include <NewPing.h>
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
Volume vol;
const char* noteNames[] = { "Do3", "Re3", "Mi3", "Fa3", "So3", "La3", "Si3", "Do4" };
const int colorR = 55;
const int colorG = 255;
const int colorB = 200;
#define Maxdistance 65 // We set The maximum distance for the ultrasonic sensors
#define Do3 261
#define Dod3 277
#define Re3 293
#define Red3 311
#define Mi3 329
#define Fa3 349
#define Fad3 369
#define So3 391
#define Sod3 415
#define La3 440
#define Lad3 466
#define Si3 493
#define Do4 523
// Frequences
int note[] = {
Do3, Re3, Mi3, Fa3, So3, La3, Si3, Do4
};
NewPing sonar1(2, 2, Maxdistance);
NewPing sonar2(4, 4, Maxdistance);
void setup() {
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
delay(1000);
vol.begin();
Serial.begin(115200);
}
void loop() {
int distance1 = sonar1.ping_cm(); // getting the distances
int distance2 = sonar2.ping_cm();
int inote = map(distance1, 0, Maxdistance, 0, 7);
uint16_t frequence = note[inote];
uint16_t volume = map(distance2, 0, Maxdistance, 255,0);
if (distance2 > Maxdistance || distance1 > Maxdistance || distance2 == 0 || distance1 == 0){
volume = 0;
frequence = 0;
}
vol.tone(frequence, volume);
Serial.print(distance1);
Serial.print(" ");
Serial.println(distance2);
lcd.setCursor(0, 0); // Placer le curseur au début
lcd.print("Frequence: "); // Texte de la ligne 1
lcd.print(frequence); // Valeur mesurée
lcd.print("Hz");
lcd.setCursor(0, 1);
lcd.print("Note: ");
if (inote >= 0) {
lcd.print(noteNames[inote]);
}
delay(65);
}