Go Down

Topic: Mux and Analog Ins (Read 843 times) previous topic - next topic

johngovan

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.

Grumpy_Mike

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

johngovan

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?

johngovan

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

Grumpy_Mike

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.

johngovan

i'm sorry. i forgot.  :(

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

Code: [Select]
//  *****************************************************************************************************************
//  *                                                                                                               *
//  *                                         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);
}




Grumpy_Mike

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.

johngovan

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

Grumpy_Mike

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.

johngovan

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

Code: [Select]
//  *****************************************************************************************************************
//  *                                                                                                               *
//  *                                         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

Grumpy_Mike

Code: [Select]
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:-

Code: [Select]

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.

johngovan

#11
Aug 05, 2014, 01:17 pm Last Edit: Aug 05, 2014, 01:21 pm by johngovan Reason: 1
Code: [Select]
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?

Grumpy_Mike

You have to say what it does.

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

johngovan

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. :(


Grumpy_Mike

Code: [Select]
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?

Go Up