58 lines
1.3 KiB
Arduino
58 lines
1.3 KiB
Arduino
|
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
|
||
|
#include <Adafruit_MCP4728.h>
|
||
|
#include <Wire.h>
|
||
|
|
||
|
Adafruit_MCP4728 mcp;
|
||
|
|
||
|
|
||
|
int tuning[15];
|
||
|
int edo = 7;
|
||
|
int sequence[] = { 0, 0, 3, 0, 5, 0, 7, 2, 0, 0, 3, 0, 5, 0, 7, 6 };
|
||
|
float voltrange = 5.0; // measured this, probably not accurate
|
||
|
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
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");
|
||
|
}
|
||
|
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
||
|
|
||
|
float n0 = 0;
|
||
|
for( int i = 0; i < 15; i++ ) {
|
||
|
tuning[i] = round(n0 + octave * (float)i / (float)edo);
|
||
|
Serial.println(tuning[i]);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop() {
|
||
|
for( int i = 0; i < 16; i++ ) {
|
||
|
note(sequence[i], 250);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void note(int note, int dur) {
|
||
|
int pot = analogRead(A0);
|
||
|
float gate = (float)pot / 1024.0;
|
||
|
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
|
||
|
delay(round(gate * (float)dur));
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
||
|
delay(round((1 - gate) * (float)dur));
|
||
|
}
|