piezo to midi code help wanted !

hi dear people,

first question as arduino newbee :
i want to use arduino with 4 piezos to trigger sounds via midi in ableton live.
the soldering part for the piezos & midi out is finished
( using >>> http://itp.nyu.edu/physcomp/Labs/MIDIOutput ),
but i desperately need some help with the coding part ,
cause i got ? errormessages when compiling/verifying till now . ( f.e error expected ',' or '...' before numeric constant )
anyone willing to help ???????
below 's the code i have till now ( only for 1 piëzo till now ) :
thanks !!!

// 4 piezos to midi

int ledPin = 13 // Led connected to digital pin 13
int piezo1 = 0 // Piezo 1 connected to analog 0
byte val = 0 // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status , to toglle the light
int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not

void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as OUTPUT
Serial.begin(31250); // set MIDI baud rate
}

void loop()
{
// deal with first piezo
val = analogRead(piezo1);
if (val >= THRESHOLD) {
noteOn (0x90 , note, 0x40);
digitalWrite(ledPin, HIGH);
delay(100);
}
// plays a MIDInote doesn't check to see that cmd is greater than 127 or that data values are less than 127
void noteOn(char cmd, char data1,char data 2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}

now it works:

int ledPin = 13   ;    // Led connected to digital pin 13 
int piezo1 = 0   ;     // Piezo 1 connected to analog 0 
byte val = 0     ;     // variable to store the value read from the sensor pin 
int statePin = 0; // variable used to store the last LED status , to toglle the light 
byte THRESHOLD = 100;  // threshold value to decide when the detected sound is a knock or not 
 
void setup()  
{ 
 pinMode(ledPin, OUTPUT); // declare the ledPin  as OUTPUT 
 Serial.begin(31250);  // set MIDI baud rate  
}  
 
void loop(){ 
 // deal with first piezo 
  val = analogRead(piezo1)/4;  
  if (val >= THRESHOLD) {  
   noteOn (144 , 10, 100); 
    digitalWrite(ledPin, HIGH); 
    delay(100); 
  }  
}


// plays a MIDInote doesn't check to see that cmd is greater  than 127 or that data values are less than 127 
void noteOn(char cmd, char data1,char data2) { 
  Serial.print(cmd, BYTE); 
  Serial.print(data1, BYTE); 
   Serial.print(data2, BYTE); 
}

just a typo: you wrote 'data 2' instead of the variable name data2. and added a few semicolons at the bginning.

//kuk

EDIT: i probably changed more than needed to be done. don't mind this, took me some time to see the problem

welcome to this forum and the Arduino.

Fixing the folllowing should let the sketch compile:

  • semicolons missing after the declarations of your variables at the begining of the program
  • the variable note is being passed in the call to noteOn but is never declared
  • missing closing bracket } in loop()
  • there is an unintended space in the third parameter of noteOn, it should be char data2

edit: kuk beat me to it.

hey thanks a lot for the fast help !
the first piezo worked out fine indeed ,
now i've hooked up 4 piezos and adapted the code
( added the 3 piezos and changed midi note number )
but i must be wrong somewhere since i get an error message at the end again when compiling/verifying...
( >in function 'void loop()': error a function-definition is not allowed here before '{' token

  • void noteOn(char cmd, char data1,char data2) is highlighted

any suggestions ? stupid things i'm forgetting ? non-stupid things i'm forgetting ?
thanks!

here's the code :

// 4 piezos to midi

int ledPin = 13 ; // Led connected to digital pin 13
int piezo1 = 0 ; // Piezo 1 connected to analog 0
byte val = 0 ; // variable to store the value read from the sensor pin
int piezo2 = 1 ; // Piezo 1 connected to analog 1
byte val2 = 0 ; // variable to store the value read from piezo2pin
int piezo3 = 2 ; // Piezo 3 connected to analog 2
byte val3 = 0 ; // variable to store the value read from piezo3pin
int piezo4 = 3 ; // Piezo 4 connected to analog 3
byte val4 = 0 ; // variable to store the value read from piezo4pin
int statePin = 0; // variable used to store the last LED status , to toglle the light
byte THRESHOLD = 80; // threshold value to decide when the detected sound is a knock or not

void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as OUTPUT
Serial.begin(31250); // set MIDI baud rate
}

void loop(){
// deal with first piezo
val = analogRead(piezo1)/4;
if (val >= THRESHOLD) {
noteOn (144 , 10, 100);
delay(180);
noteOn (144 , 10, 0);
digitalWrite(ledPin, HIGH);
delay(100);

// deal with second piezo
val2 = analogRead(piezo2)/4;
if (val2 >= THRESHOLD) {
noteOn (144 , 12, 100);
delay(180);
noteOn (144 , 12, 0);
digitalWrite(ledPin, HIGH);
delay(100);

// deal with third piezo
val3 = analogRead(piezo3)/4;
if (val3 >= THRESHOLD) {
noteOn (144 , 14, 100);
delay(180);
noteOn (144 , 14, 0);
digitalWrite(ledPin, HIGH);
delay(100);

// deal with fourt piezo
val4 = analogRead(piezo4)/4;
if (val4 >= THRESHOLD) {
noteOn (144 , 16, 100);
delay(180);
noteOn (144 , 16, 0);
digitalWrite(ledPin, HIGH);
delay(100);
}
}

// plays a MIDInote doesn't check to see that cmd is greater than 127 or that data values are less than 127
void noteOn(char cmd, char data1,char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}

use the #Button when posting code
you get this box then, which makes it easier to read formatted code

you have to close your {blocks of code} like

if (someCondition==true){              //<-start  conditinal block {
    //do something here
}                                                  //-> closing the block }

or

void loop(){                                       //<-open loop block {
    if (someCondition==true){              //<-start  conditinal block {
        //do something here
    }                                                  //-> closing the if block }
}                                                      //-> closing the loop block }

that too was part of the problem in your first example.

thanks a lot kuk !
now that's filed under 'stupid things i forgot' then :slight_smile:

 and this too hopefully

I am planning to do the same kind of thing. does the same data that is transmitted to the serial Tx pin also get sent via the usb serial?
I plan on attaching my device (when i make it) to cubase via the usb (using usb serial midi port drivers)

i'm also a noob at this and dont know how to code it yet.. how would i make it into switches instead of peizos for a mini keyboard device?

I am planning to do the same kind of thing. does the same data that is transmitted to the serial Tx pin also get sent via the usb serial?
I plan on attaching my device (when i make it) to cubase via the usb (using usb serial midi port drivers)

i'm also a noob at this and dont know how to code it yet.. how would i make it into switches instead of peizos for a mini keyboard device?

Yes, the USB connection works great for serial input and output

To use switches instead of piezo devices is easy. You would replace:

  val = analogRead(piezo1)/4;   
  if (val >= THRESHOLD) 
    noteOn (144 , 10, 100);

With

  If(digitalRead(switch1) )
     noteOn (144 , 10, 100);

And as has been mentioned above, make sure that you add braces {} as needed.

kool thnx :slight_smile: i'll see how it goes :slight_smile: