piezo to midi code help wanted !

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.