As part of a larger script intended for a very simple arduino "synthesizer" I am trying to increment the amount of cycles a melody will be played once a key is pressed (the key in turn passes a value to the variable selectMel). The problem I encounter is that the moment I call my function in the main void loop() function, I cannot control the incrementation of the variable ncycles, which is intended to be only increased once per key press. I tried to solve this by creating a global switchVar variable as an "on/off" switch but that did not do much to solve the issue at hand.
Any tips or help will be greatly appreciated!
PS: I have of course googled and tried the solutions posted on similar stackExchange posts but to no avail, sadly.
void playMan (int ncycles, int mSpeed) {
switch (selectMel) {
case 1:
if (ncycles > 0) {
tone(12, NOTE_B7, mSpeed);
delay(100);
tone(12, NOTE_B5, mSpeed);
delay(100);
tone(12, NOTE_B3, mSpeed);
delay(100);
tone(12, NOTE_B1, mSpeed);
delay(100);
ncycles--;
break;
case 2:
if (millis() % 100 == 0) {
if (switchVar == 1) {
ncycles += 5;
switchVar = 0;
}
Serial << ncycles << endl;
}
break;
default:
break;
}
}
}