//In this project we create a theremin with two ultrasonic sensors, one for volume and another for tone. We use the digital potentiometer X9C103P.
// Two ultrasonic sensors are used to create a theremin. One sensor controls volume, the other controls frequency.
// This project is an iteration on a previous project I found for a theremin. The previous project had library conflicts (two libraries used the same timer) and used a different type of poteniometer. I have also added some other small features.
// Project which provided original inspiration: https://create.arduino.cc/projecthub/opus9/theremino-f72d32
// Resource on controlling similar potentiometer using SPI protocol, provides example code and electrical setup: http://www.learningaboutelectronics.com/Articles/MCP4131-digital-potentiometer-circuit.php
*
*/
//NewTone is used to avoid the timer incompatibiliy of Tone with NewPing.
#include <NewTone.h>
#include <NewPing.h>
#include <SPI.h>
#define MIN_DISTANCE 35
NewPing sonar(5, 6, 35); //sonar(pin, pin, distance), this controls volume
// name pins for sensors and the potentiometer
// For SPI
byte address = 0x00;
int CS= 10;
int echo = 3;
int trigger = 4;
int distance;
int PotValue;
unsigned long TimeResponse;
float distance2;
float tone1;
void setup() {
// set all the pins
Serial.begin(9600);
pinMode(trigger, OUTPUT); //Tone sensor
pinMode(echo, INPUT); //Tone sensor
pinMode (CS, OUTPUT);
SPI.begin();
//This has the speaker emit a start up sound from Low to High to Low
digitalPotWrite(255);
for (int i = 50; i <= 400; i++)
{
pinMode(9, OUTPUT);
NewTone(9,i);
delay(10);
}
delay(500);
for (int i = 400; i >= 50; i--)
{
pinMode(9, OUTPUT);
NewTone(9,i);
delay(10);
}
}
void loop() {
digitalWrite(trigger, HIGH);
delayMicroseconds(10); // creates a 10 microsecond pulse
digitalWrite(trigger, LOW);
TimeResponse = pulseIn(echo, HIGH); // records how long it took to receive echo of the pulse
distance2 = TimeResponse/58; //calculates distance in centimeters
if (distance2 < MIN_DISTANCE) {
//Conversion of distance into a value for a sound
tone1 = 50.0*pow(2,(distance2/12.0)); //calculate frequency, range of about 50-360 Hz
pinMode(9, OUTPUT);
NewTone(9,tone1);
}
else {
pinMode(9, OUTPUT); // This sets the sound to a high pitched noise when no distance is detected
NewTone(9,400);
}
distance = sonar.ping_cm(); //Uses the library NewPing to calculate distance
//distance is converted to Potentiometer relevant values
int PotValue = map(distance, 0, 35, 240, 255); // I only use a range of resistance which is very low otherwise the speaker is too quiet becuae it is only a 4 ohm speaker
if (distance == 0) // sets volume to max when no hand is detected or if hand is too far away
{
PotValue = 255;
}
digitalPotWrite(PotValue);
delay(10);
}
int digitalPotWrite(int value)
{
digitalWrite(CS, LOW); //This uses SPI protocol to communicate with the potentiometer and sets the resistance
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
Comments
Please log in or sign up to comment.