12/2/2023 0 Comments Arduino piezo codeIn terms of MIDI handling, I didn’t bother with the MIDI library for this one. In the end I’ve gone with 100mS which is a good mix of responsiveness and “debouncing”. I initially set the exclude time to 50mS but that was proving too short, causing repeat triggers for a single hit. I mean some) mechanical design would help here.īut from this, I can see a threshold of 300 would probably trigger nicely. This is probably down to my crude taping of the sensors to the lids and the way they flex after being struck. Here are some plots from some of the sensors.Īs you can see, there is a lot of variation and noise in the signals. What I can’t do is estimate the time they are taking to die away again – the plotter just plots numbers, although I could probably do some guestimating from the clock cycle time of the Arduino if I really needed to know (although at this point, breaking out a real oscilloscope to the sensor’s signal might be easier). With this enabled, I can take some readings and get an idea for what levels are being generated when the sensors hit. There is a great tutorial on how to use it from Adafruit here. This will take a stream of numbers from the serial port and plot them on a graph. Find it under “Tools” – “Serial Plotter”, match the baud rate (I’ve used 115200 in this example) and off you go. ![]() Now the Arduino IDE has a neat feature that I don’t see mentioned very much, but to be honest I’ve not really used much myself either, but is really useful for this kind of thing. Then the code will collect the readings from one of the sensors and output them all to the serial port as a string of numbers. #define CALIBRATE 1 // NB: Need both DEBUG and CALIBRATE to calibrate sensors. If you remove the comment (“//”) from following two lines in the code: //#define DEBUG 1 // Will print MIDI information to serial port rather than send over MIDI. ![]() The best way is to include a “calibration” mode, which is something I’ve done. I only read one analog port at a time as the reading itself takes a bit of processing time, so I share it out in between processing the other sensors expiry.Īll well and good so far, but how do you choose the thresholds for triggering and expiry times? That will depend on your sensor, how its mounted, how hard it can be hit, and how much flexibility of movement in the sensor afterwards. I also use the expiry of the “excluded” time to trigger a MIDI note off. This means that timer will eventually wrap around back to zero after 50 years or so, so for these purposes it works fine. The millis() call returns an unsigned long, that is a number between 0 and 4,294,967,295. When it comes to checking the sensor, if the latest reading of the millisecond timer is still within the “excluded” time, I don’t try to read it again. When the sensor is triggered I read the current millisecond counter using millis() which returns the number of milliseconds since the Arduino was turned on. IF so, then record the current time in milliseconds Is the value over the THRESHOLD for triggering? IF the sensor was activated previously THENĮLSE IF the sensor is the one we're reading THEN My approach is largely as follows: For each time round the Arduino's loop: ![]() But to keep track of what is going on for several sensors at the same time gets a bit more fiddly. To do that for a single input is fairly easy – once triggered, just don’t read it again for a short while. The trick is to avoid re-triggering quickly as the signal from the sensor will bounce around a fair bit. The basic principle, as described in the Knock tutorial, is to read the analog port and when the value rises above a certain threshold, treat it as being “triggered”. ![]() The code to handle piezo sensors is relatively straight forward. If you want a proper drum sensor, have a look (and admire) Marco’s build. Neither things of which I’ve done in the above! The other projects I’ve listed really do this properly. In particular having a good interface between your sensor and the surface is important, as it reducing the direct force on the sensor itself. This kept things very simple, but isn’t very robust.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |