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 repeats;
void setup() {
Serial.begin(115200);
@ -25,17 +24,16 @@ 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; ++i){
waveform[i]=random(256);
}
@ -46,7 +44,7 @@ void loop() {
int p0 = phase;
s1 = waveform[phase];
phase += 1;
if( phase > 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;
}
}