Hi all, I'm relatively new to programming still and am experimenting with the Mozzi library. I've written a sketch to try and get a button keyboard working with an envelope and I'm getting no sound or light out of the led at all at all. Could anyone provide any suggestions as to why this isn't working?
Here's my code:
#include <MozziGuts.h> // at the top of your sketch
#include <ADSR.h> //envelope template
#include <Oscil.h> //osc template
#include <tables/sin1024_int8.h> //wave used
// control rate of the program
#define CONTROL_RATE 64
// envelope generator
ADSR <CONTROL_RATE, CONTROL_RATE> envelope;
// Oscillator
Oscil <1024, AUDIO_RATE> osc(SIN1024_DATA);
// led pin
byte led = 13;
// notepins
byte noteC = 3;
byte noteD = 4;
byte noteE = 5;
byte noteF = 6;
byte noteG = 7;
// note values
byte noteValC = 261;
byte noteValD = 293;
byte noteValE = 329;
byte noteValF = 349;
byte noteValG = 392;
// storing key values for easier recall
int keys[] = {noteC, noteD, noteE, noteF, noteG};
// variable to read if a keypress occurs
int keyPress = digitalRead(keys[0, 1, 2, 3, 4]);
// note on function
int keyPressOn(byte) {
osc.setFreq(note);
envelope.noteOn();
digitalWrite(led, HIGH);
}
// note off function
void keyPressOff(byte) {
envelope.noteOff();
digitalWrite(led, LOW);
}
// note function
int note() {
if (keyPress == keys[0]) {
return noteValC;
}
else if (keyPress == keys[1]) {
return noteValD;
}
else if (keyPress == keys[2]) {
return noteValE;
}
else if (keyPress == keys[3]) {
return noteValF;
}
else if (keyPress == keys[4]) {
return noteValG;
}
}
void setup() {
startMozzi(CONTROL_RATE); // starting Mozzi up
// setting pinmodes for keyboard and led
pinMode(noteC, INPUT_PULLUP);
pinMode(noteD, INPUT_PULLUP);
pinMode(noteE, INPUT_PULLUP);
pinMode(noteF, INPUT_PULLUP);
pinMode(noteG, INPUT_PULLUP);
pinMode(led, OUTPUT);
// setting envelope values
envelope.setADLevels(255, 64);
envelope.setTimes(50, 200, 10000, 200);
}
void updateControl() {
// your control code
if (keyPress == LOW) {
keyPressOn(note);
envelope.update();
}
else {
keyPressOff(note);
envelope.update();
}
}
int updateAudio() {
// your audio code which returns an int between -244 and 243
// actually, a char is fine
return osc.next();
}
void loop() {
audioHook(); // fills the audio buffer
}