I've modified the Mozzi_MIDI_Input example sketch (which I've been succesful in making work in the past) to instead of looking for MIDI notes to change the sine frequency, to have the keyboard be scanned and return MIDI note values internally, and then change the sine's frequency when a note is pressed.
During testing I bypassed the envelope from the example to make sure it doesn't interfere, and also set the frequency in the same way as in the note handler to make sure that's working as well.
In the end the code compiles and uploads, and the sine is outputted, but the frequency doesn't change. I've checked the keyboard scanning function independently, and it works exactly as expected, printing out a single note value, and then 0 when the button is held.
#include <Mozzi.h>
#include <mozzi_midi.h>
#include <Oscil.h>
#include <tables/sin2048_int8.h>
#include <ADSR.h>
#include <FixMath.h>
Oscil <SIN2048_NUM_CELLS, MOZZI_AUDIO_RATE> sien(SIN2048_DATA);
ADSR <MOZZI_CONTROL_RATE, MOZZI_AUDIO_RATE> env;
//trying to control the frequency with a keyboard, buttons being active low
uint8_t noten[5][5] = { //Note lookup table (promise it's not rotated, the keyboard's soldered that way)
{36,41,46,51,56},
{37,42,47,52,57},
{38,43,48,53,58},
{39,44,49,54,59},
{40,45,50,55,60}
};
bool knopfstati[5][5]; //for button states
uint8_t scan_keyboard() { //in the name
for (uint8_t links = 0; links < 5; links++) { //go throughthe columns
for (uint8_t rap = 6; rap < 11; rap++) { //initialize the output lines (colums)
digitalWrite(rap, HIGH);
}
digitalWrite(links+6, LOW); //put the column to be read low
for (uint8_t rechts = 0; rechts < 5; rechts++) { //read the individual buttons of the row
bool buttonstate = digitalRead(rechts+1); // offset by 1 because pin 0 is the audio output
if ((buttonstate == LOW) && (knopfstati[links][rechts] == HIGH)) { //return a note value when a button is pressed
knopfstati[links][rechts] = buttonstate;
return noten[links][rechts];
}
else if (buttonstate == LOW) { //return 0 if the button is held
return 0;
}
knopfstati[links][rechts] = buttonstate; //always update buttonstate
}
}
return 0xFF; //return 0xFF when nothing is pressed
}
void setup() {
Serial.begin(115200);
Serial.println("starting sint...");
for (uint8_t pinmodeset = 1; pinmodeset < 6; pinmodeset++) { //set pinmodes for rows and columns
pinMode(pinmodeset, INPUT_PULLUP); //row pins are 1 through 5
pinMode(pinmodeset + 5, OUTPUT); // column pins are 6 through 10
}
//env.setADLevels(127,127); //bypassed
//env.setTimes(5,5,1000,5);
sien.setFreq_Q16n16(Q16n16_mtof(Q8n0_to_Q16n16(42))); //testing if audio is being outputted
startMozzi();
}
void updateControl() {
uint8_t dus_de = scan_keyboard(); //get the value needed
if (dus_de != 0xFF && dus_de != 0) { //change frequency if button is pressed
sien.setFreq_Q16n16(Q16n16_mtof(Q8n0_to_Q16n16(dus_de)));
}
//env.update();
}
AudioOutput updateAudio() {
return MonoOutput::from8Bit(sien.next());
}
void loop() {
audioHook();
}