/***************************************************
Touchscreen painting example for the Adafruit ILI9341 captouch shield
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
//thin filmed transistor
#include <TFT_eSPI.h>
//Serial Peripheral Interface - communicating with one or more peripheral devices quickly over short distances
#include <SPI.h> // this is needed for display
//this does all the low level chatting with the FT6206 driver chip
#include <Wire.h> // this is needed for FT6206
//library for the Adafruit FT6206-Based capacitive touch screens and displays
#include <Adafruit_FT6206.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi.h>
// The FT6206 uses hardware I2C (SCL/SDA)
// capacitive touch sensor
Adafruit_FT6206 ctp = Adafruit_FT6206();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
//The DC or D/C pin is needed by some devices to distinguish between commands or data for the controler
#define TFT_DC 2
//Master In / Slave Out
#define TFT_MOSI 23
//Serial ClocK
#define TFT_SCLK 18
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height
#define ILI9341_RED TFT_RED
#define ILI9341_YELLOW TFT_YELLOW
#define ILI9341_GREEN TFT_GREEN
#define ILI9341_BLACK TFT_BLACK
#define ILI9341_BLUE TFT_BLUE
#define ILI9341_MAGENTA TFT_MAGENTA
// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
#define PENRADIUS 3
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "iv_chitakov"
#define AIO_KEY "aio_maay25iPJc3sGQeyTnAju6NtLbeo"
int oldcolor, currentcolor;
int countRed = 0;
int countYellow = 0;
int countGreen = 0;
int countReset = 0;
int countBlue = 0;
int countMagenta = 0;
int x_old = -1;
int y_old = -1;
WiFiClient client;
// BLock for connection with Adafruit IO
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
boolean MQTT_connect();
boolean MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return true;
} uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
mqtt.disconnect();
delay(2000);
retries--;
if (retries == 0) {
return false;
}
} return true;
}
// Function for connection to Adafruit IO and data about the feed we are going to work with
Adafruit_MQTT_Publish pushButtonSensor = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Capacitive Touch Sensor");
void setup(void) {
Serial.begin(115200);
Serial.println(F("Cap Touch Paint!"));
tft.begin();
//Capacitive Touch Panel
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);
// make the color selection boxes
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, ILI9341_BLACK);
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
// select the current color 'red'
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
currentcolor = ILI9341_RED;
delay(3000);
WiFi.begin("Wokwi-GUEST", ""); // Use username and pass to coonect to Wokwi public network
while ((!(WiFi.status() == WL_CONNECTED))) { //Is connection successful?
delay(500);
Serial.print("Свързване...");
} // Print information about the network we are connected
Serial.println("Установена е връзка.");
Serial.println("Вашият IP е:");
Serial.println((WiFi.localIP()));
}
void clearScreen()
{
currentcolor = ILI9341_RED;
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, ILI9341_BLACK);
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
countReset += 1;
Serial.print("Pixels in RED:"); Serial.println(countRed);
Serial.print("Pixels in YELLOW:"); Serial.println(countYellow);
Serial.print("Pixels in GREEN:"); Serial.println(countGreen);
Serial.print("Pixels in BLUE:"); Serial.println(countBlue);
Serial.print("Pixels in MAGENTA:"); Serial.println(countMagenta);
Serial.print("RESETS:"); Serial.println(countReset);
countRed = 0;
countYellow = 0;
countGreen = 0;
countBlue = 0;
countMagenta = 0;
delay(50);
if (MQTT_connect()) { // Try to connect to Adafruit IO
if (pushButtonSensor.publish(countReset)) { //If connection is successful, publish the value
}
}
}
void loop() {
// Wait for a touch
if (! ctp.touched()) {
delay(10); // Speeds up the simulation
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);
if (p.y < BOXSIZE) {
oldcolor = currentcolor;
if (p.x < BOXSIZE) {
currentcolor = ILI9341_RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE * 2) {
currentcolor = ILI9341_YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE * 3) {
currentcolor = ILI9341_GREEN;
tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE * 4) {
clearScreen();
} else if (p.x < BOXSIZE * 5) {
currentcolor = ILI9341_BLUE;
tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x <= BOXSIZE * 6) {
currentcolor = ILI9341_MAGENTA;
tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
}
if (oldcolor != currentcolor) {
if (oldcolor == ILI9341_RED)
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
if (oldcolor == ILI9341_YELLOW)
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
if (oldcolor == ILI9341_GREEN)
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
if (oldcolor == ILI9341_BLACK)
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, ILI9341_BLACK);
if (oldcolor == ILI9341_BLUE)
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
if (oldcolor == ILI9341_MAGENTA)
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
}
}
if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < tft.height())) {
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
if (currentcolor == ILI9341_RED)
{
if (p.x != x_old && p.y != y_old )
{
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(") - RED");
countRed += 1;
x_old = p.x;
y_old = p.y;
}
}
else if (currentcolor == ILI9341_YELLOW )
{
if (p.x != x_old && p.y != y_old )
{
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(") - YELLOW");
countYellow += 1;
x_old = p.x;
y_old = p.y;
}
}
else if (currentcolor == ILI9341_GREEN )
{
if (p.x != x_old && p.y != y_old )
{
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(") - GREEN");
countGreen += 1;
x_old = p.x;
y_old = p.y;
}
}
else if (currentcolor == ILI9341_BLUE )
{
if (p.x != x_old && p.y != y_old )
{
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(") - BLUE");
countBlue += 1;
x_old = p.x;
y_old = p.y;
}
}
else if (currentcolor == ILI9341_MAGENTA )
{
if (p.x != x_old && p.y != y_old )
{
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(") - MAGENTA");
countMagenta += 1;
x_old = p.x;
y_old = p.y;
}
}
}
}