Making a multi-keypress piano with keypad library and tone function

I want to make a portable 36-key polyphonic piano.

I have a 5x8 matrix piano keyboard(with diodes) that I want to be able to play chords on (simultaneous key press). I have made some tweaks to the example from Mark Stanley found here and I am trying to implement the tone function so that each key outputs its corresponding tone. I am not sure how to assign a tone to each key and then output those tones to analog input 6(external speaker).

I commented out the section where I believe I need help

#include <Keypad.h>
#include "pitches.h"

const byte ROWS = 5;
const byte COLS = 8;

char keys[ROWS][COLS] = {
  {'1','2','3','4','5','6','7','8'},
  {'A','B','C','D','E','F','G','H'},
  {'I','J','K','L','M','N','O','P'},
  {'Q','R','S','T','U','V','W','X'},
  {'Y','Z','!','@','#','

Any help is greatly appreciated

I also included a code I wrote that doesnt use keypad.h and only outputs one tone at a time(Piano3)

edit: I included the original file plus the pitches library that im trying to implement

pitches.h (1.95 KB)

PortablePiano.ino (1.9 KB)

Piano3.ino (5.84 KB),'%','&'},
};

byte rowPins[ROWS] = {A0, A1, A2, A3, A4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9}; //connect to the column pinouts of the keypad

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

unsigned long loopCount;
unsigned long startTime;
String msg;

void setup() {
   Serial.begin(9600);
   loopCount = 0;
   startTime = millis();
   msg = "";
}
void loop () {
   loopCount++;
   if((millis()-startTime)>5000) {
       Serial.print("Average loops per second =");
       Serial.println(loopCount/5);
       startTime = millis();
       loopCount = 0;
   }

// Fills kpd.key[ ] array with up-to 10 active keys.
   // Returns true if there are ANY active keys.
   if (kpd.getKeys())
   {
       for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
       {
           if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
           {
             
             /*
               switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
                   case PRESSED:
                   msg = " PRESSED.";
               break;
                   case HOLD:
                   msg = " HOLD.";
               break;
                   case RELEASED:
                   msg = " RELEASED.";
               break;
                   case IDLE:
                   msg = " IDLE.";
               }
               Serial.print("Key ");
               Serial.print(kpd.key[i].kchar);
               Serial.println(msg);
               */
           }
       }
   }
}  // End loop


Any help is greatly appreciated

I also included a code I wrote that doesnt use keypad.h and only outputs one tone at a time(Piano3)

edit: I included the original file plus the pitches library that im trying to implement

[pitches.h|attachment](upload://jInj0aptVfSNffSCEQEo7xWHvYh.h) (1.95 KB)

[PortablePiano.ino|attachment](upload://qJug2bL5Ck41vE2PkYdUHuPN0VZ.ino) (1.9 KB)

[Piano3.ino|attachment](upload://qzkZL5lU5jtUiJO6i4DunNKdIqT.ino) (5.84 KB)

I can't see any attempt to output notes of any sort in your code so I'm not sure what it is that you want help with.

And anyway the standard tone() function can only output one note at a time. There is supposed to be an alternative that will play 2 or more tones (depending on the specific Arduino) but I've never used it.

Steve

the output should be part of the tone function (or whatever function allows me to output two or more tones) and this im thinking would go under the " if ( kpd.key*.stateChanged )" loop but i dont know how to code that which is why its not included*

bryanfunctions:
the output should be part of the tone function (or whatever function allows me to output two or more tones)

That leaves out the built-in 'tone()' function. To get more than one tone at a time you will need a third-party library like GitHub - bhagman/Tone: A Wiring Library to produce square wave tones on arbitrary pins.

Each key in the active key list (kpd.key) has a .kcode value which is a zero-based index into the keypad key list. you could use that to index into a table of pitches.

When a key has the .kstate 'PRESSED' you would add it to your list of active notes and assign it to one of the Tone objects. When it has the .kstate 'RELEASED', turn off the tone and remove that note from the list of active notes.