#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <Servo.h> // Servo library
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Servo myservo;
int relayPin = 4;
int servoPin = 3;
// Size of the interface elements
#define SLIDER_WIDTH 200
#define SLIDER_HEIGHT 50
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 50
void setup(void) {
Serial.begin(115200);
Serial.println(F("Cap Touch Control!"));
tft.begin();
if (!ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(ILI9341_BLACK);
// Draw the interface
drawInterface();
// Set up Servo
myservo.attach(servoPin);
myservo.write(90); // start at middle position
// Set up Relay
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // start with motor off
}
void loop() {
// Wait for a touch
if (!ctp.touched()) {
return;
}
// Retrieve a point
TS_Point p = ctp.getPoint();
// flip it around to match the screen.
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
// Print out the coordinates
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
// Check for slider touch
if (p.y > 50 && p.y < 50 + SLIDER_HEIGHT) {
int servoPos = map(p.x, 20, 20 + SLIDER_WIDTH, 0, 180);
myservo.write(servoPos);
tft.fillRect(20, 50, SLIDER_WIDTH, SLIDER_HEIGHT, ILI9341_WHITE); // Clear previous slider position
tft.fillRect(servoPos + 20, 50, 10, SLIDER_HEIGHT, ILI9341_RED); // Draw new slider position
}
// Check for Motor ON button touch
else if (p.y > 150 && p.y < 150 + BUTTON_HEIGHT) {
if (p.x > 20 && p.x < 20 + BUTTON_WIDTH) {
digitalWrite(relayPin, HIGH); // turn motor on
tft.fillRect(20, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_GREEN); // highlight ON button
tft.fillRect(150, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_WHITE); // clear OFF button
}
// Check for Motor OFF button touch
else if (p.x > 150 && p.x < 150 + BUTTON_WIDTH) {
digitalWrite(relayPin, LOW); // turn motor off
tft.fillRect(20, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_WHITE); // clear ON button
tft.fillRect(150, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_RED); // highlight OFF button
}
}
}
void drawInterface() {
// Draw slider
tft.fillRect(20, 50, SLIDER_WIDTH, SLIDER_HEIGHT, ILI9341_WHITE); // Slider background
tft.fillRect(110, 50, 10, SLIDER_HEIGHT, ILI9341_RED); // Initial slider position
// Draw Motor ON button
tft.fillRect(20, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_WHITE); // Motor ON button background
tft.drawRect(20, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_GREEN); // Motor ON button border
tft.setCursor(45, 170);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print("ON");
// Draw Motor OFF button
tft.fillRect(150, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_WHITE); // Motor OFF button background
tft.drawRect(150, 150, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_RED); // Motor OFF button border
tft.setCursor(175, 170);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print("OFF");
}
Loading
ili9341-cap-touch
ili9341-cap-touch