Key is a character from a keyboard, which works ok, but the case doesnt seem to be switching the value of sp_period. (the Serial.println doesnt execute when key is 5.
switch (key) {
case 0: sp_period = 10000; break; // stop motor!
case 1: sp_period = 833; break;
case 2: sp_period = 625; break;
case 3: sp_period = 416; break;
case 4: sp_period = 294; break;
case 5: sp_period = 188; Serial.println (key); break;
case 6: sp_period = 156; break;
default: sp_period = 10000; break;
}
case 0 is redundant as it's identical in action to the default case.
Unless you're trying to switch an enum or a char or something odd, this switch should work.
Can you expand your code please, the problem may be seen here but it likely happens elsewhere.
@v_mad you may be confusing the 5 in your switch statement - which is a integer, with the character '5' which is actually stored as the integer 53 (see ASCII character tables).
EDIT: Or change the comparison in the case statement as @Danois90 just pointed out.
Thank you to all. You were right, the key was a char type, so I needed to use '1' rather than 1 etc. Now working fine.
I am very rusty on coding having had a 3+ year break, but I hope to be able to get back up to speed soon.
Cheers