// Better sequencer which uses interrupts #include #include Adafruit_MCP4728 mcp; #define nsamp 1500 #define dacmax 256 float tuning[37]; float voltrange = 4.85; // measured this, probably not accurate float octave = 4096.0 / voltrange; // number of DAC steps in an octave byte waveform[nsamp]; // 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, }; // melody int pitch[] = { 1, -1, -1, -1, -1, -1, -1, -1, }; int sequence[] = { 97, 98, 99, 100, 101, 102, 103, 104,105,}; float dur[] = { 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, }; // bass unsigned int phase = 0; unsigned int fibo = 3; unsigned int scale = 0; unsigned int fibi = 0; unsigned int gate = 4095; unsigned int pmax = nsamp; unsigned int beat = 0; int note = 0; int phrase = 16; int s; int bpm = 220; float beat_s = 60.0 / (float)bpm; float beat_m = 1000.0 * beat_s; bool noteon = false; long notestart, notedur, barstart; float xlog = 0.75; float r = 0; void setup() { Serial.begin(115200); float freqint = (float)bpm/ 60.0; int ocr = round(16000000.0 / (1024.0 * freqint)) - 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; // 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); } } for( int i = 0; i < nsamp; i++ ) { waveform[i] = 1; } fibi = 3; fibo = 3; //fibosort[fibi]; setwave(); // randomSeed(analogRead(A0)); mcp.setSpeed(800000L); note=0; barstart=millis(); } void setwave(){ waveform[0] = 0; waveform[1] = 1; fibo = fibi; // fibosort[fibi];// + 3; pmax = nsamp; for (int i=2; i 4095 ) { o = 4095; } if( o < 0 ) { o = 0; } mcp.setChannelValue(channel, o); } ISR(TIMER1_COMPA_vect){ // called once every beat note++; if( note == phrase ) { note = 0; } fibi = sequence[note]; beat = true; setwave(); phase = 0; } void loop() { float mod; int o; long now = millis(); if( beat ) { beat = false; if( pitch[note] > -1 ) { notestart = millis(); notedur = round(beat_m * dur[s]); Serial.println(notedur); gate = 4095; } } else { if( gate ) { if( now - notestart > notedur ) { gate = 0; } } mcp.fastWrite(waveform[phase] << 4, gate, 0, 0); phase += 1; if( phase == pmax ) { phase = 0; } } } void noteOn(int note) { if( note > -1 ) { mcp.setChannelValue(MCP4728_CHANNEL_B, 4095); } } void noteOff() { mcp.setChannelValue(MCP4728_CHANNEL_B, 0); }