Arpeggiator with a 4x4 Keypad - Doesn't loop

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

I think you need some code for that. I can't see any.

Steve

slipstick:
I think you need some code for that. I can't see any.

Steve

Hello Steve,
Yeah sorry, i pressed "Tab" by mistake, and it sent the post.
JVJ

Aaaand it seems i understood part of the problem.

Assumption : it comes from customKeypad.getKey(), which idles until i press something.
If I understand well, the program goes through setup(), then starts loop() and idles in customKeypad.getKey() until I press a key. It then continues down to my functions and the end of loop(), and when it's back at the top, here we are, idled again by customKeypad.getKey() until I press a new key...

That the sound stays in Keyboard Keypad, is due to tone() still playing the last note given.
In my code, by deleting the noTone() in arpeggio(), the last played tone stayed as well.

I cannot change anything in <keypad.h> without eventually breaking it...
A bit complicated to make customKeypad.getKey() loop my code as it is idling...
It looks like i'll have to built a completely other code, for it to work like I want, doesn't it ?

How do I delete this thread?
Should it stay as a good example of why one should sometimes think a bit more before posting?
But then, would I have found the correct reasoning reasoning, had I not posted?

Best regards,
JVJ

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.