Hello,
Problem :
I built a code for an arpeggiator that doesn't want to arpeggiate. I press a key, it plays the sequence once, but then it stops, and I have to press again to replay it.
Summary :
I'm trying to implement a basic arpeggiator from scratch, with for input a 4x4 Keypad.
1 2 3 Up (scale C3, C4 etc)
4 5 6 Down
7 8 9 Mode (major, minor, penta etc)
10 11 12 Speed
Hardware : Arduino UNO R3, Keypad 4x4 (0-9*#ABCD), Piezo (for sound at the moment, proper output is for later).
Software : TinkerCAD for writing and testing, Arduino for real life.
My base for the arpeggiator is a Keyboard Keypad that i implemented, playing the chromatic notes on 1-12.
**Functionning of Keyboard Keypad : **
Pressing a key converts it to a position for keys[94], that gets played in tone( ).
All works well, i can go up and down the C1-C8 scale, and it keeps playing until i press another note.
For this code, pressing "D" does a switch case "D": noTone( ).
Extract of Keyboard Keypad :
void loop(){
char customKey = customKeypad.getKey();
switch(customKey){
case '1':
Serial.println(customKey);
tone(10,keyC1toB8[(plage + 0)]);
Serial.print(keyC1toB8[(plage + 0)]);Serial.println(plage);
break;
}
Functionning of Arpeggiator :
Pressing a key gives the base note (number = position in keys[94], then arpeggio( ) looks in keys[94] the pattern from ArpeggioPattern[8] (rolling up the array), then a for-loop plays the corresponding keys[ i ] (Hertz) through tone( ). But contrary to Keyboard Keypad, it doesn't repeat!
After it played the sequence, pressing again the key will play it again. Changing the parameters (scale, mode...) works as well.
Question :
I dont understand why it doesn't repeat.
After playing it should go out of my function, out of the main switch case, down the bracket then back at the top of loop( ), shouldn't it? Have I missed an unwanted break or value reset somewhere? Except a big change in playing with the values, i don't have major structural changes from Keyboard Keypad...
What am I missing?
Extract of Arpeggiator : (starting note/mode is a repeating C4)
void loop(){
char customKey = customKeypad.getKey();
switch(customKey){
case '1':
Serial.println(customKey);
currentNote = noteShift(0);
Serial.println(currentNote);
arpeggiator(currentNote);
break;
}
void arpeggiator(int current){ // input PosinArray - Output Herz
for(int i=0;i<=7;i++){
playedNote = current + Cref[i]; //Still PosinArray value
Serial.print(playedNote);
Serial.print("-");
Serial.print(keyC1toB8[playedNote]);
Serial.print("/");
tone(10,keyC1toB8[playedNote]);
delay(time);
noTone(10);
delay(10);
}
Serial.println("Arpeg");
}
int noteShift(int keyPlayed){ // entry 1-# -> note C-B in matrix, with correct octave
int keyShift = keyPlayed + plage;
return keyShift;
}
void modeSelector(){ // Changes Cref[] so the tune can be played in arpeggio() - pending pointers
switch(mode){
case 0: for(int i=0;i<=7;i++){Cref[i]=Cind[i];} break;
case 1: for(int i=0;i<=7;i++){Cref[i]=Cmaj7[i];} break;
case 2: for(int i=0;i<=7;i++){Cref[i]=Cmaj[i];} break;
case 3: for(int i=0;i<=7;i++){Cref[i]=Cmin[i];} break;
case 4: for(int i=0;i<=7;i++){Cref[i]=Cmin5dim[i];} break;
case 5: for(int i=0;i<=7;i++){Cref[i]=Cpenta[i];} break;
case 6: for(int i=0;i<=7;i++){Cref[i]=C9413[i];}; break;
}
for(int i=0;i<=7;i++){Serial.print(Cref[i]);}
Serial.println("-");
}
Don't mind the heavy code, i'm a beginner and have taken on a naïve approach to the problem.
Thanks a lot for your answers,
TinkerCAD link for Arpeggiator : Circuit design Arpeggiator | Tinkercad
TinkerCAD ling for Keyboard Keypad : Circuit design Keypad Keyboard | Tinkercad