50 lines
1.1 KiB
Arduino
50 lines
1.1 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 bpm = 98;
|
||
|
float beat_s = 60.0 / (float)bpm;
|
||
|
int beat_m = round(1000.0 * beat_s);
|
||
|
long init_t;
|
||
|
|
||
|
float pi = 3.141592653589793;
|
||
|
float mod_a = pi;
|
||
|
float mod_b = pi / 4.0;
|
||
|
float mod_c = pi / 3.0;
|
||
|
float mod_d = pi / 5.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");
|
||
|
}
|
||
|
|
||
|
init_t = millis();
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop() {
|
||
|
long now = millis();
|
||
|
long mod_t = (now - init_t);
|
||
|
float tf = (float)mod_t / (float)beat_m;
|
||
|
uint16_t a = round(4095.0 * (0.5 + 0.5 * sin(mod_a * tf)));
|
||
|
uint16_t b = round(4095.0 * (0.5 + 0.5 * sin(mod_b * tf)));
|
||
|
uint16_t c = round(4095.0 * (0.5 + 0.5 * sin(mod_c * tf)));
|
||
|
uint16_t d = round(4095.0 * (0.5 + 0.5 * sin(mod_d * tf)));
|
||
|
mcp.fastWrite(a, b, c, d);
|
||
|
}
|