Smart Systems - Arduino Theremin - Coding


Coding 1.0

#include <CapSense.h>

CapSense   cs_4_2 = CapSense(4,2);
CapSense   cs_4_5 = CapSense(4,5);

void setup()                  
{
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()                  
{
  long total = cs_4_2.capSense(30);
  long total2 = cs_4_5.capSense(30);
  int con = constrain(total+total2, 0, 5000);
  int bright = map(con, 0, 5000, 0, 500);
  Serial.print(total);
  Serial.print("\t");
  Serial.println(total2);
  if ((total+total2) > 100)
  {
    tone(6, total+total2,100);
  }
  analogWrite(8, constrain(bright, 0, 255));
  analogWrite(9, constrain(bright-50, 0, 255));
  analogWrite(10, constrain(bright-100, 0, 255));
  analogWrite(7, constrain(bright-150, 0, 255));
  analogWrite(12, constrain(bright-200, 0, 255));
  analogWrite(13, constrain(bright-250, 0, 255));
  delay(10);    // arbitrary delay
}




Coding 2.0

#include <CapSense.h>

CapSense   cs_4_2 = CapSense(4,2);
CapSense   cs_4_5 = CapSense(4,5);

void setup()                  
{
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()                  
{
  long total = cs_4_2.capSense(30);
  long total2 = cs_4_5.capSense(30);
  int val1 = sqrt(total);
  int val2 = sqrt(total2);
  int con = constrain(val1+val2, 0, 100);
  int bright = map(con, 0, 100  , 0, 500);
  int bright2 = map(con, 0, 100  , 0, 5000);
  Serial.print(val1);
  Serial.print("\t");
  Serial.println(val2);
  if ((val1 > 10)&&(val2 > 10))
  {
    tone(6,bright2,100);
  }
  analogWrite(8, constrain(bright, 0, 255));
  analogWrite(9, constrain(bright-50, 0, 255));
  analogWrite(10, constrain(bright-100, 0, 255));
  analogWrite(7, constrain(bright-150, 0, 255));
  analogWrite(12, constrain(bright-200, 0, 255));
  analogWrite(13, constrain(bright-250, 0, 255));
  delay(10);    // arbitrary delay
}

The coding I used for arduino theremin are listed here. the second version is the version I used. The second version is the one where I Square root the value to provide a more linear tone. I used the tone() function to provide the sound for the theremin.

I also introduced led volume indicator in the system. When the sound is in the orange led range the sound should sound the best, and when the sound pitch gets too high. The led will go in the red led zone just like a real mixer.

No comments:

Post a Comment