Drum Pad coding help requested

NickApparently

New member
Hey All!

I'm programming a Teensy 4.0 for my Drum Pad. The thing runs fine, but I feel that the software could be improved a lot. I'm a pretty decent programmer for industrial PLCs, but when it comes to Arduino code I'm pretty novice.

I think the code could mainly be improved by using classes and objects, but since the most functions I use are pretty simple voids I never got around to really understand classes and objects. Now it feels like a big hurdle to change the code with classes and objects. Next to that, the code for the menu (and the display accordingly) are huge. I couldn't think of, or find, any simpler way to program the menu, but still feel like there should be a better way.

Quick rundown of the Drum Pad:
There are a total of 6 pads, 2 external inputs and 2 external foot-switch inputs.
Hits are detected by a FSR sensor. Next the velocity is measured by a piezo element. According to multiple settings a Midi command is sent.
Settings are changeable in the menu. An encoder and a 1602 LCD-display make it possible to navigate through the menu.
Settings are saved in one of 9 slots.

Performance wise, it would be great if the program wouldn't "pause" when writing to the LCD, but I'm not sure if thats possible.

Now my question is: Would anyone be so kind to look through my code and give me pointers on how to improve?
If anything needs to be explained, please let me know.

https://github.com/NickApparently/TeensyDrum/blob/main/src/TeensyDrum.ino

~
 
Writing to the LCD... I use this technique to avoid the LCD hogging the main loop:

1. Set up an interval timer to run every 1 ms
2. Create a basic ring buffer for your characters, 128 characters is more than enough and Teensy isn't short of RAM.
3. Create consumer and producer pointers into the ring.
4. The main code drops a character into the ring and increments the consumer pointer, wrapping when you hit to top of the buffer
5. The timer code empties the ring one character at a time and writes it to the LCD, incrementing the producer point and wrapping when the producer pointer hits the top of the buffer
6. When the producer pointer and the consumer pointer are the same then the buffer is empty and you're all caught up.

Hope this helps.
 
Wow, thanks! That's a brilliant idea.
Is there a specific reason why you'd use a consumer pointer instead of a byte variable?

Apart from that, a discussion with a colleague made me realize the biggest thing I could use for 'objects' are the pads itself. The pads are objects I could reuse code-wise, doih. I just never realized it. Still looking for other improvements :D
 
Wow, thanks! That's a brilliant idea.
Is there a specific reason why you'd use a consumer pointer instead of a byte variable?

Apart from that, a discussion with a colleague made me realize the biggest thing I could use for 'objects' are the pads itself. The pads are objects I could reuse code-wise, doih. I just never realized it. Still looking for other improvements :D

I have written a simple Ring Buffer Library which you can see here. I hope it will be useful to you or give you ideas for your own.
 
Back
Top