|
| 1 | +/****************************************************************************** |
| 2 | + Example4_QwiicTwist.ino |
| 3 | +
|
| 4 | + This example shows how to use the Audio Player Breakout with the Qwiic Twist. |
| 5 | + Load your uSD card up with some sound files (MP3 or WAV). |
| 6 | + Wire up the Qwiic Twist and the Mono Audio Amp. |
| 7 | + Press the Qwiic Twist to play the selected track number. |
| 8 | + Rotate the Qwiic Twist to change the track number. |
| 9 | + Watch the serial monitor for feedback. |
| 10 | +
|
| 11 | + Development environment specifics: |
| 12 | + IDE: Arduino 2.3.6 |
| 13 | + Hardware Platform: SparkFun Redboard Qwiic |
| 14 | +
|
| 15 | + By: Pete Lewis, Nathan Seidle |
| 16 | + SparkFun Electronics |
| 17 | + Date: May 5th, 2025 |
| 18 | + License: MIT. See license file for more information but you can |
| 19 | + basically do whatever you want with this code. |
| 20 | +
|
| 21 | + Hardware Connections: |
| 22 | + Use a qwiic cable to connect from the Redboard Qwiic to the Qwiic Twist. |
| 23 | + Connect audio-in, speakers, and power to the Mono Audio Amp. |
| 24 | +
|
| 25 | + Feel like supporting our work? Buy a board from SparkFun! |
| 26 | + MY1690X Serial MP3 Player Shield: https://www.sparkfun.com/sparkfun-serial-mp3-player-shield-my1690x.html |
| 27 | + MY1690X Audio Player Breakout: https://www.sparkfun.com/sparkfun-audio-player-breakout-my1690x-16s.html |
| 28 | +
|
| 29 | + Hardware Connections: |
| 30 | + MY1690 Pin -> Arduino Pin |
| 31 | + ------------------------------------- |
| 32 | + TXO -> 8 |
| 33 | + RXI -> 9 |
| 34 | + VIN -> 5V |
| 35 | + GND -> GND |
| 36 | +******************************************************************************/ |
| 37 | + |
| 38 | +#include <Wire.h> |
| 39 | +#include "SparkFun_MY1690_MP3_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_MY1690 |
| 40 | + |
| 41 | +//For boards that support software serial |
| 42 | +#include "SoftwareSerial.h" |
| 43 | +SoftwareSerial serialMP3(8, 9); //RX on Arduino connected to TX on MY1690's, TX on Arduino connected to the MY1690's RX pin |
| 44 | + |
| 45 | +//For boards that have multiple hardware serial ports |
| 46 | +//HardwareSerial serialMP3(2); //Create serial port on ESP32: TX on 17, RX on 16 |
| 47 | + |
| 48 | +SparkFunMY1690 myMP3; |
| 49 | + |
| 50 | +int songCount; //Number of songs on the SD card |
| 51 | + |
| 52 | +#include "SparkFun_Qwiic_Twist_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_Twist |
| 53 | +TWIST twist; //Create instance of this object |
| 54 | + |
| 55 | +int user_track_number = 1; |
| 56 | +int user_track_number_previous = 1; |
| 57 | + |
| 58 | +void setup() |
| 59 | +{ |
| 60 | + Serial.begin(115200); |
| 61 | + Serial.println("Example 4 - Control sound playback with a Qwiic Twist"); |
| 62 | + |
| 63 | + Wire.begin(); |
| 64 | + |
| 65 | + if (twist.begin() == false) |
| 66 | + { |
| 67 | + Serial.println("Twist does not appear to be connected. Please check wiring. Freezing..."); |
| 68 | + } |
| 69 | + |
| 70 | + // reset twist count to 1 |
| 71 | + twist.setCount(1); |
| 72 | + |
| 73 | + serialMP3.begin(9600); //The MY1690 expects serial communication at 9600bps |
| 74 | + |
| 75 | + if (myMP3.begin(serialMP3) == false) // Beginning the MP3 player requires a serial port (either hardware or software) |
| 76 | + { |
| 77 | + Serial.println(F("MY1690 Device not detected. Check wiring. Freezing.")); |
| 78 | + while (1); |
| 79 | + } |
| 80 | + |
| 81 | + songCount = myMP3.getSongCount(); |
| 82 | + if (songCount == 0) |
| 83 | + { |
| 84 | + Serial.println(F("Oh no! No songs found. Make sure the SD card is inserted and there are MP3s on it. Freezing.")); |
| 85 | + while (1); |
| 86 | + } |
| 87 | + |
| 88 | + Serial.print(F("Number of tracks on SD card: ")); |
| 89 | + Serial.println(songCount); |
| 90 | + |
| 91 | + Serial.print(F("MY1690 Version: ")); |
| 92 | + Serial.println(myMP3.getVersion()); |
| 93 | + |
| 94 | + myMP3.play(); //Will play the lowest numbered song in the folder |
| 95 | + |
| 96 | + myMP3.setVolume(30); //30 is loudest. 5 is comfortable with headphones. 0 is mute. |
| 97 | + |
| 98 | + Serial.print(F("Volume: ")); |
| 99 | + Serial.println(myMP3.getVolume()); |
| 100 | + |
| 101 | + myMP3.setPlayModeNoLoop(); |
| 102 | +} |
| 103 | + |
| 104 | +void loop() |
| 105 | +{ |
| 106 | + if (twist.isPressed()) |
| 107 | + { |
| 108 | + Serial.print(" Pressed! Playing track number: "); |
| 109 | + Serial.println(user_track_number); |
| 110 | + myMP3.playTrackNumber(user_track_number); |
| 111 | + delay(500); |
| 112 | + } |
| 113 | + |
| 114 | + user_track_number = twist.getCount(); |
| 115 | + |
| 116 | + if (user_track_number > songCount) // limit max track number to song count |
| 117 | + { |
| 118 | + user_track_number = songCount; |
| 119 | + twist.setCount(user_track_number); // reset twist count to track number |
| 120 | + } |
| 121 | + else if (user_track_number < 1) // limit min track number to 1 |
| 122 | + { |
| 123 | + user_track_number = 1; |
| 124 | + twist.setCount(user_track_number); // reset twist count to track number |
| 125 | + } |
| 126 | + |
| 127 | + // check if the user has changed the track number |
| 128 | + if (user_track_number != user_track_number_previous) |
| 129 | + { |
| 130 | + user_track_number_previous = user_track_number; |
| 131 | + Serial.print("user_track_number: "); |
| 132 | + Serial.println(user_track_number); |
| 133 | + } |
| 134 | + delay(10); |
| 135 | +} |
0 commit comments