Initial commit

main
Mike Lynch 2023-12-10 09:18:00 +11:00
commit 65657944c4
7 changed files with 330 additions and 0 deletions

94
Hexany/Hexany.ino 100644
View File

@ -0,0 +1,94 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
// 1 3 5 7 hexany log2
float hexany[] = { 0.0, 0.12928301694496647, 0.32192809488736235, 0.3923174227787603, 0.5849625007211562, 0.8073549220576041, 0.9068905956085185 };
float tuning[7];
int pitch[] = { 0, -1, 1, 2, -1, 3, -1, 4, 5, -1, 6, 5, -1, 3, -1, 1 };
float dur[] = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
float gate = 0.5;
int bpm = 125;
float beat_s = 60.0 / (float)bpm;
int beat_m = round(1000.0 * beat_s);
float voltrange = 5.0; // measured this, probably not accurate
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
float mod_b = 3.141592653589793 / 4.0;
int s;
bool noteon = false;
long play_t;
long release_t;
long last_t;
long init_t;
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 < 7; i++ ) {
tuning[i] = round(n0 + octave * hexany[i]);
}
s = 0;
release_t = 0;
play_t = 0;
last_t = millis();
init_t = last_t;
}
void loop() {
long now = millis();
if( noteon ) {
if( now - last_t > release_t ) {
noteOff();
last_t = now;
play_t = round(beat_m * dur[s] * (1.0 - gate));
noteon = false;
s += 1;
if( s > 15 ) {
s = 0;
}
}
} else {
if( now - last_t > play_t ) {
noteOn(pitch[s]);
last_t = now;
release_t = round(beat_m * dur[s] * gate);
noteon = true;
}
}
// sychronise this better
long mod_t = (now - init_t);
float mod = 0.5 + 0.5 * sin(mod_b * (float)mod_t / (float)beat_m);
mcp.setChannelValue(MCP4728_CHANNEL_C, round(4095.0 * mod));
}
void noteOn(int note) {
if( note > -1 ) {
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
}
}
void noteOff() {
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
}

View File

@ -0,0 +1,41 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
float xlog = 0.5;
float r = 3.783423;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MCP4728 test!");
// Try to initialize!
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
}
}
mcp.setChannelValue(MCP4728_CHANNEL_A, 4095);
mcp.setChannelValue(MCP4728_CHANNEL_B, 2048);
mcp.setChannelValue(MCP4728_CHANNEL_C, 1024);
mcp.setChannelValue(MCP4728_CHANNEL_D, 0);
}
void loop() {
int pot = analogRead(A0);
r = (float)pot / 1024.0 + 3.0;
xlog = r * xlog * (1 - xlog);
int cv = round(xlog * 4095.0);
mcp.setChannelValue(MCP4728_CHANNEL_A, cv);
delay(100);
}

42
Scales/Scales.ino 100644
View File

@ -0,0 +1,42 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
int tuning[13];
int scale[] = { 0, 4, 7, 9, 12 };
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");
}
Serial.println(octave);
float n0 = 2048;
for( int i = 0; i < 13; i++ ) {
tuning[i] = round(n0 + octave * (float)i / 12);
Serial.println(tuning[i]);
}
}
void loop() {
for( int i = 0; i < 5; i++ ) {
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[scale[i]]);
delay(200);
}
}

View File

@ -0,0 +1,57 @@
// 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));
}

View File

@ -0,0 +1,33 @@
#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));
}

View File

@ -0,0 +1,41 @@
// Basic demo for configuring the MCP4728 4-Channel 12-bit I2C DAC
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
float xlog = 0.5;
float r = 3.783423;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MCP4728 test!");
// Try to initialize!
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
}
}
mcp.setChannelValue(MCP4728_CHANNEL_A, 4095);
mcp.setChannelValue(MCP4728_CHANNEL_B, 2048);
mcp.setChannelValue(MCP4728_CHANNEL_C, 1024);
mcp.setChannelValue(MCP4728_CHANNEL_D, 0);
}
void loop() {
for( int i = 0; i < 1024; i++ ) {
mcp.setChannelValue(MCP4728_CHANNEL_A, i);
mcp.setChannelValue(MCP4728_CHANNEL_B, i * 2);
mcp.setChannelValue(MCP4728_CHANNEL_C, i * 3);
mcp.setChannelValue(MCP4728_CHANNEL_D, i * 4);
}
}

View File

@ -0,0 +1,22 @@
/*
Analog Read Serial
Reads an analog input on pin 0.
prints the result to the Serial Monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(100); // delay in between reads for stability
}