//////////////////////////////////////////////////////////////////////////////
///// Name : Sittinon
///// Proposition : Touch screen ILI9341
//////////////////////////////////////////////////////////////////////////////
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Arduino.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <U8g2_for_Adafruit_GFX.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
//------------------------------
// Set the legs of the TFT screen.
#define TFT_DC 15
#define TFT_CS 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BOXSIZE 40
//-------------------------------------------------
// Time
unsigned long previousMillis = 0;
unsigned long previousMillis_Thingspeak = 0;
const long interval = 1000;
const long interval_Thingspeak = 20000;
//-------------------------------------------------
int led = 17;
int Count = 0;
//--------------------------------
// LGB bar
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
1, 2, 42, 41, 40, 35, 0, 45, 48, 47
}; // an array of pin numbers to which LEDs are attached
//--------------------------------
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
pinMode(led, OUTPUT);
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
//Wire.setPins(10, 8);
tft.begin();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello, TFT!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("I can has colors?");
//delay(2000);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(13, 18);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("ON!");
tft.fillRect(BOXSIZE+3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(53, 18);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("OFF!");
}
void loop() {
Touch();
for (int i = 0; i <= 9; i++){
digitalWrite(ledPins[i], LOW);
}
}
void Touch(){
delay(10);
// Wait for a touch
if (! ctp.touched()) {
return;
}
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(")");
if (p.y < BOXSIZE){
if (p.x < 40){
digitalWrite(led, HIGH);
}
else if (p.x < 40*2){
digitalWrite(led, LOW);
}
else if (p.x < 40*3 || p.x < 40*4){
Count++;
if(Count > 1)
Count = 0;
}
}
}