Looptober24/22Envelopes/22Envelopes.ino

93 lines
1.8 KiB
C++

// Triggering shaped envelopes
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
float tuning[37];
float voltrange = 4.85; // measured this, probably not accurate
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
//bool gates[] = { true, false, true, false, true, false, false, true };
bool gates[] = { true, false, false, false, true, false, false, false };
int freqs[8];
int lpattern = 8;
int note = 0;
bool trigger = false;
long t0 = 0;
void setup() {
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
OCR1A = 10000;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 escaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
if (!mcp.begin(0x64)) {
while (1) {
delay(100);
}
}
randomSeed(analogRead(A0));
mcp.setSpeed(800000L);
for( int i = 0; i < 8; i++ ) {
freqs[i] = random(10, 450);
}
trigger = true;
}
ISR(TIMER1_COMPA_vect){ // called once every note
trigger = true;
}
void loop() {
long now = millis();
float f1;
int gate;
long freq;
if( trigger ) {
t0 = now;
trigger = false;
freq = freqs[note];
gate = gates[note];
note++;
if( note == lpattern ) {
note = 0;
}
}
f1 = 0.5 - 0.5 * sin((float)(now - t0) / (float)1000.0);
mcp.setChannelValue(MCP4728_CHANNEL_A, round(4095.0 * f1));
mcp.setChannelValue(MCP4728_CHANNEL_B, gate ? 4095 : 0);
}