/*
Binary Counting Touch Screen
https://wokwi.com/projects/376554003478915073
Based on:
* Binary Counter (https://wokwi.com/projects/300683470203519498)
* ft6206-paint (https://wokwi.com/projects/311598148845830720)
*/
#include "SPI.h"
#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_FT6206.h"
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BUTTONSIZE 40
enum mode {
forward,
backward,
};
mode oldMode, currentMode;
int i = 0;
#define BITBOUNDARY 128
void setup() {
// Start TFT Display
tft.begin();
// Start screen and console logging
Serial.begin(115200);
if (!ctp.begin(40)) { //pass 'sensitivity' coefficient
Serial.println("Couldn't enable touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen enabled");
// Initialize pins for the LEDs
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
// Buttons
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(4);
// Forward Button
tft.fillRect(0, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_RED);
tft.setCursor(10, 5);
tft.println("+");
currentMode = forward;
// Backward Button
tft.fillRect(BUTTONSIZE, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_WHITE);
tft.setCursor(10 + BUTTONSIZE * 1, 5);
tft.println("-");
}
void loop() {
// Binary number boundaries
if (i >= BITBOUNDARY){
i = 0;
}
else if (i <= 0){
i = BITBOUNDARY;
}
// Handle when the screen is touched
// Buttons will need to be firmly pressed to ensure the mode gets activated
if (ctp.touched()) {
// Get touch coordinates
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
Serial.print("(");
Serial.print(p.x);
Serial.print(", ");
Serial.print(p.y);
Serial.println(")");
// Handle button presses. If the mode is different, switch to that mode
if (p.y < BUTTONSIZE) {
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(4);
oldMode = currentMode;
if (p.x < BUTTONSIZE) {
currentMode = forward;
tft.fillRect(0, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_RED);
tft.setCursor(10, 5);
tft.println("+");
} else if (p.x < BUTTONSIZE*2) {
currentMode = backward;
tft.fillRect(BUTTONSIZE, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_RED);
tft.setCursor(10 + BUTTONSIZE * 1, 5);
tft.println("-");
}
if (oldMode != currentMode) {
if (oldMode == forward)
tft.fillRect(0, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_WHITE);
tft.setCursor(10, 5);
tft.println("+");
if (oldMode == backward)
tft.fillRect(BUTTONSIZE, 0, BUTTONSIZE, BUTTONSIZE, ILI9341_WHITE);
tft.setCursor(10 + BUTTONSIZE * 1, 5);
tft.println("-");
}
}
}
// Clear when the number of digits changes
// (floor(log10(i)) + 1 calculates the number of digits an integer has)
switch (currentMode){
case forward:
if (floor(log10(i)) + 1 != floor(log10(i + 1)) + 1 || i + 1 == BITBOUNDARY){
tft.fillRect(20, 150, 200, 40, ILI9341_BLACK);
}
i++;
break;
case backward:
if (floor(log10(i)) + 1 != floor(log10(i - 1)) + 1 || i - 1 == 0){
tft.fillRect(20, 150, 200, 40, ILI9341_BLACK);
}
i--;
break;
}
// Perform bit shifting for the Audrino IO ports
PORTD = (i << 2);
PORTB = (i >> 6);
// Display the current number
tft.setTextSize(4);
tft.setCursor(20, 150);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.println(i % BITBOUNDARY);
delay(100);
}