Arduino CapSense-based Synth/Piano with custom sounds via Processing

Hi there,

I’ve recently built a “fruit piano” of sorts using CapSense Library (takes capacity value in a circuit of two pins and a resistor in between, emits a tone if value exceeds threshold)

Here’s about what it looks like:

The code is really very simple:

#include <CapacitiveSensor.h>
#define SND_PIN 13 
int range = 700; 
byte button; 
int freq; 

CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);        
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);     
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2,5);      
CapacitiveSensor   cs_2_6 = CapacitiveSensor(2,6);
CapacitiveSensor   cs_2_7 = CapacitiveSensor(2,7);
void setup()                    
{

   
   Serial.begin(9600);

}

void loop()                    
{
   
    long total1 =  cs_2_3.capacitiveSensor(30);
    long total2 =  cs_2_4.capacitiveSensor(30);
    long total3 =  cs_2_5.capacitiveSensor(30);
    long total4 =  cs_2_6.capacitiveSensor(30);
    long total5 =  cs_2_7.capacitiveSensor(30);
    
      button = 0;
  if (total1 > range) button |= 1; 
  if (total2 > range) button |= 2;
  if (total3 > range) button |= 3;
  if (total4 > range) button |= 4;
  if (total5 > range) button |= 5;
  
  switch (button) {
   case 1: freq = 523.25; break; // C
   case 2: freq = 587.33; break; // D
   case 3: freq = 659.26; break; // E
   case 4: freq = 698.46; break; // F
   case 5: freq = 783.99; break; // G
   case 6: freq = 700; break; //
   case 7: freq = 800; break; //
   default: freq = 0;

  


Serial.begin(9600);    
    
Serial.print("\t");                    // tab character for debug window spacing

Serial.print(total1);                  // print sensor output 1
Serial.print("\t");
Serial.print(total2);                  // print sensor output 2
Serial.print("\t");
Serial.println(total3);                // print sensor output 3

  }
 freq? tone(SND_PIN, freq) : noTone(SND_PIN);
   
   
}

What I’d like to do is enhance the “piano” with custom sounds, probably by using the serial connection with the laptop, probably with processing - make a piece of software capable of reading total1, total2 etc. and play sounds accordingly, but before I delve into it, I’ve got a ton of questions:

  1. My project is definitely not the first of its kind, someone must have already come up with a tool to play sounds from serial port code, if you have that or know where to find it, that’d save me lots of time.

  2. Feasibility - it is entirely possible to do what I mean to do, right?

  3. If there is no software made for my purposes already, can you give me a nod as to which functions I should use in Processing, I’m totally new to the whole thing.

Thank you.
Every little bit helps

Best regards,
Me

  1. Feasibility - it is entirely possible to do what I mean to do, right?

Yes no problem

The simplest way is to use MIDI and then use the MIDI messages to trigger sounds in your PC. Look at an application called "hairless", this software runs on your PC and converts the serial output from the Arduino into an internal MIDI bus in your PC. The you just direct this but towards your MIDI player or sound generator. I use Garage Band to do this.

This project shows how to do it, all be it with different forms of triggering the notes.
http://www.thebox.myzen.co.uk/Hardware/Pendulum.html

Serial.begin() should be called only once, in setup().

Please fix the misplaced curly braces, and use Format (CTRL+T) to get properly indented blocks.

Your code will behave unexpectedly when multiple sensors are touched at the same time. It could be simplified into something like

  freq = 0;
  if (total1 > range) freq = 523.25;
  else if (total2 > range) ...
  ...
  if (freq > 0) tone(SND_PIN, freq);
  else noTone(SND_PIN);

Big thanks to both of you, guys, sorry for the late reply.