Hello all,
I am using 4 piezos (and more comings) connected on a muxshield on analog inputs 0 to 3.
My code was working but I now encounter a problem.
If one of my sensor is unplugged e.g. pin 3 and I push the one on pin 2 a voltage is detected also on pin3. If I replug everything goes back to normal.
I have tried to remove the mux shield and changed the code to read arduino analog inputs and I have the same issue.
The piezo are connected in parrallel with a 5,1 zener and a 1Mohm resistor.
I have now tried to input 5volt on pin 2 with pin 3 unplugged and get the same issue (the consequence here is two midi notes playing at the same time.
I think I have burnt something on my UNO board...maybe one of you have another idea?
here is my code.
Thanks a lot,
Rok
//*******************************************************************************************************************
// User settable variables
//exemple avec millis
//http://forum.arduino.cc/index.php/topic,76140.msg574940.html#msg574940
//*******************************************************************************************************************
#include <MuxShield.h> // for the muxshield library (optional?)
unsigned char PadNote[6] = {52,53,54,55,56,57}; // MIDI notes from 0 to 127 (Mid C = 60)
unsigned char CurrentPadNote[6] = {0,0,0,0,0,0}; // MIDI notes from 0 to 127 (Mid C = 60)
int PadCutOff[6] = {600,600,600,600,600,600}; // Minimum Analog value to cause a drum hit
int MaxPlayTime[6] = {90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed
#define midichannel 0; // MIDI channel from 0 to 15 (+1 in "real world")
//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[6] = {0,0,0,0,0,0}; // Array of flags of pad currently playing
int PinPlayTime[6] = {0,0,0,0,0,0}; // Counter since pad started to play
unsigned char status;
int pin = 0;
int hitavg = 0;
//*******************************************************************************************************************
// Multitasking
//*******************************************************************************************************************
// Time periods of blinks in milliseconds (1000 milliseconds to a second).
// Time variable and constants are always unsigned long
const unsigned long MidiOutSendInterval = 1UL ;
const unsigned long MidiInReadInterval = 50UL ;
// Variable holding the timer value so far. One for each "Timer"
unsigned long MidiOutSendTimer = 0 ;
unsigned long MidiInReadTimer = 0 ;
//*******************************************************************************************************************
// Midi In Variables
//*******************************************************************************************************************
byte statusIN;
byte PitchIN;
byte VelocityIN;
//*******************************************************************************************************************
//Initialize the Mux Shield
//*******************************************************************************************************************
MuxShield muxShield;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup()
{
//Set I/O 1, as analog inputs
muxShield.setMode(1,ANALOG_IN);
MidiOutSendTimer = millis () ;
MidiInReadTimer = millis () ;
// connect to the serial port 31250 for MIDI communication
Serial.begin(31250);
//Serial.begin(9600);
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop() {
// First, check if it is time to send Midi
if ( (millis () - MidiOutSendTimer) >= MidiOutSendInterval ) {
for(int pin=0; pin < 4; pin++)
{
//hitavg = analogRead(pin);
// read the input pin, classic input version
hitavg = muxShield.analogReadMS(1,pin); // read muxShield analog value on pin pin, row 1
if((hitavg > PadCutOff[pin]))
{
if((activePad[pin] == false))
{
if((CurrentPadNote[pin] != PadNote[pin])){
CurrentPadNote[pin] = PadNote[pin];
}
MIDI_TX(144,PadNote[pin],127);
PinPlayTime[pin] = 0;
activePad[pin] = true;
}
else
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
}
}
else if((activePad[pin] == true))
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
if(PinPlayTime[pin] > MaxPlayTime[pin])
{
activePad[pin] = false;
MIDI_TX(128,CurrentPadNote[pin],127);
}
}
}
// end of the loop Reset timer with current time at end
MidiOutSendTimer = millis () ;
}
//Check if is time to read midi in
if ( (millis () - MidiInReadTimer) >= MidiInReadInterval ) {
// ok it is time
checkMIDI();
// end of the loop Reset timer with current time at end
MidiInReadTimer = millis () ;
}
}
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status = MESSAGE + midichannel;
Serial.write(status);
Serial.write(PITCH);
Serial.write(VELOCITY);
}
//*******************************************************************************************************************
// Receive MIDI Message
//*******************************************************************************************************************
void checkMIDI(){
do{
if (Serial.available()){
statusIN = Serial.read();//read first byte
PitchIN = Serial.read();//read next byte
VelocityIN = Serial.read();//read final byte
if (statusIN == 145 && PitchIN != 0 && VelocityIN != 0){//if Chan 2 note on
digitalWrite(13,HIGH);//turn on led
//assign value to pad note
PadNote[0] = PitchIN;
} else {
digitalWrite(13,LOW);//turn of led
}
}
}
while (Serial.available() > 24);//when three bytes available
}