kelvinmead
Member
Code:
//////////////////////////////////////
// Kick Synth //
// Kelvin Mead, March 2026 //
// 0.3 //
//////////////////////////////////////
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h> // SCL pin 19, SDA pin 18
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveformModulated waveformMod1; //xy=140,498
AudioSynthWaveform waveform3; //xy=160,465
AudioSynthWaveform waveform2; //xy=161,432
AudioSynthWaveform waveform1; //xy=161.5,399
AudioSynthNoisePink pink1; //xy=164,532
AudioEffectEnvelope envelope5; //xy=295,534
AudioEffectEnvelope envelope4; //xy=297,501
AudioEffectEnvelope envelope3; //xy=298,468
AudioEffectEnvelope envelope2; //xy=299,434
AudioEffectEnvelope envelope1; //xy=299.5,400
AudioMixer4 mixer1; //xy=470.5,435
AudioMixer4 mixer2; //xy=598,476
AudioOutputI2S i2s1; //xy=765.5,455
AudioConnection patchCord1(waveformMod1, envelope4);
AudioConnection patchCord2(waveform3, envelope3);
AudioConnection patchCord3(waveform2, envelope2);
AudioConnection patchCord4(waveform1, envelope1);
AudioConnection patchCord5(pink1, envelope5);
AudioConnection patchCord6(envelope5, 0, mixer2, 1);
AudioConnection patchCord7(envelope4, 0, mixer1, 3);
AudioConnection patchCord8(envelope3, 0, mixer1, 2);
AudioConnection patchCord9(envelope2, 0, mixer1, 1);
AudioConnection patchCord10(envelope1, 0, mixer1, 0);
AudioConnection patchCord11(mixer1, 0, mixer2, 0);
AudioConnection patchCord12(mixer2, 0, i2s1, 0);
AudioConnection patchCord13(mixer2, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=64.5,253
// GUItool: end automatically generated code
// Pin Definitions
#define LED 13
#define buttonPin1 38
#define buttonPin2 37
#define buttonPin3 36
#define buttonPin4 35
#define buttonPin5 34
#define buttonPin6 33
#define encButtonPin 39
Bounce button1 = Bounce(buttonPin1, 15);
Bounce button2 = Bounce(buttonPin2, 15); // 15 = 15 ms debounce time
Bounce button3 = Bounce(buttonPin3, 15);
Bounce button4 = Bounce(buttonPin4, 15);
Bounce button5 = Bounce(buttonPin5, 15);
Bounce button6 = Bounce(buttonPin6, 15);
Bounce encButton = Bounce(encButtonPin, 15);
// init synth values, maybe use these to save later
int synWav[5] = {WAVEFORM_SINE, WAVEFORM_SAWTOOTH, WAVEFORM_SQUARE, WAVEFORM_TRIANGLE, WAVEFORM_PULSE};
float synAmp[6] = {0.75, 0.75, 0.75, 0.75, 0.75, 0.75};
int synFre[6] = {110, 110, 110, 110, 110, 110};
float synPul[6] = {0.15, 0.15, 0.15, 0.15, 0.15, 0.15};
int synAtt[6] = {10, 10, 10, 10, 10, 10};
int synHol[6] = {10, 10, 10, 10, 10, 10};
int synDec[6] = {75, 75, 75, 75, 75, 75};
float synSus[6] = {0.4, 0.4, 0.4, 0.4, 0.4, 0.4};
int synRel[6] = {200, 200, 200, 200, 200, 200};
int state = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(encButtonPin, INPUT_PULLUP);
pinMode(LED,OUTPUT);
oldPot1value = analogRead(pot1);
oldPot2value = analogRead(pot2);
oldPot3value = analogRead(pot3);
oldPot4value = analogRead(pot4);
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.75);
mixer1.gain(0, 0.75);
mixer1.gain(1, 0.75);
mixer1.gain(2, 0.75);
mixer1.gain(3, 0.75);
mixer2.gain(0, 0.75);
mixer2.gain(1, 0.75);
mixer2.gain(2, 0.0);
mixer2.gain(3, 0.0);
}
void loop() {
readButtons();
setSound1(state);
setSound2(state);
setSound3(state);
setSound4(state);
setSound5(state);
playSound();
}
void readButtons() {
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
encButton.update();
if (button1.fallingEdge()) { state = 1; Serial.print(" state = "); Serial.println(state); }
if (button2.fallingEdge()) { state = 2; Serial.print(" state = "); Serial.println(state); }
if (button3.fallingEdge()) { state = 3; Serial.print(" state = "); Serial.println(state); }
if (button4.fallingEdge()) { state = 4; Serial.print(" state = "); Serial.println(state); }
if (button5.fallingEdge()) { state = 5; Serial.print(" state = "); Serial.println(state); }
if (button6.fallingEdge()) { state = 6; Serial.print(" state = "); Serial.println(state); }
if (encButton.fallingEdge()) { state = 7; Serial.print(" state = "); Serial.println(state); }
}
void playSound() {
// button pressed
if (button1.fallingEdge()) { envelope1.noteOn(); }
if (button2.fallingEdge()) { envelope2.noteOn(); }
if (button3.fallingEdge()) { envelope3.noteOn(); }
if (button4.fallingEdge()) { envelope4.noteOn(); }
if (button5.fallingEdge()) { envelope5.noteOn(); }
if (button6.fallingEdge()) {
// AudioNoInterrupts();
envelope1.noteOn();
envelope2.noteOn();
envelope3.noteOn();
envelope4.noteOn();
envelope5.noteOn();
// AudioInterrupts();
Serial.println("all");
}
// button released
if (button1.risingEdge()) { envelope1.noteOff(); }
if (button2.risingEdge()) { envelope2.noteOff(); }
if (button3.risingEdge()) { envelope3.noteOff(); }
if (button4.risingEdge()) { envelope4.noteOff(); }
if (button5.risingEdge()) { envelope5.noteOff(); }
if (button6.risingEdge()) {
// AudioNoInterrupts();
envelope1.noteOff();
envelope2.noteOff();
envelope3.noteOff();
envelope4.noteOff();
envelope5.noteOff();
// AudioInterrupts();
}
}
void setSound1(int currentSyn){
waveform1.begin(WAVEFORM_SINE); // synWav
waveform1.amplitude(synAmp[currentSyn]); // synAmp
waveform1.frequency(synFre[currentSyn]); // synFre
waveform1.pulseWidth(synPul[currentSyn]); // synPul
envelope1.attack(synAtt[currentSyn]); // synAtt
envelope1.hold(synHol[currentSyn]); // synHol
envelope1.decay(synDec[currentSyn]); // synDec
envelope1.sustain(synSus[currentSyn]); // synSus
envelope1.release(synRel[currentSyn]); // synRel
}
void setSound2(int currentSyn) {
waveform2.begin(WAVEFORM_SQUARE); // synWav
waveform2.amplitude(synAmp[currentSyn]); // synAmp
waveform2.frequency(synFre[currentSyn]); // synFre
waveform2.pulseWidth(synPul[currentSyn]); // synPul
envelope2.attack(synAtt[currentSyn]); // synAtt
envelope2.hold(synHol[currentSyn]); // synHol
envelope2.decay(synDec[currentSyn]); // synDec
envelope2.sustain(synSus[currentSyn]); // synSus
envelope2.release(synRel[currentSyn]); // synRel
}
void setSound3(int currentSyn) {
waveform3.begin(WAVEFORM_SAWTOOTH); // synWav
waveform3.amplitude(synAmp[currentSyn]); // synAmp
waveform3.frequency(synFre[currentSyn]); // synFre
waveform3.pulseWidth(synPul[currentSyn]); // synPul
envelope3.attack(synAtt[currentSyn]); // synAtt
envelope3.hold(synHol[currentSyn]); // synHol
envelope3.decay(synDec[currentSyn]); // synDec
envelope3.sustain(synSus[currentSyn]); // synSus
envelope3.release(synRel[currentSyn]); // synRel
}
void setSound4(int currentSyn) {
waveformMod1.begin(WAVEFORM_TRIANGLE); // synWav
waveformMod1.amplitude(synAmp[currentSyn]); // synAmp
waveformMod1.frequency(synFre[currentSyn]); // synFre
envelope4.attack(synAtt[currentSyn]); // synAtt
envelope4.hold(synHol[currentSyn]); // synHol
envelope4.decay(synDec[currentSyn]); // synDec
envelope4.sustain(synSus[currentSyn]); // synSus
envelope4.release(synRel[currentSyn]); // synRel
}
void setSound5(int currentSyn) {
pink1.amplitude(synAmp[currentSyn]); // synAmp
envelope5.attack(synAtt[currentSyn]); // synAtt
envelope5.hold(synHol[currentSyn]); // synHol
envelope5.decay(synDec[currentSyn]); // synDec
envelope5.sustain(synSus[currentSyn]); // synSus
envelope5.release(synRel[currentSyn]); // synRel
}
}
ok, theres nothing special here. i use the bounce library to set up 6 buttons. buttons 1 to 5 are waveforms, and pressing the button will play that sound. button 6 plays all the sounds at the same time.
the loop is; read the buttons, if the buttons are pressed do something, if not pressed, stop doing something. they have an additional serial.print, so i can see when the button is pressed.
on pressing all the buttons, the serial monitor prints the number of the button being pressed. this all works as it should.
when i try to play the sound, buttons 1 to 5 work fine, but button 6 refuses to play anything.
if i swap the code around so 1 and 6 are reversed, then button 1 plays the sounds all together (the expected result from 6) but the button 6 plays no sound at all.
so it feels like a button wiring issue, except the serial monitor is happily printing 1 - 6 everytime i press the relevant button!
using teensy 4.1 and the audio breakout board