Pages: [1]   Go Down
Author Topic: piezo to midi code help wanted !  (Read 1407 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
  
  

Logged

berlin
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now it works:
Code:
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
« Last Edit: December 18, 2007, 08:09:04 am by leKuk » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 18, 2007, 08:03:21 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
 

 

    
Logged

berlin
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


you have to close your {blocks of code} like
Code:

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


or

Code:

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.




Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
and this too hopefully
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

B-dui in creation.

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
Code:
 val = analogRead(piezo1)/4;  
  if (val >= THRESHOLD)
    noteOn (144 , 10, 100);  


With
Code:
 If(digitalRead(switch1) )
     noteOn (144 , 10, 100);


And as has been mentioned above, make sure that you add braces {} as needed.
« Last Edit: December 18, 2007, 02:29:44 pm by mem » Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

kool thnx smiley i'll see how it goes smiley
Logged

B-dui in creation.

Pages: [1]   Go Up
Jump to: