#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#define CONTROL_RATE 128
const byte LED_PIN = 5;
const char TRIGGER_PIN = 0; // set the input for the knob to analog pin 0
const char POT_PIN = 1; // set the input for the knob to analog pin 0
byte led_brightness;
byte volume;
// control oscillators using sinewaves to modulate LED brightness
Oscil <SIN2048_NUM_CELLS, CONTROL_RATE> kLed(SIN2048_DATA);
// audio oscillator
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
void updateLED(byte b) {
// stagger pwm counter starts to try to reduce combined flicker
static byte LED_count = 0;
(LED_count++ >= b) ? PORTD &= ~(1 << LED_PIN) : PORTD |= (1 << LED_PIN);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
// set led brightness modulation frequencies
// set audio oscil frequency
//Serial.begin(115200);
startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h
}
void updateControl() {
led_brightness = 128 + kLed.next();
int potRead = mozziAnalogRead(TRIGGER_PIN); // value is 0-1023
int potRead2 = mozziAnalogRead(POT_PIN);
int mod = map(potRead2, 10, 1023, 0.5, 20);
aSin.setFreq(potRead2);
kLed.setFreq(mod);
// Serial.println(potRead);
// map it to an 8 bit range for efficient calculations in updateAudio
volume = map(potRead, 1023, 952, 0, 255);
}
int updateAudio() {
updateLED(led_brightness);
// this would make more sense with a higher resolution signal
// but still benefits from using HIFI to avoid the 16kHz pwm noise
return ((int)aSin.next() * volume) >> 2;
return ((int)kLed.next() * volume) >> 2;
}
void loop() {
audioHook(); // required here