/*Explanation:
Includes: Include the necessary libraries for the display, touch screen, and graphics.
Pin Definitions: Define the pins used for the display and touch screen.
Calibration Values: These values are used to map the touch screen coordinates to the display's resolution. Adjust them based on your specific touch screen.
Display and Touch Screen Objects: Create instances of the display and touch screen classes.
Global Variables: Use global variables to keep track of drawing state (e.g., isDrawing, prevX, prevY).
Setup: Initialize the display, set its orientation, and clear the screen.
Loop:
Get Touch Point: Read touch screen data.
Map Coordinates: Map the touch screen coordinates to the display's resolution.
Drawing Logic:
If the touch is pressed (within the pressure range), start or continue drawing:
If it's the start of a stroke, update prevX and prevY.
Otherwise, draw a line from the previous touch point to the current touch point.
If the touch is released, stop drawing.
To Use This Code:
Connect Display and Touchscreen: Connect the ILI9486 display and touch screen to your Arduino Mega according to the pin assignments.
Calibrate Touchscreen: Run the touch screen calibration sketch provided with your touchscreen library to ensure accurate touch input.
Upload Sketch: Upload the code to your Arduino Mega.
Once uploaded, you can use your finger to draw on the display. The code will draw white lines on the black background. You can customize the color, line thickness, and other drawing properties as needed.
Explanation2:
Button Dimensions: The code defines variables buttonX, buttonY, buttonWidth, and buttonHeight to specify the position and size of the button. Adjust these values to position the button where you want it.
Color Variable: The code introduces a currentColor variable, initially set to ILI9486_WHITE, to store the current drawing color.
Button Drawing: In the setup() function, the code draws a red rectangle (the button) on the display using tft.fillRect().
Touch Detection: In the loop() function, the code checks if the touch coordinates are within the button's dimensions (buttonX, buttonY, buttonWidth, buttonHeight) and if the touch pressure is within the valid range.
Color Change: If the button is touched, the code toggles the currentColor between white and red using a ternary operator:
currentColor = (currentColor == ILI9486_WHITE) ? ILI9486_RED : ILI9486_WHITE;
This concisely changes the currentColor to ILI9486_RED if it was previously white, and to ILI9486_WHITE if it was previously red.
Redraw Button: The code redraws the button with the new color using tft.fillRect().
Drawing Logic: The rest of the loop() function handles the drawing logic (drawing lines using currentColor as the line color), as it did before.
Now, when you touch the red button in the top-left corner of the display, the drawing color will toggle between white and red. You can further extend this by adding more buttons to change the drawing color to other colors!
*/
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9486.h"
#include "Adafruit_TouchScreen.h"
#include <TouchScreen.h>
// Define display pins
#define DC 10
#define CS 9
#define RST 8
#define RD 7
#define WR 6
// Define data pins
#define D0 22
#define D1 23
#define D2 24
#define D3 25
#define D4 26
#define D5 27
#define D6 28
#define D7 29
// Define touch screen pins
#define YP A1 // must be an analog pin, use 'A1' or 'A2'
#define XM A2 // must be an analog pin, use 'A1' or 'A2'
#define YM 34
#define XP 35
// Calibration values (adjust based on your touch screen)
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 930
#define TS_MAXY 940
// Create an instance of the display
Adafruit_ILI9486 tft = Adafruit_ILI9486(DC, CS, RST, RD, WR, D0, D1, D2, D3, D4, D5, D6, D7);
// Create an instance of the touch screen
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// Global variables for drawing
int prevX = 0;
int prevY = 0;
bool isDrawing = false;
// Color variable
uint16_t currentColor = ILI9486_WHITE;
// Button dimensions (adjust as needed)
int buttonX = 10;
int buttonY = 10;
int buttonWidth = 50;
int buttonHeight = 30;
void setup() {
Serial.begin(9600);
tft.begin(ILI9486_DRIVER);
tft.setRotation(1); // Adjust the rotation as needed
tft.fillScreen(ILI9486_BLACK);
// Draw the button on the display
tft.fillRect(buttonX, buttonY, buttonWidth, buttonHeight, ILI9486_RED);
}
void loop()
{
TSPoint p = ts.getPoint();
// Map the touch coordinates to the display's resolution
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
// Check if the button is touched
if (p.x >= buttonX && p.x <= buttonX + buttonWidth &&
p.y >= buttonY && p.y <= buttonY + buttonHeight &&
p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
// Change color
currentColor = (currentColor == ILI9486_WHITE) ? ILI9486_RED : ILI9486_WHITE;
// Redraw the button to show the color change
tft.fillRect(buttonX, buttonY, buttonWidth, buttonHeight, currentColor);
}
// Drawing logic
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
if (!isDrawing)
{
isDrawing = true;
prevX = p.x;
prevY = p.y;
}
else
{
tft.drawLine(prevX, prevY, p.x, p.y, currentColor);
prevX = p.x;
prevY = p.y;
}
}
else
{
isDrawing = false;
}
}