Setup and initialize GPIOAB ?

by pitchoilcan on Mon May 21, 2012 9:23 am

Code: Select all
Adafruit_MCP23017 mcp; // created an  object

so I need to add:
Code: Select all
int pattern =0; //create an interger

and
Code: Select all
mcp.writeGPIOAB(0, pattern); // write to port//pin

then  'if' statements:
Code: Select all
if(PadNote[i] == 64) pattern |= 0x05; //condition

=============
working it out......
=============
Code: Select all
if(PadNote[i] == 60) pattern |= 0x01;

               // the same goes for turning the LED off:-

if(PadNote[i] == 60) pattern &= ~0x01;

#include <Wire.h>
#include "Adafruit_MCP23017.h"

ledBits; // create  object

 ledBits.initialize(); // set all registers to default

 ledBits.portMode(0, 0b0000000000000000); // set all pins on chip 0 to output

"The Honeymooners: Ralph Kramden, Inc. (#1.19)" (1956)
Ralph Kramden: [to Norton] I don't know why a man of your age watches birds.
Ed Norton: Why shouldn't I watch birds? They watch me, don't they?
Ralph Kramden: The only bird that watches you, Norton, is a woodpecker.

Ed Norton: Like we say in the sewer, "time and tide wait for no man".
Last edited by pitchoilcan on Tue May 22, 2012 1:09 pm, edited 6 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

click and clack, it's like car talk for arduino....

by pitchoilcan on Mon May 21, 2012 9:50 am

/////////
//mcp.begin();      // set all registers to default
mcp.begin(0x05, 01010101); // set all registers to GPIOAB
//mcp.pinMode(0, OUTPUT);
mcp.pinMode(0, pattern); // set all pins on chip
On power up the device may be configured by sequentially writing to all of the registers

"The Honeymooners: Dial J for Janitor (#1.38)" (1956)
Norton: Well anyway, I had this headache and I couldn't get rid of it see, so I went to the Navy doctor. So he examines me and says I have pressure on the brain, so he should remove it. So well, I took a lot of kidding from the boys on the ship you know, 'cause afterwards they said, "What did he do, remove the pressure or the brain!"
Ralph: Don't ask me, 'cause I know what he removed!

Alice: So, how's your plumbing?
Norton: Still broken. I never thought I'd work eight hours in a sewer and come home wanting to see water!
Last edited by pitchoilcan on Tue May 22, 2012 12:47 pm, edited 2 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

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

by adafruit_support_rick on Mon May 21, 2012 10:28 am

mcp.begin initializes the library with the address of the MCP23017 chip you want to talk to. It has nothing to do with gpioab.

The chip's address is determined by the wiring on the chip's pins 15, 16, and 17. The datasheet shows the locations of these pins:
http://www.adafruit.com/datasheets/mcp23017.pdf.
If they are all wired to ground, the chip's address is 0. If 15 is wired to Vcc while 16 and 17 are ground, then the chip's address is 1. So the three pins form a 3-bit binary number.

NOTE: Here, "pin" refers to the actual physical pins sticking out of the chip, not a software "pin" as used in the library.

ALSO NOTE: Every MCP23017 in your circuit MUST have a different address!

So, let's say your MCP23017 is wired for address 3. You would call
Code: Select all
mcp.begin(3);


Okay, from now on, "pin" refers to the software pins as used by the library:
There is no library call to set the mode of all the pins all at once, so you have to do it one-at-a-time with calls to mcp.pinMode.

You've got 16 LEDs attached to your chip, right? So you want to set all 16 pins to OUTPUT. Do it with a loop:
Code: Select all
for (int i=0; i < 16; i++) {
    mcp.pinMode(i,OUTPUT);
}


Now, you can start turning your LEDs on and off.

From your earlier post, I get the idea that you have some sort of array called PadNote, and you want to map a PadNote value of 60 to LED 0, 61 to LED 1, 62 to LED 2, etc. Right? If so, then you can simply do something like this, instead of a big bunch of if statements:
Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
    mcp.digitalWrite(PadNote[i]-60, HIGH);
}


Now, if you absolutely INSIST on using writeGPIOAB instead of digitalWrite, then you would have to do something like this instead:
Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
   pattern |= (1 << ((PadNote[i]-60);
   mcp.writeGPIOAB(pattern);
}


Turn the LEDs off this way:

Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
    mcp.digitalWrite(PadNote[i]-60, LOW);
}


- OR -

Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
   pattern &= ~(1 << ((PadNote[i]-60);
   mcp.writeGPIOAB(pattern);
}
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

ITM New York

by pitchoilcan on Mon May 21, 2012 11:15 am

THANK YOU :shock: :D
"The Honeymooners: The Safety Award (#1.34)" (1956)
Ed Norton: [to Ralph] Look, just don't get upset. You're gettin' all upset now. Let's calm down and look nice when we get down there. There's no sense in getting upset. Now listen, the boys in the sewer, there, when we get upset we got a little motto... a little saying that gives us comfort in time of need. Maybe I can pass it on to you. May I favor you with this little ode? "When the tides of life turn against you, and the current upsets your boat. Don't waste those tears on what might have been, just lay on your back and float.

ITM NYC, Second to none!
"In the morning to all the to all of the boots on the ground, feets in the air, farmers in the fields, guns to the head, wings in the skies, foots in the oceans, bums on seats, fingers in the dikes, rats in the sewers, asses in the air, checks in the mail, ankles in the straps, drones in the sky, bakers in the banned, dung in the pit, truckers on the highways, astronauts manning the moon and mars base stations, hams on the air, drones overhead, earthquake machines in the oceans, aliens underground, moon bases on the dark side, spooks in the wires, bankers in the government, special ops in the sand, clones in the White House, shills in the congress, shills on the hill, stooges in the media, hackers in the ballot machines, sysadmins on the inside, producers on the job, knights in the service, formulas in the propagations, memes in the cold, fractals in the universe and all of our human resources in the chatroom!"
Last edited by pitchoilcan on Tue May 22, 2012 12:41 pm, edited 7 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

pondering..."in the same unbroken chain of execution so yada yada..."

by pitchoilcan on Tue May 22, 2012 7:22 am

ask an engineer
if(PadNote == 60) pattern |= 0x00; yada yada is later followed by if(PadNote == 60) pattern &= ~0x00; //OF COURSE it will never activate a pin


[Ralph and Norton go to the IRS and sees Richard Puder]
Richard Puder: Which one of you gentlemen is Kramden?
Norton: He's Kramden, I'm clean.
Last edited by pitchoilcan on Tue May 22, 2012 9:42 am, edited 4 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

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

by adafruit_support_rick on Tue May 22, 2012 7:47 am

Well, obviously, you don't put those two statements in line. Someplace, you have to be deciding whether to turn on a light or turn it off.

Your input is, what, some sort of midi thing? So, in addition to telling you what note to play, it must be doing one of two things:
1) telling you the duration of the note, or
2) telling you when the note is struck and telling you when the note is released.

Case 2) is easy - when the note is struck you do
Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
    mcp.digitalWrite(PadNote[i]-60, HIGH);
}
, and when the note is released you do
Code: Select all
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
    mcp.digitalWrite(PadNote[i]-60, LOW);
}


If it's case 1), then you need to time the note. So you make an array that's just like a keyboard - one location for each note. When a note is struck, you take the note duration and store it in the array.
Now, what you can do is continuously loop through this array and look at each number stored in it. If the number is not zero, turn on the LED and subtract 1. If the number is zero, turn off the LED.
Put in some sort of delay at the end of the loop so you get the timing right, according to whatever time units you are getting from midi.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

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

by adafruit_support_rick on Tue May 22, 2012 7:56 am

By the way - the arduino forum guys seem to think that you're using a different library. I think you want to stick to one forum or the other. Cross-posting is only going to confuse you.
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

New approach | Ask an engineer

by pitchoilcan on Tue May 22, 2012 8:38 am

Ask an engineer:
Code: Select all
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);
}

Now all LEDs just flicker all at once, continuously. Perhaps I've in-artfully pasted the new conditional "ifs", perhaps not :| .
Pondering.........
Still it's progress :wink:
"The Honeymooners: Pal O' Mine (#1.8)" (1955)
Ed Norton: [about Ralph getting Jim McKeever's ring stuck on his finger] That's like King Farouk slipping into Gary Cooper's bathing suit!

Alice: [looking at a ring Norton has] Hey, that's a very handsome ring. Who's it for?
Ed Norton: Jim McKeever. He gave me my start in the sewers. My first push, so to speak. We recently appointed him foreman, so I thought it'd be nice to give him a little something.
Trixie Norton: Ed was all for buying him a pair of suspenders, but I thought a ring would be more suitable for the occasion.
Ed Norton: Well, I happen to know that Jim McKeever has a weakness for red suspenders.
Alice: Oh, well, I think Trixie's right, Ed. A man would rather have a ring than suspenders.
Ed Norton: Not if his pants are always falling down.
Attachments
passeetryagain_mcp_ada_II.txt
MAKE: making progresses
(9.41 KiB) Downloaded 60 times
Last edited by pitchoilcan on Tue May 22, 2012 9:56 am, edited 3 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

Important Note: [NYC, second only to silicon valley]

by pitchoilcan on Tue May 22, 2012 9:09 am

I always thought adafruit industries should have a storefront in NYC. because there are only a few place left to get analog stuff in New York there is one Chinese man on Canal St, there is Argo closeout/surplus also on Canal St there is "chips and things" on Fulton St. but there aren't much into analog anymore. My point is Apart from RadioShack there are very options in the city. So I hope by the time Makerfaire NYC rolls around you might announce a brick an mortar store. Kinda like a genius bar for arduinoheads.
Last edited by pitchoilcan on Tue May 22, 2012 12:40 pm, edited 3 times in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

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

by adafruit_support_rick on Tue May 22, 2012 9:39 am

that's because your function turns them on and then turns them off again right away.
Try the attached file.
Attachments
passeetryagain_mcp_ada_II.c
(8.52 KiB) Downloaded 60 times
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

like a fish out of water

by pitchoilcan on Tue May 22, 2012 9:48 am

Ed Norton: I know just how you feel because I went through the same thing two or three years ago when they laid me off from the sewer. I felt just like a fish out of water.
Last edited by pitchoilcan on Tue May 22, 2012 12:44 pm, edited 1 time in total.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

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

by adafruit_support_rick on Tue May 22, 2012 9:57 am

Bang! Zoom! To the moon, oilcan!
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Yikes whatdidIdonow?

by pitchoilcan on Tue May 22, 2012 10:12 am

Ask an engineer:
ALL LEDs remain HIGH
taking a look-see.......
Ed Norton: Ol' Ed Norton, reliable old Ed Norton, working seventeen years in the sewer. And now everything's down the drain!

:roll:
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

it's a wonderful town, New York is

by pitchoilcan on Tue May 22, 2012 1:01 pm

that's because your function turns them on and then turns them off again right away.
are you saying that because of the multiplexers are switching make the LEDs unstable, is it possible to successfully write the LEDs, should I consider 595's and if so what are the pros and cons? After having invested this must time with this chip i'll like to see it through, but I'll also like to be done.
User avatar
pitchoilcan
 
Posts: 22
Joined: Tue Apr 24, 2012 9:18 am

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

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

No - you were literally turning them on and then off in your code:
Code: Select all
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
User avatar
adafruit_support_rick
 
Posts: 8585
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY