#include <Wire.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// Display and touchscreen setup
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
// Motor control and sensors
const int stepPin = 4;
const int dirPin = 5;
const int enablePin = 6;
const int irSensorPin = 7;
const int relayPin = 8; // Relay control pin
volatile bool motorRunning = false;
volatile bool relayActive = false; // Relay is initially inactive
unsigned long irSensorTimestamp = 0;
unsigned long resumeMotorTime = 0; // Time to resume motor after IR sensor sensing
const int stepsPerRevolution = 6400;
int irSensorCounter = 0;
int holderLimit = 10; // Set initial holder limit
bool objectDetected = false;
// Button dimensions for touchscreen
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 50
#define BUTTON_PADDING 10
// Define button colors
#define DEFAULT_COLOR ILI9341_WHITE
#define PRESSED_COLOR ILI9341_BLUE
struct Button {
int x;
int y;
const char* label;
uint16_t color; // Default color
uint16_t pressedColor; // Color when pressed
bool isPressed;
};
Button buttons[] = {
{10, 140, "Start", DEFAULT_COLOR, PRESSED_COLOR, false},
{120, 140, "Stop", DEFAULT_COLOR, PRESSED_COLOR, false},
{10, 200, "Up", DEFAULT_COLOR, PRESSED_COLOR, false},
{120, 200, "Down", DEFAULT_COLOR, PRESSED_COLOR, false},
{10, 260, "Reset", DEFAULT_COLOR, PRESSED_COLOR, false}
};
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(irSensorPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT); // Set the relay pin as an output
digitalWrite(enablePin, LOW);
digitalWrite(dirPin, LOW);
tft.begin();
if (!ctp.begin(40)) {
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
tft.fillScreen(ILI9341_BLACK);
drawButtons();
updateDisplay();
Serial.begin(115200);
while (!Serial);
}
void drawButton(Button& button) {
tft.fillRect(button.x, button.y, BUTTON_WIDTH, BUTTON_HEIGHT, button.color);
tft.drawRect(button.x, button.y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_BLACK);
tft.setCursor(button.x + 10, button.y + 20);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print(button.label);
}
void drawButtons() {
for (int i = 0; i < 5; i++) {
drawButton(buttons[i]);
}
}
void updateDisplay() {
tft.fillRect(0, 0, 240, 50, ILI9341_BLACK); // Clear the area
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Holder Limit: ");
tft.print(holderLimit);
tft.setCursor(10, 40);
tft.print("Holder Count: ");
tft.print(irSensorCounter);
}
void loop() {
handleIrSensor();
handleMotorControl();
handleTouchscreen();
}
void handleTouchscreen() {
if (!ctp.touched()) {
// Release all buttons when no touch detected
for (int i = 0; i < 5; i++) {
if (buttons[i].isPressed) {
buttons[i].isPressed = false;
buttons[i].color = DEFAULT_COLOR;
drawButton(buttons[i]); // Reset button color to default
}
}
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
for (int i = 0; i < 5; i++) {
if (p.x >= buttons[i].x && p.x <= buttons[i].x + BUTTON_WIDTH &&
p.y >= buttons[i].y && p.y <= buttons[i].y + BUTTON_HEIGHT) {
if (!buttons[i].isPressed) {
buttons[i].isPressed = true;
buttons[i].color = buttons[i].pressedColor;
drawButton(buttons[i]);
}
Serial.println(buttons[i].label);
if (strcmp(buttons[i].label, "Start") == 0) {
motorRunning = true;
digitalWrite(enablePin, LOW);
digitalWrite(dirPin, LOW);
irSensorCounter = 0; // Reset the counter when motor starts
Serial.println("Start button pressed");
} else if (strcmp(buttons[i].label, "Stop") == 0) {
motorRunning = false;
digitalWrite(enablePin, HIGH);
digitalWrite(relayPin, LOW); // Deactivate the relay
Serial.println("Stop button pressed");
} else if (strcmp(buttons[i].label, "Up") == 0) {
holderLimit++;
updateDisplay();
} else if (strcmp(buttons[i].label, "Down") == 0) {
if (holderLimit > 0) {
holderLimit--;
updateDisplay();
}
} else if (strcmp(buttons[i].label, "Reset") == 0) {
irSensorCounter = 0; // Reset the holder count
updateDisplay();
Serial.println("Reset button pressed");
}
} else if (buttons[i].isPressed) {
// Button released outside its area
buttons[i].isPressed = false;
buttons[i].color = DEFAULT_COLOR;
drawButton(buttons[i]);
}
}
}
void handleIrSensor() {
if (digitalRead(irSensorPin) == LOW) { // Check if an object is detected by the IR sensor
irSensorTimestamp = millis(); // Update timestamp when the IR sensor detects something
if (!objectDetected && motorRunning) {
irSensorCounter++; // Increment the counter if object is not already detected and motor is running
updateDisplay(); // Update the display
objectDetected = true; // Set flag to true to indicate object detection
}
if (!relayActive && motorRunning) {
relayActive = true; // Relay is active after IR sensor trigger and motor is running
delay(2000);
digitalWrite(relayPin, HIGH); // Turn on the relay
delay(10000);
}
} else {
if (relayActive && motorRunning) {
relayActive = false; // Relay is not active when the IR sensor is not triggered and motor is running
digitalWrite(relayPin, LOW); // Turn off the relay
}
objectDetected = false; // Reset flag when no object is detected
}
// Stop motor if counter value reaches the maximum limit
if (irSensorCounter >= holderLimit && motorRunning) {
motorRunning = false;
digitalWrite(enablePin, HIGH);
digitalWrite(relayPin, LOW); // Deactivate the relay
updateDisplay(); // Update the display
Serial.println("Motor stopped automatically as counter reached the maximum limit.");
}
}
void handleMotorControl() {
if (motorRunning) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(400);
digitalWrite(stepPin, LOW);
delayMicroseconds(400);
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch