Hey Guys,
I'm trying to make a theremin with Arduino, using an arduino UNO, shield base, two ultrasonic sensor (one for the frequency, one for the volume), and a speaker.
The thing is with the integrated tone module I can't control the volume, but with every other library (I've tried libraries for the speaker and for the Ultrasonic sensor) there's a problem. I don't really understand what is it but it might be because they are all setting on the same timer on the arduino card.
Could you help me?
Here's my code :
#define BUZZER_PIN 8
#include "Ultrasonic.h"
Ultrasonic telemetre1(2);
Ultrasonic telemetre2(3);
// Fréquences des notes de Do3 à Do4
#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
// Distance
long distance;
// Frequences
int note[] = {
Do3, Dod3, Re3, Red3, Mi3, Fa3, Fad3, So3, Sod3, La3, Lad3, Si3, Do4
};
// Volumes
int vol[] = {
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
};
void setup() {
Serial.begin(9600);
}
void loop() {
int frequence = disttofreq();
sound(frequence);
delay(10);
}
int disttofreq() {
distance = telemetre1.MeasureInCentimeters();
if (distance > 50){
return 0;
}
int inote = map(distance, 0, 50, 0, 13);
int frequence = note[inote];
return frequence;
}
int disttovol() {
distance = telemetre2.MeasureInCentimeters();
if (distance > 50){
return 0;
}
int ivol = map(distance, 0, 50, 0, 11);
int volume = vol[ivol];
}
void sound(int frequence) {
tone(BUZZER_PIN, frequence, 500); // Pin du Buzzer, frequence qu'on veut jouer et temps ou on joue la note
}