//////////////////////////////////////////////////////////////////////////////
///// 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 60
//-------------------------------------------------
// 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();
  tft.setRotation(1);
  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_CYAN);
  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.setCursor(BOXSIZE/2 - 17, BOXSIZE/2 - 6);
//  tft.setFont(u8g2_font_helvR14_tf);  
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.println("ON!");
  tft.fillRect(BOXSIZE+3, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  tft.setCursor(BOXSIZE*1.5 - 17,BOXSIZE/2 - 6);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  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(")");
  // Se presionó botón verde
  if (p.x >0 && p.x <= BOXSIZE && p.y > 0 && p.y < BOXSIZE){
    digitalWrite(led, HIGH);
    Count++;
    if(Count > 1){
    Count = 0;}
  }
  // Se presionó botón rojo
  if (p.x >BOXSIZE + 3 && p.x <=2*  BOXSIZE  + 3 && p.y > 0 && p.y < BOXSIZE){
    digitalWrite(led, LOW);
    Count++;
    if(Count > 1){
    Count = 0;}
  }  
}Loading
ili9341-cap-touch
ili9341-cap-touch