I'm making a arduino music player using a 3.95" tft screen, a mega2560, and a DFPlayer mini.
The code I'm using works, but instead of only displaying 30 volume levels in Serial monitor, it displays 255. How do I fix that error?
[code]
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F
#define MINT 0x8ff8
#define MINTBLUE 0x87ff
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <XPT2046_Touchscreen.h> // Hardware SPI library
char *name = "Touch Shield"; //edit name of shield
const int TS_LANDSCAPE = 1; //XPT2046_TouchScreen.h
const int TS_LEFT = 261, TS_RT = 3770, TS_TOP = 3869, TS_BOT = 296;
#define XPT_CS 53 // MEGA2560 SHIELD
#define XPT_IRQ 255 // XPT2046 library does not like IRQ
XPT2046_Touchscreen ts(XPT_CS, XPT_IRQ);
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
bool pressed = ts.touched();
if (pressed) {
TS_Point p = ts.getPoint();
if (TS_LANDSCAPE) mapxy(p.y, p.x);
else mapxy(p.x, p.y);
}
return pressed;
}
void Touch_init(void)
{
ts.begin();
}
void mapxy(int x, int y) //maps ADC to pixel_x, pixel_y
{
int aspect = tft.getRotation(); //LANDSCAPE
int tft_width = tft.width();
int tft_height = tft.height();
switch (aspect & 3) {
case 0: //PORTRAIT
pixel_x = map(x, TS_LEFT, TS_RT, 0, tft_width);
pixel_y = map(y, TS_TOP, TS_BOT, 0, tft_height);
break;
case 1: //LANDSCAPE
pixel_x = map(y, TS_TOP, TS_BOT, 0, tft_width);
pixel_y = map(x, TS_RT, TS_LEFT, 0, tft_height);
break;
case 2: //PORTRAIT REV
pixel_x = map(x, TS_RT, TS_LEFT, 0, tft_width);
pixel_y = map(y, TS_BOT, TS_TOP, 0, tft_height);
break;
case 3: //LANDSCAPE REV
pixel_x = map(y, TS_BOT, TS_TOP, 0, tft_width);
pixel_y = map(x, TS_LEFT, TS_RT, 0, tft_height);
break;
}
}
#define USE_TOUCH 0x2046
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
Adafruit_GFX_Button pause_btn, play_btn, next_btn, prev_btn, up_btn, down_btn;
bool down;
uint8_t volume = 15;
uint8_t prevVol;
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
tft.begin(0x9488);
Touch_init();
tft.setRotation(1);
tft.fillScreen(BLACK);
myDFPlayer.volume(volume); //Set volume value. From 0 to 30
prevVol = volume;
pause_btn.initButton(&tft, 200, 280, 80, 80, DARKCYAN, MINT, BLACK, "", 2);
play_btn.initButton(&tft, 280, 280, 80, 80, DARKCYAN, MINT, BLACK, "", 2);
next_btn.initButton(&tft, 360, 280, 80, 80, DARKCYAN, MINT, BLACK, ">", 2);
prev_btn.initButton(&tft, 120, 280, 80, 80, DARKCYAN, MINT, BLACK, "<", 2);
up_btn.initButton(&tft, 440, 280, 80, 80, DARKCYAN, MINT, BLACK, "^", 2);
down_btn.initButton(&tft, 40, 280, 80, 80, DARKCYAN, MINT, BLACK, "^", 2);
next_btn.drawButton(false);
play_btn.drawButton(false);
pause_btn.drawButton(false);
prev_btn.drawButton(false);
up_btn.drawButton(false);
down_btn.drawButton(false);
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
tft.fillRect(10, 15, 18, 196, DARKGREY);
myDFPlayer.play(1);
}
void loop(void)
{
down = Touch_getXY();
pause_btn.press(down && pause_btn.contains(pixel_x, pixel_y));
play_btn.press(down && play_btn.contains(pixel_x, pixel_y));
next_btn.press(down && next_btn.contains(pixel_x, pixel_y));
prev_btn.press(down && prev_btn.contains(pixel_x, pixel_y));
up_btn.press(down && up_btn.contains(pixel_x, pixel_y));
down_btn.press(down && down_btn.contains(pixel_x, pixel_y));
if (pause_btn.justReleased()) {
pause_btn.drawButton(true);
tft.fillRect(195, 271, 3, 18, WHITE); //pausebar1
tft.fillRect(205, 271, 3, 18, WHITE); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
}
if (pause_btn.justPressed())
{
pause_btn.drawButton(true);
play_btn.drawButton(false);
next_btn.drawButton(false);
prev_btn.drawButton(false);
up_btn.drawButton(false);
down_btn.drawButton(false);
myDFPlayer.pause();
}
if (play_btn.justReleased()) {
play_btn.drawButton(true);
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, WHITE);
}
if (play_btn.justPressed())
{
pause_btn.drawButton(false);
play_btn.drawButton(true);
next_btn.drawButton(false);
prev_btn.drawButton(false);
up_btn.drawButton(false);
down_btn.drawButton(false);
myDFPlayer.start();
}
if (up_btn.justReleased()) {
up_btn.drawButton();
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
}
if (up_btn.justPressed())
{
pause_btn.drawButton(false);
play_btn.drawButton(false);
next_btn.drawButton(false);
prev_btn.drawButton(false);
up_btn.drawButton(true);
down_btn.drawButton(false);
myDFPlayer.volumeUp();
volume ++;
}
if (down_btn.justReleased()) {
down_btn.drawButton();
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
}
if (down_btn.justPressed())
{
pause_btn.drawButton(false);
play_btn.drawButton(false);
next_btn.drawButton(false);
prev_btn.drawButton(false);
up_btn.drawButton(false);
down_btn.drawButton(true);
myDFPlayer.volumeDown();
volume --;
}
if (next_btn.justReleased()) {
next_btn.drawButton();
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
}
if (next_btn.justPressed())
{
pause_btn.drawButton(false);
play_btn.drawButton(false);
next_btn.drawButton(true);
prev_btn.drawButton(false);
up_btn.drawButton(false);
down_btn.drawButton(false);
myDFPlayer.next();
}
if (prev_btn.justReleased()) {
prev_btn.drawButton();
tft.fillRect(195, 271, 3, 18, BLACK); //pausebar1
tft.fillRect(205, 271, 3, 18, BLACK); //pausebar2
tft.fillTriangle(288, 280, 273, 272, 273, 288, BLACK);
}
if (prev_btn.justPressed())
{
pause_btn.drawButton(false);
play_btn.drawButton(false);
next_btn.drawButton(false);
prev_btn.drawButton(true);
up_btn.drawButton(false);
down_btn.drawButton(false);
myDFPlayer.previous();
}
Volume();
}
void Volume() {
// if (volume = !prevVol) {
// prevVol = volume;
Serial.println(volume);
tft.fillRect(10, 15, 18, 196, DARKGREY);
if (volume == 0) {
tft.fillRect(16, 201, 6, 4, RED);
}
if (volume == 1) {
tft.fillRect(16, 195, 6, 10, RED);
}
if (volume == 2) {
tft.fillRect(16, 189, 6, 16, RED);
}
if (volume == 3) {
tft.fillRect(16, 183, 6, 22, RED);
}
if (volume == 4) {
tft.fillRect(16, 177, 6, 28, RED);
}
if (volume == 5) {
tft.fillRect(16, 171, 6, 34, RED);
}
if (volume == 6) {
tft.fillRect(16, 165, 6, 40, RED);
}
if (volume == 7) {
tft.fillRect(16, 159, 6, 46, RED);
}
if (volume == 8) {
tft.fillRect(16, 153, 6, 52, RED);
}
if (volume == 9) {
tft.fillRect(16, 147, 6, 58, RED);
}
if (volume == 10) {
tft.fillRect(16, 141, 6, 64, RED);
}
if (volume == 11) {
tft.fillRect(16, 135, 6, 70, RED);
}
if (volume == 12) {
tft.fillRect(16, 129, 6, 76, RED);
}
if (volume == 13) {
tft.fillRect(16, 123, 6, 82, RED);
}
if (volume == 14) {
tft.fillRect(16, 117, 6, 88, RED);
}
if (volume == 15) {
tft.fillRect(16, 111, 6, 94, RED);
}
if (volume == 16) {
tft.fillRect(16, 105, 6, 100, RED);
}
if (volume == 17) {
tft.fillRect(16, 99, 6, 106, RED);
}
if (volume == 18) {
tft.fillRect(16, 93, 6, 112, RED);
}
if (volume == 19) {
tft.fillRect(16, 87, 6, 118, RED);
}
if (volume == 20) {
tft.fillRect(16, 81, 6, 124, RED);
}
if (volume == 21) {
tft.fillRect(16, 75, 6, 130, RED);
}
if (volume == 22) {
tft.fillRect(16, 69, 6, 136, RED);
}
if (volume == 23) {
tft.fillRect(16, 63, 6, 142, RED);
}
if (volume == 24) {
tft.fillRect(16, 57, 6, 148, RED);
}
if (volume == 25) {
tft.fillRect(16, 51, 6, 154, RED);
}
if (volume == 26) {
tft.fillRect(16, 45, 6, 160, RED);
}
if (volume == 27) {
tft.fillRect(16, 39, 6, 166, RED);
}
if (volume == 28) {
tft.fillRect(16, 33, 6, 172, RED);
}
if (volume == 29) {
tft.fillRect(16, 27, 6, 178, RED);
}
if (volume == 30) {
tft.fillRect(16, 21, 6, 184, RED);
// }
}
}
[/code]