From 64a04067e0ac7d31e7a2cff88327667696b28805 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sun, 7 Jan 2024 14:53:43 +1100 Subject: [PATCH] Fixed buffer overflow which was setting the phase to random values every cycle, leading to weirdness --- Karplus_Strong/Karplus_Strong.ino | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Karplus_Strong/Karplus_Strong.ino b/Karplus_Strong/Karplus_Strong.ino index a5d7136..071f75e 100644 --- a/Karplus_Strong/Karplus_Strong.ino +++ b/Karplus_Strong/Karplus_Strong.ino @@ -14,7 +14,6 @@ Adafruit_MCP4728 mcp; unsigned int phase; unsigned int repeats; - void setup() { Serial.begin(115200); @@ -25,18 +24,17 @@ void setup() { } } -// mcp.setSpeed(400000L); -// mcp.setSpeed(800000L); + randomSeed(analogRead(A0)); mcp.setSpeed(800000L); phase=0; - pluck(); } byte waveform[nsamp]; + + void pluck(){ - Serial.println("pluck"); - for (int i=0; i nsamp ) { + if( phase >= nsamp ) { phase = 0; repeats += 1; } @@ -54,9 +52,8 @@ void loop() { waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1); mcp.fastWrite(s1 << 4, 0, 0, 0); if( repeats > 100 ) { - pluck(); - repeats = 0; - + pluck(); + repeats = 0; } }