I’ve built a theremin like instrument using a daisyseed where I’m changing the pitch and volume using two ultrasonic distance sensors. Everything works fine but whenever I’m changing the pitch or volume there is this incredible loud crackle. Below is the code, a few pictures and a link to the video.
//THEREMIN VARIATON
//DENIS FLUERARU
//NEOPIXELS
#include <Adafruit_NeoPixel.h>
#define PIN D0
#define NUMPIXELS 48
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 30
//DISTANCE SENSORS
#define trigPin1 D2
#define echoPin1 D3
long duration1;
float distance1;
long counterzero1;
#define trigPin2 D5
#define echoPin2 D6
long duration2;
float distance2;
long counterzero2;
int midiSignal;
int MIDItoFQ;
float sonarAmplitude;
float ampSignal;
int lightState = 0;
int timer = -1;
//DAISY
#include "DaisyDuino.h"
DaisyHardware hw;
size_t num_channels;
static Oscillator osc;
void MyCallback(float **in, float **out, size_t size) {
osc.SetFreq(mtof(midiSignal));
osc.SetAmp(ampSignal);
osc.SetWaveform(osc.WAVE_TRI);
for (size_t i = 0; i < size; i++) {
float sig = osc.Process();
for (size_t chn = 0; chn < num_channels; chn++) {
out[chn][i] = sig;
}
}
}
void setup() {
float sample_rate;
// Initialize for Daisy pod at 48kHz
hw = DAISY.init(DAISY_SEED, AUDIO_SR_48K);
num_channels = hw.num_channels;
sample_rate = DAISY.get_samplerate();
osc.Init(sample_rate);
osc.SetWaveform(osc.WAVE_TRI);
DAISY.begin(MyCallback);
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin2, INPUT);
counterzero1 = 0;
counterzero2 = 0;
pixels.begin();
setPixels(255, 255, 255);
pixels.setBrightness(255);
}
void loop() {
frequency();
amplitude();
states();
}
void frequency() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance1 = (duration1 - 10) * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance1 = constrain(distance1, 20, 100);
MIDItoFQ = (distance1 - 20) / 5 + 48;
if (MIDItoFQ >= 64) {
MIDItoFQ = 0;
}
if (MIDItoFQ == 0) {
counterzero1++;
} else {
counterzero1 = 0;
}
if (counterzero1 >= 5 || counterzero1 == 0) {
midiSignal = MIDItoFQ;
}
}
void amplitude() {
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2 = (duration2 - 10) * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance2 = constrain(distance2, 20, 50);
sonarAmplitude = (distance2 - 20) * 0.033;
if (sonarAmplitude >= 0.95) {
sonarAmplitude = 0;
}
if (sonarAmplitude == 0) {
counterzero2++;
} else {
counterzero2 = 0;
}
if (counterzero2 >= 200 || counterzero2 == 0) {
ampSignal = sonarAmplitude;
//delay(50);
}
}
void setPixels(int r, int g, int b) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
}
void states() {
if (midiSignal == 48 && ampSignal == 0.00 && lightState != 2) {
if (timer == -1) {
lightState = 1;
timer = 0;
setPixels(255, 0, 0);
}
timer++;
if (timer >= 2500) {
lightState = 2;
}
return;
}
if (lightState == 2) {
setPixels(0, 255, 0);
}
if (counterzero1 >= 15) {
// if (midiSignal <= 0 && ampSignal <= 0.00 && lightState != 0) {
timer = -1;
lightState = 0;
setPixels(255,255, 255);
}
if (lightState == 0 || lightState == 1) {
midiSignal = 0;
ampSignal = 0.00;
}
}


