#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#include <AccelStepper.h>
#define TFT_CS 53
#define TFT_DC 49
#define TFT_RST 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define BUTTON_X 100
#define BUTTON_Y 150
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 50
#define BUTTON_COLOR ILI9341_GREEN
#define BACKGROUND_COLOR ILI9341_BLACK
#define TEXT_COLOR ILI9341_WHITE
#define BUTTON_TEXT_SIZE 2
#define XP A1
#define XM A2
#define YM 7
#define YP 6
#define STEP_PIN 12
#define DIR_PIN 11
#define ENABLE_PIN 13
#define MINPRESSURE 10
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
bool isButtonPressed(TSPoint point) {
int x = point.x;
int y = point.y;
return (x >= BUTTON_X && x <= (BUTTON_X + BUTTON_WIDTH) && y >= BUTTON_Y && y <= (BUTTON_Y + BUTTON_HEIGHT) && point.z > MINPRESSURE);
}
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(3);
tft.fillScreen(BACKGROUND_COLOR);
drawButton();
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
stepper.disableOutputs(); // Initially disable stepper outputs
}
void loop() {
TSPoint touch = ts.getPoint();
if (touch.z > 0) {
Serial.println("Touch detected");
// Check if touch is within the button area
if (isButtonPressed(touch)) {
Serial.println("Button pressed");
tft.fillRect(BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, BACKGROUND_COLOR);
tft.setCursor(BUTTON_X + 10, BUTTON_Y + (BUTTON_HEIGHT / 2) - 8);
tft.setTextColor(TEXT_COLOR);
tft.setTextSize(BUTTON_TEXT_SIZE);
tft.println("STOP");
delay(500);
// Move stepper motor continuously
stepper.enableOutputs(); // Enable stepper outputs before moving
stepper.setSpeed(100); // Set the speed of the stepper motor (steps per second)
stepper.runSpeed(); // Start the stepper motor at the specified speed
delay(1000); // Run the motor for 1 second
stepper.disableOutputs(); // Disable stepper outputs after moving
}
}
}
void drawButton() {
tft.fillRect(BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, BUTTON_COLOR);
tft.setCursor(BUTTON_X + 10, BUTTON_Y + (BUTTON_HEIGHT / 2) - 8);
tft.setTextColor(TEXT_COLOR);
tft.setTextSize(BUTTON_TEXT_SIZE);
tft.println("START");
}