Arduino Theremin Code
Arduino Theremin Code
This projects is a theremin created with two ultrasonic sensors, one controls
the frequency (tone) and the other controls the amplitude (volume).
This project is a fork of two projects:
- https://create.arduino.cc/projecthub/opus9/theremino-f72d32 (this project
had library conflicts)
- https://create.arduino.cc/projecthub/jvmccall/ultrasonic-theremin-033d6f
(this project had typo errors in the code and a mistake in the schema)
Resource on controlling similar potentiometer using SPI protocol, provides
example code and electrical setup:
http://www.learningaboutelectronics.com/Articles/MCP4131-digital-potentiometer-
circuit.php
*/
/*
Potentiometer conecctions:
1 - Arduino pin ~9
2 - Speaker +
3 - GND
4 - 5V
5 - Arduino pin ~10
6 - Arduino pin 13
7 - Arduino pin ~11
8 - GND
More info https://app.ultralibrarian.com/details/23D04BE7-10A0-11E9-AB3A-
0A3560A4CCCC/Microchip/MCP4161-103E-P
*/
#define MIN_DISTANCE 35
NewPing sonar(5, 6, 35); // the first and the second number are the pins of the
sensor of volume, the third is the maximum distance
// For SPI
byte address = 0x00;
int CS= 10;
int echo = 3;
int trigger = 4;
int distance;
int PotValue;
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);
delay(500);
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
digitalPotWrite(PotValue);
delay(10);
}