Oi, this website is currently on the move to https://turboflip.de - party on there!

LCTV #6 – Webbased Arduino Theremin

Arduino basic code for two sensors

/*
 *  This sketch uses both visual distance sensors to send key data via USB keyboard
 * 
 * */
 

#include "Keyboard.h" // reference to keyboard lib

int keycodes[] = {48,49,50,51,52,53,54,55,56,57}; // key map for all keys from 0 to 9

int dist_sensor_pin = 0; // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
int ldr_sensor_pin = 0; // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

float smoothed_distance = 0;
int distance = 0;

int ldr_threshold = 5; // value used as trigger edge for ldr sensor

bool is_triggered = false;

void setup() {
    Keyboard.begin(); // intialize Leonardo as a generic keyboard
}

void loop() {

  int brightness = analogRead(ldr_sensor_pin)/1024; // read brightness value from LDR sensor
  
  float volts = analogRead(dist_sensor_pin)*0.0048828125;  // value from sensor * (5/1024)
  distance = 13*pow(volts, -1); // worked out from datasheet graph
  distance = constrain(distance, 4, 30);// for reliability : limit all incoming data to range of 4-30
    
  // !!! SMOOTHING calculation for distance sensor  
  int smooth_amount = 10;
  smoothed_distance = smoothed_distance + ((distance - smoothed_distance)/smooth_amount);

  float mapped_distance =  map(smoothed_distance, 4, 30, 0, 10);
  
   if(brightness<ldr_threshold && is_triggered == false ){

      // send keyboard data
      Keyboard.write(  keycodes[  int(mapped_distance)   ] ); // send keycode to computer

      //set catching bool to avoid multiple triggering
      is_triggered = true;
    
   }

    // make triggerable again when sensor is light again
   if(brightness>ldr_threshold){
        is_triggered = false;
    }

    delay(10); //tiny delay for stability

}

p5.js basic setup

<!DOCTYPE html>
<html lang="en">
   <head>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.12/Tone.js" integrity="sha512-MyXAzMk3sw/i85erXXKd2+z2fzlmKD15TzKKwZd541ifNwUQ8Z73CfAwuyIPc0p5bL7xEA3u/l+/V0K9IyPbCg==" crossorigin="anonymous"></script>
 
                 
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    
     
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"></script>
    
       <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/addons/p5.dom.min.js"></script>
 
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />
 
  </head>
   
 
  <body>
    <main>
    </main>
    <script src="sketch.js"></script>
  </body>
</html>
let synth;
 
 // arrays for mapping
let mykeys = [48,49,50,51,52,53,54,55,56,57]; // a - ä
let basenotes = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"];
 
function setup() {
   
  createCanvas(window.innerWidth,window.innerHeight);
   
  initSynth();
   
}
 
// ------------------------------------------
 
 
function draw() {
  background(255,10);
}
 
// ------------------------------------------
 
function keyPressed(){
 
  let cnote = returnMappedNote(keyCode);
   
   console.log(keyCode);
   
   let now = Tone.now();
  synth.triggerAttackRelease( cnote, "2n", now );
 
 
}

// ------------------------------------------
 
function returnMappedNote(  _v ){
 
 
     let xdid = width/mykeys.length;
 
    for(let i=0; i< mykeys.length;i++){
     
        if( mykeys[i] == _v){
         
          //console.log("found key input" + _v );
          //console.log("this is the note: " +  basenotes[i]);
           
           
          noStroke();
          fill(0);
          rect( i*xdid,0,xdid,height );
           
           
          return basenotes[i] + "5";
           
           
        }
     
    }
   
   
  return false;
   
}
 
// ------------------------------------------

function initSynth(){
   
   synth = new Tone.PolySynth();
   synth.polyphony = 32;
    
   synth.options.envelope.attack = .03; 
   synth.options.envelope.release = .5;
   synth.options.envelope.sustain = .2;
   synth.options.envelope.decay = .1;

   synth.options.oscillator.modulationType = "sine";
   synth.options.oscillator.harmonicity = .5;
   synth.options.oscillator.type = "sine2";
   synth.options.oscillator.partials = [.9,.701 ];
   synth.volume.value = -8;
   synth.chain( Tone.Destination);
 
}