// Better sequencer which uses interrupts #include #include 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 // melody // int pitch[] = { 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1,2, -1, -1, -1, }; // int dur[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, }; // hi hat int pitch[] = { 1, 3, 4, 10, 6, 3, 11, -1 }; float dur[] = { 1,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5, }; int note = 0; int phrase = 8; int s; int bpm = 120; float beat_s = 60.0 / (float)bpm; float beat_m = 1000.0 * beat_s; bool noteon = false; int beat = false; long notestart, notedur, barstart; int target, slew; float slewk = 0.005; void setup() { Serial.begin(115200); float freq = (float)bpm / 60.0; int ocr = round(16000000.0 / (1024.0 * freq )) - 1; Serial.println(ocr); if( ocr < 65536 ) { cli(); TCCR1A = 0;// set entire TCCR1A register to 0 TCCR1B = 0;// same for TCCR1B TCNT1 = 0;//initialize counter value to 0 OCR1A = ocr; //ocr; // 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(); } else { Serial.println("BPM out of range"); } if (!mcp.begin(0x64)) { while (1) { delay(100); } } // randomSeed(analogRead(A0)); mcp.setSpeed(800000L); make_tuning(12); note=0; slew=0; barstart=millis(); } void make_tuning(int edo) { float n0 = 0; float edof = (float)edo; for( int i = 0; i < 37; i++ ) { tuning[i] = round(n0 + octave * (float)i / edof); } } // mod_note argument is milliseconds from the start of last note float mod_note(int x) { float xb = x / (0.2 * beat_m); return 0.5 + 0.5 * xb * (xb - 1) * ( xb - 2); } // mod_note argument is milliseconds from the start of this bar float mod_bar(int x) { return 0.5 + 0.5 * sin((float)x / (beat_m * 2)); } void set_mod(MCP4728_channel_t channel, float v) { int o; o = round(v * 4095.0); if( o > 4095 ) { o = 4095; } if( o < 0 ) { o = 0; } mcp.setChannelValue(channel, o); } ISR(TIMER1_COMPA_vect){ // called once every beat beat = true; } void loop() { float mod; int o; long now = millis(); if( beat ) { beat = false; if( pitch[s] > -1 ) { notestart = millis(); notedur = round(beat_m * dur[s]); noteOn(pitch[s]); noteon = true; } s += 1; if( s == phrase ) { s = 0; barstart = notestart; } } else { if( noteon ) { set_mod(MCP4728_CHANNEL_C, mod_note(now - notestart)); if( now - notestart > notedur ) { noteOff(); noteon = false; } } set_mod(MCP4728_CHANNEL_D, mod_bar(now - barstart)); slew += round((float)(target - slew) * slewk); if( slew > 4095 ) { slew = 4095; } if( slew < 0 ) { slew = 0; } mcp.setChannelValue(MCP4728_CHANNEL_A, slew); } } void noteOn(int note) { if( note > -1 ) { //mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]); target = tuning[note]; mcp.setChannelValue(MCP4728_CHANNEL_B, 4095); } } void noteOff() { mcp.setChannelValue(MCP4728_CHANNEL_B, 0); }