Fixed buffer overflow which was setting the phase to random values

every cycle, leading to weirdness
main
Mike Lynch 2024-01-07 14:53:43 +11:00
parent 29d7156d16
commit 64a04067e0
1 changed files with 7 additions and 10 deletions

View File

@ -14,7 +14,6 @@ Adafruit_MCP4728 mcp;
unsigned int phase; unsigned int phase;
unsigned int repeats; unsigned int repeats;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -25,17 +24,16 @@ void setup() {
} }
} }
// mcp.setSpeed(400000L); randomSeed(analogRead(A0));
// mcp.setSpeed(800000L);
mcp.setSpeed(800000L); mcp.setSpeed(800000L);
phase=0; phase=0;
pluck();
} }
byte waveform[nsamp]; byte waveform[nsamp];
void pluck(){ void pluck(){
Serial.println("pluck");
for (int i=0; i<nsamp; ++i){ for (int i=0; i<nsamp; ++i){
waveform[i]=random(256); waveform[i]=random(256);
} }
@ -46,7 +44,7 @@ void loop() {
int p0 = phase; int p0 = phase;
s1 = waveform[phase]; s1 = waveform[phase];
phase += 1; phase += 1;
if( phase > nsamp ) { if( phase >= nsamp ) {
phase = 0; phase = 0;
repeats += 1; repeats += 1;
} }
@ -56,7 +54,6 @@ void loop() {
if( repeats > 100 ) { if( repeats > 100 ) {
pluck(); pluck();
repeats = 0; repeats = 0;
} }
} }