Hello All
I can get the image on the screen just fine but when I touch the image, the system reboots so I went with a suggestion for now just to draw a rectangle and put code to it.
Here is the code I am using and I can get the rectangles to display, the text to appear inside but the code for touching the screen and playing an associated MP3 file is not working .
Can anyone see what could be the problem.
Here is my entire code for the RA and VS devices...
First everything is tested and a sine wave plays to ensure that the audio player is working.
Then I draw out the rectangles with the phrases inside.Then I detect the touches which put out the X and Y points touched.
Finally, I have it where the display is touched within certain X&Y points, it should play an MP3 audio file depending on where you touch and that is not working.
Any suggestions is appreciated on how to get the audio to play when the screen is touched is appreciated!
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#include <Adafruit_VS1053.h>
#include <SD.h>
// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 2
#define RA8875_CS 9
#define RA8875_RESET 8
#define RA8875_X+ A1
#define RA8875_X- A2
#define RA8875_Y+ A3
#define RA8875_Y- A4
#define CLK 13 // SPI Clock, shared with SD card
#define MISO 12 // Input data, from VS1053/SD card
#define MOSI 11 // Output data, to VS1053/SD card
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 MCS - chip select pin (output)
#define SHIELD_DCS 6 // VS1053 DCS - data select pin (output)
#define CARDCS 4 // VS1053 CCS - Card chip select pin
#define DREQ 3 // VS1053 DREQ - Data request, ideally an Interrupt pin
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);;
Adafruit_GFX_Button btn1;
uint16_t tx, ty;
void setup()
{
Serial.begin(9600);
Serial.println("RA8875 start");
/* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("Found RA8875");
/* Initialize the music player */
if (! musicPlayer.begin()) {
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
musicPlayer.sineTest(0x44, 500); // Makes a tone to indicate VS1053 is working
Serial.println(F("VS1053 Audio Test Completed - Beep"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println(F("SD Found"));
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(5,5);
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
// With hardware accelleration this is instant
tft.fillScreen(RA8875_WHITE);
pinMode(RA8875_INT, INPUT);
digitalWrite(RA8875_INT, HIGH);
tft.touchEnable(true);
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
tft.fillScreen(RA8875_BLACK);
/* Switch to text mode */
tft.textMode();
/* Set the cursor location (in pixels) */
tft.textSetCursor(10,10);
// PWM Use
for (uint8_t i=255; i!=0; i-=5 )
{
tft.PWM1out(i);
delay(10);
}
for (uint8_t i=0; i!=255; i+=5 )
{
tft.PWM1out(i);
delay(10);
}
tft.PWM1out(255);
tft.fillScreen(RA8875_RED);
delay(500);
tft.fillScreen(RA8875_YELLOW);
delay(500);
tft.fillScreen(RA8875_GREEN);
delay(500);
tft.fillScreen(RA8875_CYAN);
delay(500);
tft.fillScreen(RA8875_MAGENTA);
delay(500);
tft.fillScreen(RA8875_BLACK);
// GFX acceleration
// first number indicates where box starts (600)
// second number indicates how far from the top it starts (10)
// third number indicates how wide the box is (200)
// fourth number indicates how high the box is (100)
tft.drawRect(10, 150, 300, 200, RA8875_GREEN);
tft.drawRect(350, 10, 200, 200, RA8875_CYAN);
tft.drawRect(575, 10, 200, 200, RA8875_CYAN);
tft.drawRect(350, 255, 200, 200, RA8875_CYAN);
tft.drawRect(575, 255, 200, 200, RA8875_CYAN);
/* Change the cursor location and color ... */
// first number indicates how far from the left side the screen the words are (40)
// second number indicates what how far from the top of the screen it is (210)
tft.textSetCursor(40, 210);
tft.textTransparent(RA8875_WHITE);
tft.textEnlarge(3);
tft.textWrite("I Want");
tft.textSetCursor(350, 38);
tft.textTransparent(RA8875_WHITE);
tft.textEnlarge(2);
tft.textWrite("A Drink");
tft.textSetCursor(575, 38);
tft.textTransparent(RA8875_WHITE);
tft.textEnlarge(2);
tft.textWrite("My iPad");
tft.textSetCursor(350, 275);
tft.textTransparent(RA8875_WHITE);
tft.textEnlarge(2);
tft.textWrite("To Eat");
tft.textSetCursor(575, 275);
tft.textTransparent(RA8875_WHITE);
tft.textEnlarge(2);
tft.textWrite("Potty");
}
void loop()
{
float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
int Xposn, Yposn;
if (tft.touched()) { //Verify the touched area
tft.touchRead(&tx, &ty);
Serial.print(F("Touch: "));
Serial.print(tx); Serial.print(", "); Serial.println(ty);
if (tx > 580 && tx < 680 && ty > 370 && ty < 535)
// code to try and get the touch screen areas to play a file when touched
if ((tx>=80) && (tx<=400) && (ty>=370) && (ty<=700))
{musicPlayer.startPlayingFile("001.mp3");}
if ((tx>=440) && (tx<=690) && (ty>=160) && (ty<=470))
{musicPlayer.startPlayingFile("002.mp3");}
if ((tx>=730) && (tx<=930) && (ty>=180) && (ty<=480))
{musicPlayer.startPlayingFile("003.mp3");}
if ((tx>=460) && (tx<=690) && (ty>=570) && (ty<=875))
{musicPlayer.startPlayingFile("004.mp3");}
if ((tx>=730) && (tx<=947) && (ty>=930) && (ty<=913))
{musicPlayer.startPlayingFile("005.mp3");}
}
}
Top Comments