Hi All,
Im working on project using am Arduino Uno R3 and 3.2" Color TFT Touchscreen Breakout v2 as the UI. The LCD is the Breakout Broad Model and I am communicating between the devices using the 8-Bit configuration and I have followed the wiring diagram provided by the touchscreen report.
However, the project I’m working on requires a handful of the Analog I/O pins on the arduino, therefore, I have assigned the LCD control pins to Arduino DIOs.
The issue: I have the Y+ and X- pins assigned to A2 and A3 respectively. When ever I upload my code, the screen displays the appropriate UI, however, the touchscreen doesn’t work. I removed the wires connected between the arduino and the touchscreen pins and the arduino registers this as a “touch” and moved out of the while loops that is in the void setup (). The while loop just waits for a user to hit a start button to move to the main loop.
Additionally, looking at the “paint.ino” example, it’s hard to decipher what digital pin 13 is interfacing with on the LCD. This is possibly where I’m getting hung up as currently my D13 on the arduino is assigned to the LCD_CS pin on the LCD. Any feedback would be greatly appreciated.
See code below:
#include <Arduino.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <EEPROM.h>
#include <IoAbstractionWire.h>
#include <Wire.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 1 // can be a digital pin
#define XP 0 // can be a digital pin
/*Define the area where touchscreen sensing is applicable.*/
#define TS_MINX 50
#define TS_MINY 50
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 363);
/*Defines the pressure needed to sense an action on the touchscreen.*/
#define MINPRESSURE 10
#define MAXPRESSURE 1000
//Define the signals used for I2C communication
#define LCD_CS 13 // Chip Select goes to D13
#define LCD_CD 12 // Command/Data goes to D12
#define LCD_WR 11 // LCD Write goes to D11
#define LCD_RD 10 // LCD Read goes to D10
#define LCD_RESET A4 // RESET goes to D9
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8
// D1 connects to digital pin 9
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
/*Assign human-readable names to some common 16-bit color values:*/
#define BLACK 0x0000
#define WHITE 0xFFFF
#define GREEN 0x07E0
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define BOXSIZE 40
#define PENRADIUS 3
/*Define the Arduino Analog pins that will monitor tablets.*/
/*Assign the digital value that will correspond to light intensity at the sensor.*/
#define lightSensor1 A0
#define lightSensor2 A1
int lightLevel1 = 0;
int lightLevel2 = 0;
/*Define the Trip setpoint that would indicate a video drop out.*/
/*Define constant that would indicate a video drop out. A constant
will need to be assigned for both light sensors*/
const int tripValue = 60;
int vidDrop1 = 0;
int vidDrop2 = 0;
/*Define the count down timer variables.*/
/*previousTime and interval define the update rate.*/
unsigned long timeSec = 18000;
unsigned long previousTime = 0;
const long interval = 1000;
/*Define the EEPROM update varialbes.*/
/*previousWrite and memInterval define the update rate.*/
/*memInterval value represents 5 minutes in milliseconds.*/
unsigned long previousWrite = 0;
const long memInterval = 300000;
/*Define an EEPROM address to store tablet information for each tablet being monitored.*/
int eeAddress1 = 0;
int valAddr1;
int eeAddress2 = 1;
int valAddr2;
/*Define boolean for previous screen measurements. Indicates when the screen recovers after a drop*/
bool previousState_ScreenBlank1 = false;
bool previousState_ScreenBlank2 = false;
bool startTask = false;
void setup(void) {
Serial.begin(9600);
Serial.println(F("Tablet Flicker Test"));
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
Serial.println(F("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
Serial.println(F("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9341) {
Serial.println(F("ILI9341 LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
return;
}
tft.begin(identifier);
valAddr1 = EEPROM.read(eeAddress1);
Serial.println(valAddr1);
valAddr2 = EEPROM.read(eeAddress2);
Serial.println(valAddr2);
/*Displays the results of the previous Tablet Flicker Test.*/
tft.fillScreen(BLACK);
tft.setRotation(3);
tft.setCursor(18,0);
tft.setTextColor(WHITE); tft.setTextSize(3);
tft.print("Previous Results");
tft.setTextSize(2);
tft.setCursor(15, 60);
tft.print("Tablet 1 Total Drops:");
tft.setCursor(280, 60);
tft.print(valAddr1);
tft.setCursor(15, 120);
tft.print("Tablet 2 Total Drops:");
tft.setCursor(280, 120);
tft.print(valAddr2);
tft.fillRect(90, 170, 150, 50, GREEN);
tft.setCursor(122, 185);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("START");
/*Waits for the user to hit the "Start" button before setuping up the main screen and entering "void loop()".*/
while(startTask == false) {
startButton();
}
/*Reinitializes the values in EEPROM to 0.*/
EEPROM.write(eeAddress1, 0);
EEPROM.write(eeAddress2, 0);
/*Sets up the User Interface Display.*/
tft.fillScreen(BLACK);
tft.setRotation(3);
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(2);
tft.println("Tablet Flicker Test");
tft.setCursor(0, 28);
tft.println("Time: ");
tft.setCursor(0, 56);
tft.println("Trip Value: ");
tft.setCursor(150, 56);
tft.print(tripValue);
tft.setCursor(0,132);
tft.setTextSize(2);
tft.print("Tablet 1:");
tft.setCursor(0, 160);
tft.print("Tablet 2:");
tft.setCursor(140, 84);
tft.setTextSize(2);
tft.print("Light");
tft.setCursor(140, 104);
tft.print("Level");
tft.setCursor(250, 84);
tft.print("Video");
tft.setCursor(250, 104);
tft.print("Drops");
}
void loop() {
countDown(); //Function which provides a preset countdown timer in the "HH:MM:SS" format.
monitorForVideoDrops(); //Function which reads the analog inputs to determine if a Tablet Flicker has occurred.
printStatus(); //Function which prints the status of the Tablet's being monitored.
//Setting a delay of 900ms accounts for additional delays in other areas of code.
//This ensures that the countdown timer is within 1 second of accuracy of a Real Time Clock.
delay(900);
}
void countDown () {
unsigned long currentMillis = millis(); //Sets the current time as a value in milliseconds
if(currentMillis - previousTime >= interval) { //Creates a 1 second interval for the countdown timer
previousTime = currentMillis;
}
if (timeSec > 0) { //If statement executes the countdown timer.
//Once the countdown timer reaches "0" this if statement stops executing.
timeSec --;
unsigned long hrs = (timeSec / 3600); //Variable which translates the overall timer value into hours.
unsigned long min = ((timeSec /60) % 60); //Variable which translates the overall timer value into minutes.
unsigned long sec = timeSec % 60; //Variable which translates the overall timer value into seconds.
tft.setCursor(80, 28);
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(2);
if (hrs < 10) {
tft.print(0);
}
tft.print(hrs); tft.print(":");
if (min < 10) {
tft.setCursor(115, 28);
tft.setTextSize(2);
tft.print(0);
}
tft.print(min); tft.print(":");
if (sec < 10) {
tft.setCursor(150, 28);
tft.setTextSize(2);
tft.print("0");
Serial.print(0);
}
tft.print(sec); tft.print(' ');
Serial.println(sec);
}
else if (timeSec = 0) {
EEPROM.update(eeAddress1,vidDrop1); //Writes the number of Video Drops for Tablet 1 to memory when the timer hits 0.
EEPROM.update(eeAddress2,vidDrop2); //Writes the number of Video Drops for Tablet 2 to memory when the timer hits 0.
}
if(currentMillis - previousTime >= 300000) {
previousTime = currentMillis;
EEPROM.update(eeAddress1, vidDrop1);
EEPROM.update(eeAddress2, vidDrop2);
}
}
void monitorForVideoDrops() {
int lightLevel1 = analogRead(lightSensor1); //Reads Arduino Pin A0 value from the light sensor
int lightLevel2 = analogRead(lightSensor2); //Reads Arduino Pin A1 value from the light sensor
if (lightLevel1 < tripValue) {
if (previousState_ScreenBlank1 == false) { //If the light value drops below 60, and the screen was on in the previous state, the vidDrop variable is incremented by 1
vidDrop1++;
previousState_ScreenBlank1 == true;
delay(100); //This delay was added to ride out noisy transitions during drops and recoveries
}
}
else if (lightLevel1 > tripValue) { //If the light value at or above 60 the state is set to NOT blank for the next cycle
previousState_ScreenBlank1 == false;
}
if (lightLevel2 < tripValue) {
if(previousState_ScreenBlank2 == false) { //If the light value drops below 60, and the screen was on in the previous state, the vidDrop variable is incremented by 1
vidDrop2++;
previousState_ScreenBlank2 == true;
delay(100); //This delay was added to ride out noisy transitions during drops and recoveries
}
else if (lightLevel2 > tripValue) { //If the light value at or above 60 the state is set to NOT blank for the next cycle
previousState_ScreenBlank2 == false;
}
}
}
void printStatus() {
tft.setTextColor(WHITE, BLACK);
tft.setCursor(165, 132);
tft.setTextSize(2);
tft.print(lightLevel1);
tft.setCursor(275, 132);
tft.print(vidDrop1);
tft.setCursor(165, 160);
tft.print(lightLevel1);
tft.setCursor(275, 160);
tft.print(vidDrop2);
}
void startButton() {
TSPoint p = ts.getPoint();
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
startTask = true;
Serial.println(p.x);
}
}