89 lines
1.9 KiB
C++
89 lines
1.9 KiB
C++
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
|
|
#include <Adafruit_MCP4728.h>
|
|
#include <Wire.h>
|
|
|
|
Adafruit_MCP4728 mcp;
|
|
|
|
|
|
// idea behind this - sequence a cloud of gate events
|
|
|
|
|
|
float hexany[] = { 0.12928301694496647, 0.32192809488736235, 0.3923174227787603, 0.5849625007211562, 0.8073549220576041, 0.9068905956085185 };
|
|
float tuning[18];
|
|
|
|
int chord[10] = { 0, 2, 4, 6, 8, 7, 11, 15, 14, 10 };
|
|
|
|
int bpm = 60;
|
|
float beat_s = 60.0 / (float)bpm;
|
|
int beat_m = round(1000.0 * beat_s);
|
|
float mod_b = 3.141592653589793 / 4.0;
|
|
float voltrange = 4.85; // measured this, probably not accurate
|
|
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
|
|
|
|
|
|
int gate = 0;
|
|
int gatel = 1000;
|
|
int tick = 16;
|
|
long init_t;
|
|
float p_gate = 0.0;
|
|
|
|
void setup(void) {
|
|
Serial.begin(115200);
|
|
while (!Serial)
|
|
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
|
|
|
if (!mcp.begin(0x64)) {
|
|
Serial.println("Failed to find MCP4728 chip");
|
|
while (1) {
|
|
delay(10);
|
|
}
|
|
Serial.println("MCP4728 initialised");
|
|
}
|
|
|
|
float n0 = 0;
|
|
for( int o = 0; o < 3; o++ ) {
|
|
for( int i = 0; i < 18; i++ ) {
|
|
tuning[o * 6 + i] = round(n0 + octave * (o + hexany[i]));
|
|
}
|
|
}
|
|
|
|
|
|
init_t = millis();
|
|
|
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
int note;
|
|
int k = 0;
|
|
if( gate > 0 ) {
|
|
gate--;
|
|
if( gate == 0 ) {
|
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
|
}
|
|
} else {
|
|
long now = millis();
|
|
long mod_t = now - init_t;
|
|
tick--;
|
|
if( tick == 0 ) {
|
|
tick = 32;
|
|
p_gate = sin(mod_b * (float)mod_t / (float)beat_m);
|
|
}
|
|
if( (float)random(10000) / 20.0 < abs(p_gate)) {
|
|
if( p_gate > 0 ) {
|
|
note = random(5);
|
|
} else {
|
|
note = 5 + random(5);
|
|
}
|
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
|
|
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
|
|
gate = gatel;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|