Mux and Analog Ins

how to maximize the use of the arduino analog inputs?

i'll explain:
i have two multiplexers that is hooked to A0 and A1
i have a working code that reads the two mux.
but A2-A5 is unused.

my question is how to read the remaining unused analog ins and read the multiplexer at the same time?
so i can still use the A2-A5 to hook more piezo into it.

thank you for the replies in advance.

What is the problem?
Just read the other inputs with an analogue read.

i'll try. thank you so much gramps. i'm new to coding.
my second question is do you have an example how can i control midi note using a potentiometer?

for example, when my pot is in full turn left it gives me midi note 42 and when pot is halfway it gives me note 43 and full turn right and gives me note 44?

its working. :slight_smile: my only problem is the pot code for selecting midi note. please help me. thank you.

Post what you have, say what it does and what you want it to do.
Be sure to post it correctly.
Read the how to use this forum sticky post.

i’m sorry. i forgot. :frowning:

i’ll try to build SpikenzieLabs midi drum trigger.

i’d like to control 1 of the 6 piezos note using a pot so when 1 turn the pot int the left i got a note 42 (snare) and in the middle is note 43(Bass) and in the right note 44(cymbals). something like that.

1 pot controls that 1 piezo note.

here’s the code i use from SpikenzieLabs site

//  *****************************************************************************************************************
//  *                                                                                                               *
//  *                                         SpikenzieLabs.com                                                     *
//  *                                                                                                               *
//  *                                           Drum Kit - Kit                                                      *
//  *                                                                                                               *
//  *                                                                                                               *
//  *****************************************************************************************************************
//
//  BY: MARK DEMERS Copywrite 20009
//  April. 2009
//  VERSION: 1.b
//
//  DESCRIPTION:
//  Arduino analog input used to sense piezo drum hits then sent serialy to processing.
//  
//  Required - Hardware:
//  1. Drum kit - kit (From SpikenzieLabs.com)
//  2. Arduino
//
//  Required - Software:
//  1. Serial MIDI converter
//  2. Garage Band, Ableton Live etc ...
//
// LEGAL:
// This code is provided as is. No guaranties or warranties are given in any form. It is your responsibilty to 
// determine this codes suitability for your application.




//*******************************************************************************************************************
// User settable variables			
//*******************************************************************************************************************

unsigned char PadNote[6] = {60,61,62,63,64,65};         // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[6] = {600,300,600,600,600,600};           // Minimum Analog value to cause a drum hit

int MaxPlayTime[6] = {90,10,10,20,20,20};               // Cycles before a 2nd hit is allowed

#define  midichannel	0;                              // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)





//*******************************************************************************************************************
// 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;

//*******************************************************************************************************************
// Setup			
//*******************************************************************************************************************

void setup() 
{
  Serial.begin(57600);                                  // connect to the serial port 115200
}

//*******************************************************************************************************************
// Main Program			
//*******************************************************************************************************************

void loop() 
{
  for(int pin=0; pin < 6; pin++)
  {
    hitavg = analogRead(pin);                              // read the input pin

    if((hitavg > PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        if(VelocityFlag == true)
        {
//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
          hitavg = (hitavg / 8) -1 ;                                                 // Upper range
        }
        else
        {
          hitavg = 127;
        }

        MIDI_TX(144,PadNote[pin],hitavg); 
        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,PadNote[pin],127); 
      }
    }
  } 
}


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

At the start of each loop read the pot.
Then when it comes to playing the MIDI note, test to see if it is note 6 using an if statement.
If it is then use a map function to change the range of the pot reading into the range of MIDI note numbers you want and send that to the MIDI_TX function instead.

thank you gramps. honestly i have a hard time in making codes. =(

The way this forum works is you make the changes, if they are not successful you post your changed code and we try and correct it or suggest where you are going wrong.

If you have a hard time with this then do some learning first, like going through and understanding the example code in the IDE.

okay. i think i need to study ore about pots.
as of now i’ll try to control note changes using a switch. and here is my new code base on spikenzielabs

//  *****************************************************************************************************************
//  *                                                                                                               *
//  *                                         SpikenzieLabs.com                                                     *
//  *                                                                                                               *
//  *                                           Drum Kit - Kit                                                      *
//  *                                                                                                               *
//  *                                                                                                               *
//  *****************************************************************************************************************
//
//  BY: MARK DEMERS Copywrite 20009
//  April. 2009
//  VERSION: 1.b
//
//  DESCRIPTION:
//  Arduino analog input used to sense piezo drum hits then sent serialy to processing.
//  
//  Required - Hardware:
//  1. Drum kit - kit (From SpikenzieLabs.com)
//  2. Arduino
//
//  Required - Software:
//  1. Serial MIDI converter
//  2. Garage Band, Ableton Live etc ...
//
// LEGAL:
// This code is provided as is. No guaranties or warranties are given in any form. It is your responsibilty to 
// determine this codes suitability for your application.




//*******************************************************************************************************************
// User settable variables			
//*******************************************************************************************************************

unsigned char PadNote[6] = {60,61,47,53,55,120};         // 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,30,30,30,30,30};               // Cycles before a 2nd hit is allowed

#define  midichannel	0;                              // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)

boolean SwitchPin =3;          // Digital pin you are using for switch (don't use 0 or 1)
int  Piezotrigger = 0;                     // Array element (the piezo connector you are using)
                                    // Numbered from from left to right on the Drum Kit
int  BassDrum = 60;               // the MIDI note for the Bass drum sound
int  Snare  = 61;                // the MIDI note for the snare sound





//*******************************************************************************************************************
// 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;


//*******************************************************************************************************************
// Setup			
//*******************************************************************************************************************

void setup() 
{
  Serial.begin(115200);                                  // connect to the serial port 115200
}

//*******************************************************************************************************************
// Main Program			
//*******************************************************************************************************************

void loop() 
{
  
  digitalRead(SwitchPin);
  if(SwitchPin)
  {
    PadNote[BassDrum] = BassDrum;
  }else
  {
   PadNote[Snare] = Snare;
  }
  
  for(int pin=0; pin < 6; pin++)
  {
    hitavg = analogRead(pin);                              // read the input pin

    if((hitavg > PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        if(VelocityFlag == true)
        {
//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
          hitavg = (hitavg / 8) -1 ;                                                 // Upper range
        }
        else
        {
          hitavg = 127;
        }

        MIDI_TX(144,PadNote[pin],hitavg); 
        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,PadNote[pin],127); 
      }
    }
  } 
}


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

but when i try to push the switch, nothing happens

digitalRead(SwitchPin);
  if(SwitchPin)

SwitchPin will always be 3, you do nothing with the value returned from the digital read, you need to do something like this:-

int switchState = digitalRead(SwitchPin);
  if(switchState)

And use switchState when ever you were using SwitchPin.

You are confusing the pin number with what you get when you read that pin number.

unsigned char PadNote[6] = {60,61,47,53,55,120};         // 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,30,30,30,30,30};               // Cycles before a 2nd hit is allowed

#define  midichannel	0;                              // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)

boolean SwitchPin =3;          // Digital pin you are using for switch (don't use 0 or 1)
int  Piezotrigger = 0;                     // Array element (the piezo connector you are using)
                                    // Numbered from from left to right on the Drum Kit
int  BassDrum = 60;               // the MIDI note for the Bass drum sound
int  Snare  = 61;                // the MIDI note for the snare sound





//*******************************************************************************************************************
// 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;


//*******************************************************************************************************************
// Setup			
//*******************************************************************************************************************

void setup() 
{
  Serial.begin(115200);                                  // connect to the serial port 115200
}

//*******************************************************************************************************************
// Main Program			
//*******************************************************************************************************************

void loop() 
{
  
 int switchState = digitalRead(SwitchPin);
  if(SwitchState)
  {
    PadNote[BassDrum] = BassDrum;
  }else
  {
   PadNote[Snare] = Snare;
  }
  
  for(int pin=0; pin < 6; pin++)
  {
    hitavg = analogRead(pin);                              // read the input pin

    if((hitavg > PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        if(VelocityFlag == true)
        {
//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
          hitavg = (hitavg / 8) -1 ;                                                 // Upper range
        }
        else
        {
          hitavg = 127;
        }

        MIDI_TX(144,PadNote[pin],hitavg); 
        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,PadNote[pin],127); 
      }
    }
  } 
}


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

is this correct?

You have to say what it does.

I can't see the it where you subistuted the pot value for the note to play.

i was trying to use a momentary switch to use as a hi-hat close and open note. i can't code a pot to select a note so i decided to use switch first.

like when i press the switch and hit the piezo i got a close hi-hat sound and when i am not pressing the switch and hit that same piezo i get an open hi hat sound. but my code isn't working. :frowning:

int  BassDrum = 60;               // the MIDI note for the Bass drum sound
int  Snare  = 61;                // the MIDI note for the snare sound

On my MU10 60 is a Bongo H and 61 a Bongo L

So if the only thing that makes you think you code is not working is the sound it produces then check your synth.
Also run a MIDI monitor on your main computer and see what numbers are being produced, is it 60 & 61 or is it some other numbers?

i map my midi to sound an open and close hihat in note 60 and 61. but still nothing happens. maybe there's something wrong with my code. the switch didn't select the note in my synth. :frowning:

I am puzzled, you said:-

like when i press the switch and hit the piezo i got a close hi-hat sound and when i am not pressing the switch and hit that same piezo i get an open hi hat sound.

and then you said:-

i map my midi to sound an open and close hihat in note 60 and 61. but still nothing happens.

So one statement says you get a sound and the other says the code is not working. What is it?

i’m sorry gramps. my statements wasn’t clear.

let me start it over again.

what i am trying to make is to add a switch in my arduino drum kit which controls the hi-hat open and close sound. that switch acts like a hi-hat pedal. when i hit the piezo and that switch is unpressed, i got an open hi-hat sound and when i hit the piezo while the switch is pressed i got a close hi-hat sound.

this is the steps i make:

  1. i uploaded the code i’ve posted earlier
  2. open hairless midi serial
  3. open vst host and open the vst plugin i am using (addictive drum)
  4. inside the vst plugin, i’ve change the note 60 to open hihat and 61 to close hihat.
  5. i hit my piezo and i got the open hihat sound
  6. i press the switch and i still get an open hihat sound.

You have this array of notes

unsigned char PadNote[6] = {60,61,47,53,55,120};

So you already have 60 and 61 in that list, presumably on sensors 0 and 1 so I am not sure which entry you want to change.
However this bit is very wrong:-

{
    PadNote[BassDrum] = BassDrum;
  }else
  {
   PadNote[Snare] = Snare;

The thing in the square brackets is the array index. You have the array PadNote defined as 6 elements 0 to 5.
That code sets PadNote array element 60 to the value 60 and PadNote array element 61 to the value 61
As you only have 6 elements this code is going to write over a random area of memory. If you want the last sensor to change between the two sounds you need to use:-

{
    PadNote[5] = BassDrum;
  }else
  {
   PadNote[5] = Snare;

Then the note will change if the sensor is triggered while the button is pressed, but as I said you already have those two notes assigned to the first two sensors.

boolean HiHatSwitchPin = 3;          // Digital pin you are using for switch (don't use 0 or 1)
                                    // Numbered from from left to right on the Drum Kit
int  ClosedNote = 60;               // the MIDI note for the closed sound
int  OpenNote  = 61;                // the MIDI note for the closed sound


//*******************************************************************************************************************
// User settable variables			
//*******************************************************************************************************************

unsigned char PadNote[6] = {55,57,47,53,55,60};         // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[6] = {700,700,700,700,700,700};           // Minimum Analog value to cause a drum hit

int MaxPlayTime[6] = {90,30,30,30,30,30};               // Cycles before a 2nd hit is allowed

#define  midichannel	0;                              // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)





//*******************************************************************************************************************
// 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;


//*******************************************************************************************************************
// Setup			
//*******************************************************************************************************************

void setup() 
{
  Serial.begin(115200);   // connect to the serial port 115200
  
}

//*******************************************************************************************************************
// Main Program			
//*******************************************************************************************************************

void loop() 
{
  digitalRead(HiHatSwitchPin);
  if(HiHatSwitchPin)
  {
    PadNote [5] = ClosedNote;
  }else
  {
   PadNote [5] = OpenNote;
  }
  
  for(int pin=0; pin < 6; pin++)
  {
    hitavg = analogRead(pin);                              // read the input pin

    if((hitavg > PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        if(VelocityFlag == true)
        {
//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
          hitavg = (hitavg / 8) -1 ;                                                 // Upper range
        }
        else
        {
          hitavg = 127;
        }

        MIDI_TX(144,PadNote[pin],hitavg); 
        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,PadNote[pin],127); 
      }
    }
  } 
}


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