/* -= Chaos Synth Karplus =- Based Logistic equation Pot0 = chaos constant Pot1 = lowpass filter Pot2 = tempo/speed PWM audio out - PIN 3 created by JLS 2018 */ #include #include #include #define SIZE 1024 #define OFFSET 128 uint8_t out = 0; int last = 0; int curr = 0; uint8_t delaymem[SIZE]; uint8_t locat = 0; uint8_t bound = SIZE; int accum = 0; int lowpass = 1; float r; float x = 0.2; #define PWM_PIN 3 #define PWM_VALUE_DESTINATION OCR2B #define PWM_INTERRUPT TIMER2_OVF_vect void initializeTimer() { TCCR2A = _BV(COM2B1) | _BV(WGM20); TCCR2B = _BV(CS20); TIMSK2 = _BV(TOIE2); pinMode(PWM_PIN,OUTPUT); } SIGNAL(PWM_INTERRUPT) { PWM_VALUE_DESTINATION = out; delaymem[locat++] = out; if (locat >= bound) locat = 0; curr = delaymem[locat]; out = accum >> lowpass; accum = accum - out + ((last>>1) + (curr>>1)); last = curr; } void setup() { initializeTimer(); } void loop() { r = map(analogRead(0),0,1023,35000,39900); r /= 10000.0; float nx = x; x = r*nx*(1-nx); uint8_t xout = (SIZE-OFFSET) * x; for (int i = 0; i < SIZE; i++) delaymem[i] = random(256); lowpass = analogRead(1)>>8; bound = OFFSET + xout; delay (analogRead(2)>>2); }