Note! There are still active bugs and unoptimized code that I am too lazy to troubleshoot and fix. This is all for reference only and will not receive any bug fixing updates.
Song: Looping the Rooms / rusino
This code uses a PID control loop for the DC motor with built-in encoder and utilizes external interrupts to count the encoder pulse. Timer 1 is also being used to provide an updated target count every 2 beats of the song.
When the button is pressed, the song will start playing from the speaker and the motor will rotate every 2 beats (1 beat was too fast and unstable). The only way to stop is to either restart the board or take out the power as intended :)
-
USE AN EXTERNAL POWER SUPPLY FOR DEVICES LIKE THE DC MOTOR Microcontrollers like an Arduino are not design to output enough current for power hungry device like motors and speakers. Too much current draw can "brick" a component for good so be warned!
-
NO SERVO CONTROL/NO PIN 9 AND 10 FOR PWM DUE TO TIMER 1 This program has configured timer1 to move the motor on beat of the song therefore disrupts any normal use of pin 9 and 10 PWM output and all servo controls. More info can be found here: https://deepbluembedded.com/arduino-timer-interrupts/
-
Datasheet for the DFPlayer Mini suggest using a "resistor in series" between the microcontroller and DFPlayer if the microcontroller output is 5V. However, I think they meant voltage divider circuit because DFPlayer uses 3V3 and Arduino uses 5V and this resistor will only limit the current going in the player. But what do I know? I'm not an engineer.
- The Arduino is being powered by the regulated 5V line from the MDD3A motor driver. Please research alternative ways of powering the Arduino before attempting to do this as there is no protection for reverse polarity or overvoltage. In this case, you can simply power the Arduino via USB connection :) https://docs.arduino.cc/learn/electronics/power-pins/#3v35v-pin
- Motor encoder: https://www.youtube.com/watch?v=dTGITLnYAY0
- Timer code generator: https://deepbluembedded.com/arduino-timer-calculator-code-generator/
- DFPlayer Mini: https://www.youtube.com/watch?v=7WiSeQxb1bU
If you find yourself using this code to add to your project or make a video/tutorial with it, I simply ask that you credit me @lenpai0 and provide a link to this page so everything is traceable. Thanks and enjoy!