arduino-modular/Fibomod/Fibomod.ino

103 lines
2.7 KiB
Arduino
Raw Normal View History

// can fibomod make an interesting noise?
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
2024-02-11 03:01:31 +00:00
#define nsamp 1500
#define dacmax 256
unsigned int phase = 0;
2024-02-11 03:01:31 +00:00
unsigned int fibo = 3;
unsigned int scale = 0;
2024-02-11 03:01:31 +00:00
unsigned int fibi = 0;
unsigned int pmax = nsamp;
unsigned int fibosort[] = { 4,3,11,8,29,7,21,19,38,76,5,55,199,6,9,12,16,18,24,36,48,72,144,13,22,31,44,62,124,47,141,17,34,68,136,152,15,33,41,123,165,205,58,116,211,89,139,14,23,28,32,42,46,56,63,69,84,92,96,112,126,138,161,168,184,207,224,252,101,151,233,39,87,59,10,20,40,61,88,110,122,155,220,244,248,71,27,51,54,57,102,107,108,114,153,171,204,214,216,228,37,113,79,158,35,77,105,231,26,52,104,232,43,129,181,209,64,94,188,192,25,53,106,109,212,218,121,49,91,147,203,229,30,45,60,66,80,82,90,93,99,120,132,164,176,180,183,186,198,240,246,131,178,67,201,65,143,145,119,133,189,238,73,149,202,111,235,78,83,117,156,166,174,208,234,249,118,236,179,85,95,170,187,190,191,128,97,75,103,142,81,159,162,74,148,226,177,239,70,115,140,154,160,210,217,230,241,253,251,221,247,127,86,172,137,195,213,219,50,100,200,237,157,163,242,98,167,182,196,173,135,255,169,185,193,197,175,134,130,215,146,223,222,227,125,245,194,150,225,206,243,254,250 };
byte waveform[nsamp];
void setup() {
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
2024-02-11 03:01:31 +00:00
OCR1A = 2000;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
2024-02-11 03:01:31 +00:00
Serial.begin(115200);
if (!mcp.begin(0x64)) {
while (1) {
delay(100);
}
}
mcp.setSpeed(800000L);
for( int i = 0; i < nsamp; i++ ) {
waveform[i] = 1;
}
2024-02-11 03:01:31 +00:00
fibo = fibosort[fibi];
setwave();
}
2024-02-11 03:01:31 +00:00
void setwave(){
waveform[0] = 0;
waveform[1] = 1;
fibo = fibosort[fibi];
Serial.println(fibo);
pmax = nsamp;
for (int i=2; i<nsamp; ++i){
waveform[i] = (waveform[i - 1] + waveform[i - 2]) % fibo;
if( waveform[i - 1] == 0 && waveform[i] == 1 ) {
pmax = i - 1;
break;
}
}
Serial.println(pmax);
float scale = 256 / fibo;
for( int i = 0; i < nsamp; ++i ) {
waveform[i] *= scale;
}
}
ISR(TIMER1_COMPA_vect){//timer1
2024-02-11 03:01:31 +00:00
fibi++;
if( fibi == 128 ) {
fibi = 0;
}
2024-02-11 03:01:31 +00:00
Serial.println("Setting wave");
setwave();
phase = 0;
}
void loop() {
2024-02-11 03:01:31 +00:00
mcp.fastWrite(waveform[phase] << 4, 0, 0, 0);
phase += 1;
2024-02-11 03:01:31 +00:00
if( phase == pmax ) {
phase = 0;
}
}