Embarrassing Keypad Library/Code Question

Status
Not open for further replies.

DrDarin

Member
Hardware: Teensy LC, custom pcbs (one to seat Teensy, another for reed switches)
Libraries: Keypad, MIDI
Output Conundrum:
ON
6
OFF
6
ON
4
OFF
4
ON
2
OFF
2

Output generated by holding magnet close to reed switch. The switch wiring configuration is typical rows/columns.

CODE:

HTML:
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
#include <Keypad.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const byte ROWS = 8;
const byte COLS = 8;
//Matrix begins two octaves below Middle C (C4) - MIDI note 60
char keys[ROWS][COLS] = {
{'36','37','38','39','40','41','42','43'}, 
{'44','45','46','47','48','49','50','51'},
{'52','53','54','55','56','57','58','59'},
{'60','61','62','63','64','65','66','67'},
{'68','69','70','71','72','73','74','75'},
{'76','77','78','79','80','81','82','83'},
{'84','85','86','87','88','89','90','91'},
{'92','93','94','95','96','97','98','99'}
};

byte rowPins[ROWS] = {1,2,3,4,5,6,7,8};
byte colPins[COLS] = {13,14,15,16,17,18,19,20};

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
    MIDI.begin();
}

void loop() {
    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)
        {
            if (kpd.key[i].stateChanged)
            {
                switch (kpd.key[i].kstate) {
                    case PRESSED:
                      MIDI.sendNoteOn(kpd.key[i].kchar,127,1);
                      Serial.println(" ON ");
                      Serial.println(kpd.key[i].kchar);
                    break;
                    case RELEASED:
                      MIDI.sendNoteOff(kpd.key[i].kchar,127,1);
                      Serial.println(" OFF ");
                      Serial.println(kpd.key[i].kchar);
                    break;
                    case HOLD:
                    break;
                    case IDLE:
                    break;
                }
            }
        }
    }
}

OUTPUT SOUGHT: What I'm really looking for are outputs according to the array figures. The numbers in the array represent MIDI note numbers. For my purposes, I only need on/off with no other MIDI info. So, the first ON in the output should present 36, the second 37, the third 38, etc...

Now, the weird thing is if I define the array as int and convert the kchar values to ints in the printlns, it reads 36, 38, 40, 42... when it should read 36, 37, 38, 39...

What am I doing wrong/missing here?!?
 
Sorry, should have also stated that removing the single quotes results in an ASCII representation of the numbers. So, the output looks like:

ON
$
OFF
$
ON
,
OFF
,
ON
4
OFF
4
 
Thats correct. The asc-ii value for "$" is 36.
If you want it "human-readable", rethink your println.. you tell it to print a char. And that's what you get:)

hint: you want to print an "int", not a "char".
 
Simple prints like this will show the results:
Code:
  char foo = '65';

  Serial.println( '65' );
  Serial.println( 65 );
  Serial.println( foo );
  foo = 65;
  Serial.println( foo );
  Serial.println( (int)foo );

gives:
13877
65
5
A
65
 
When I convert the char values to an int in the printlns and leave it char in the array def, they're separated by 4 digits so the output becomes 36, 40, 44...

When I define the array as int and also convert to int in the printlns, I get the two digits spacing - 36, 38, 40...

I'll keep playing with the printlns...thanks. I really do appreciate your willingness to help. Darin
 
When I convert to int in the println, it shows 36, then 38, then 40, but only if I also define the array as int.

If I define the array as char and convert to int in the println, it shows 36, 44, 52, etc.
 
Turns out it was a shitty board design. I got my columns and rows mixed up, therefore each were consolidated wrong at the cable ends. My very, very bad - and so sorry to have wasted your time. You guys rock.
 
Actually...

Turns out it was a shitty board design. I got my columns and rows mixed up, therefore each were consolidated wrong at the cable ends. My very, very bad - and so sorry to have wasted your time. You guys rock.

It really turns out it wasn't the board at all. I just reconfigured the array and it's now working fine with the existing board(s). Sheesh. Thanks again for your help.
 
Status
Not open for further replies.
Back
Top