Projects with more than one Arduino
The other day I introduced a friend to Arduino. They already have a background in code but hadn’t touched anything embedded for a minute. For a starter project we talked about making music with the Arduino using piezeo speakers and the Arduino tone library.
I put together an example project just to make sure i had the answers to any questions and realized the tone library is pretty fun for lofi chip music. You have a basic squarewave beep for melodies, using random() you can generate a psuedo-white noise sound that works well for percussion. If you give random a range to ‘filter’ the noise you can make a snare drum sound different from a hihat sound. Kick drums can be made with a fast descending note, pitching it up for toms:
I wanted to make a bassline play with a drum track. Due to the single timer available on the Arduino Uno this is not possible. I found 3 more Arduinos and decided to make an ‘orchestra’.
I implemented a basic sequencer using a 16 int array and the timer method from BlinkWithoutDelay.ino and started making beepy noises using 2 Arduinos. I wrote a beat, uploaded it to Arduino1, sequenced some frequencies and uploaded them to Arduino2 from the same (now edited) sketch. Commenting code in and out for the difference sequences was not really ideal, nor was the idea of 4 separate sketches.
Using Arduino EEPROM library I figured out how to get each individual Arduino to do different things when programmed from the same sketch. This works well in this case as a lot of the code is shared, it is just the musical sequence that changes from Arduino1..4.
It is a 2 step process:
-
Using the EEPROM I tagged each Arduino with a unique ID that is stored in the Arduino’s ‘permanent’ memory. Then I removed the tagging code; no more writing to EEPROM.
-
With the now ID’d Arduinos, in the setup() function I look up the ID number and then using a conditional I can set each Arduino to send a different pattern to the sequencer.
You still have to program each Arduino by swapping ports in the IDE but once the initial EEPROM ID setup is done you don’t need to worry about accidentally erasing one sequence for another.
Synchronising the sequencers is currently done by power cycling all Arduinos at the same time using a USB hub, interestingly they fall out of sync with each other after about 5 minutes. It is a mix of Genuino and clone hardware so maybe there is a difference in the crystal??? Anyway, this can be fixed by implementing the sequencer on Arduino1 and slave-ing Arduino2..4 via an interrupt pin, which i will probably do next.
hardware
Hi TZ! you need:
- an Arduino, which you have
- a breadboard and some wire
- some beepy buzzers
- an 100 Ohm resistor
Connect one leg of the speaker to pin 7. Connect the other leg to the 100Ohm resistor and then to ground. Use the tone library to make beeps.