#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h> // Capacitive touch screen library
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206(); // Capacitive touch screen object
const int stepPin = 7; // Step pin connected to A4988 STEP pin
const int dirPin = 6; // Direction pin connected to A4988 DIR pin
bool motorRunning = false;
void setup() {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Initialize the TFT screen
tft.begin();
tft.setRotation(3); // Adjust screen orientation if needed
// Initialize the capacitive touch screen
if (!ts.begin(40)) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
// Draw buttons on the screen
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(20, 50, 100, 50, ILI9341_BLUE); // Start button
tft.fillRect(160, 50, 100, 50, ILI9341_RED); // Stop button
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(40, 65);
tft.print("START");
tft.setCursor(180, 65);
tft.print("STOP");
}
void loop() {
// Check for touch input
if (ts.touched()) {
TS_Point p = ts.getPoint(); // Get touch point
if (p.x > 20 && p.x < 120 && p.y > 50 && p.y < 100) {
// Start button pressed
motorRunning = true;
digitalWrite(dirPin, HIGH); // Set direction as needed
Serial.println("Motor started");
} else if (p.x > 160 && p.x < 260 && p.y > 50 && p.y < 100) {
// Stop button pressed
motorRunning = false;
Serial.println("Motor stopped");
}
delay(200); // Debounce delay
}
// Run the motor if it's supposed to be running
if (motorRunning) {
// Write your motor control code here
// For example:
digitalWrite(stepPin, HIGH);
delay(1); // Adjust delay for motor speed
digitalWrite(stepPin, LOW);
delay(1); // Adjust delay for motor speed
}
}