[Help needed!] using Adafruit-MCP23017-Arduino-Library
Moderators: adafruit_support_bill, adafruit

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by adafruit_support_rick on Tue May 22, 2012 2:35 pm

No - you were literally turning them on and then off in your code:
Code: Select all | TOGGLE FULL SIZE
void readSensors(int analogPin){
    //This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
  for (int i=0; i<16; i++){
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2); 
    digitalWrite(CONTROL2, (i&3)>>1); 
    digitalWrite(CONTROL3, (i&1));     

    //Read and store the input value at a location in the array
    if(analogPin==0){
      mux0array[i] = analogRead(analogPin);
    }
     if(analogPin==1){
      mux1array[i] = analogRead(analogPin);
    }
     if(analogPin==2){
      mux2array[i] = analogRead(analogPin);
    }
   if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
      mcp.digitalWrite(PadNote[i]-60, HIGH);
  }
  if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
    mcp.digitalWrite(PadNote[i]-60, LOW);
  }
  }
}

So, if PadNote[ i] was, say, 65, it would pass the first test and turn on the light. Then it will immediately pass the next test and turn off the light. So your lights flicker.

PadNote is a contant array - the values in it never change, and it has nothing to do with the current input. The current input is in mux0array. That's what readSensors() does - it reads your drumpads and puts the current analog value of each pad into muxarray.

The code in checkSensors() really doesn't make any sense to me. Did you get this out of some example code? It looks like it wants to do some sort of average of 8 readings on each pad to try to calculate some sort of touch velocity. After that, it sends the note and velocity to MIDI_TX.

I don't think the hitavg calculation is doing what you want it to do. It doesn't look right to me. It is NOT calculating an average of 8 consecutive readings.

After that, you seem to be trying to compute an ON time for the note. Again, I don't think you're doing this the right way. However, you do set activePad[i] to true to indicate that the note is playing. Later on, when you think the note has timed out, you send a different midi command and set activePad[i] to false to indicate that the note is no longer playing.

All I did was to tie the digitalWrite HIGH/LOW calls to the state of activePad[i]. When activePad[i] is true, the light is on. When activePad[i] is false, the light is off.

However, your notes are NEVER going to time out, because your loop ALWAYS sets PinPlayTime[pad] = 0; Every time you go through the loop, you reset the time count, so it will never ever be greater than MaxPlayTime[pad].

And, I don't think you want to be ONLY checking against MaxPlayTime. Don't you want your notes to have different durations?

Bottom line: There is nothing wrong with the LED code in here. It is not your problem. Your problem is that you aren't doing the right things with muxarray, hitavg, and PinPlayTime

adafruit_support_rick
 
Posts: 12827
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Bits and pieces | All together now

by pitchoilcan on Tue May 22, 2012 3:07 pm

http://blog.georgmill.de/2011/03/22/e-drumset-selbst-gebaut/
http://sourceforge.net/projects/yaamidrum/
Thanks
I think it's because of the way the piezo give you a quick reading and then falls rapidly. However in your rewrite they stay on not flicker.

Grummpy Mike from the other forum did the same thing using mcp23016 and foam sensors,
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html
====
http://todbot.com/blog/2006/10/29/spook ... l-arduino/
http://spikenzielabs.com/SpikenzieLabs/DrumKitKit.html
Knock Knock!
// deal with first piezo, this is kind of a hack
val = analogRead(piezoAPin);
if( val >= PIEZOTHRESHOLD ) {
t=0;
while(analogRead(piezoAPin) >= PIEZOTHRESHOLD/2) {
t++;
}
Last edited by pitchoilcan on Tue May 22, 2012 3:37 pm, edited 4 times in total.
User avatar
pitchoilcan
 
Posts: 23
Joined: Tue Apr 24, 2012 10:18 am

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by adafruit_support_rick on Tue May 22, 2012 3:21 pm

However in you rewrite they stay on not flicker.

Right. That's because your PadPlayTime code doesn't work, and so your loop will turn them on and never turn them off

adafruit_support_rick
 
Posts: 12827
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by adafruit_support_rick on Tue May 22, 2012 3:37 pm

When you edited checkSensors() to get rid of the HiHat stuff, I think you messed up a few else clauses.

Try this:
Code: Select all | TOGGLE FULL SIZE
void checkSensors(int analogPin)
{

   for(int pin=0; pin < 16; pin++)
   {   
      if(analogPin==0)
      {
         hitavg = mux0array[pin];
         pad=pin;
      }
      if(analogPin==1)
      {
         hitavg = mux1array[pin];
         pad=pin+16;
      }
      if(analogPin==2)
      {
         hitavg = mux2array[pin];
         pad=pin+32;
      }

      if((hitavg > PadCutOff[pin]))
      {
         //Serial.writeln("hitavg > PadCutOff[pin])");
         if((activePad[pad] == 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[pad],hitavg);
            PinPlayTime[pad] = 0;
            activePad[pad] = true;
         }
         else
         {
            PinPlayTime[pad] = PinPlayTime[pad] + 1;
         }         
      }
      else
      {
         if((activePad[pad] == true))
         {
            PinPlayTime[pad] = PinPlayTime[pad] + 1;

            if(PinPlayTime[pad] > MaxPlayTime[pad])
            {
               activePad[pad] = false;
               MIDI_TX(128,PadNote[pad],127);
            }
         }
      }
            
      if(analogPin==0)
      {
         if (activePad[pad])
            mcp0.digitalWrite(pad, HIGH);
         else
            mcp0.digitalWrite(pad, LOW);
      }
      if(analogPin==1)
      {
//         if (activePad[pad])
//            mcp1.digitalWrite(pad-16, HIGH);
//         else
//            mcp1.digitalWrite(pad-16, LOW);
      }
      if(analogPin==2)
      {
//         if (activePad[pad])
//            mcp2.digitalWrite(pad-32, HIGH);
//         else
//            mcp2.digitalWrite(pad-32, LOW);
      }
   }
}

adafruit_support_rick
 
Posts: 12827
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by pitchoilcan on Tue May 22, 2012 3:48 pm

Yes that works ! wow :D I'll take a closer look later today (need to go out and bang on the ole can for a while). but in the mean time. Thanks again!
User avatar
pitchoilcan
 
Posts: 23
Joined: Tue Apr 24, 2012 10:18 am

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by adafruit_support_rick on Tue May 22, 2012 5:20 pm

You're welcome! Glad to hear it's working for you! :D

When you're ready to add the other two MCP23017's, wire one of them with pin 15 to +5V and pins 16 & 17 to Gnd (that will be mcp1). Wire another with pin 16 to +5V and pins 15 & 17 to Gnd (that will be mcp2). Then uncomment all the lines that refer to mcp1 and mcp2.

And don't forget to set aside a copy of your working sketch before you start making changes to it!

adafruit_support_rick
 
Posts: 12827
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: [Help needed!] using Adafruit-MCP23017-Arduino-Library

by pitchoilcan on Mon Nov 12, 2012 9:50 pm

I've found that Pin 18 the reset pin must connected to +5V in order for it to work.
User avatar
pitchoilcan
 
Posts: 23
Joined: Tue Apr 24, 2012 10:18 am