34 lines
669 B
Arduino
34 lines
669 B
Arduino
|
#include <Adafruit_MCP4728.h>
|
||
|
#include <Wire.h>
|
||
|
|
||
|
Adafruit_MCP4728 mcp;
|
||
|
|
||
|
|
||
|
long int last_
|
||
|
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop() {
|
||
|
int pot = analogRead(A0);
|
||
|
float rate = 2000 * (float)pot / 1024.0 + 0.01;
|
||
|
long int t = millis();
|
||
|
float v = 0.5 + 0.5 * sin((float)t / rate);
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_A, round(v * 4095));
|
||
|
}
|