/*Seeeduino XIAO -> ILI9341 Vin -> Vcc GND -> GND GPIO 3 -> CS GPIO 1 -> RESET GPIO 2 -> D/C GPIO 10(MOSI) -> SDI(MOSI) GPIO 8(SCK) -> SCK GPIO 0 -> LED */ #include #include #include #include //#include //#define SPICOM SPI #define Adafruit_ILI9341_DRIVER #define TFT_MOSI D10 #define TFT_SCLK D8 #define TFT_MISO D9 #define TFT_CS D7 #define TFT_DC D6 #define TFT_RST D4 \ #define NEOPIXEL_PIN 5 #define NUM_PIXELS 30 #define BRIGHTNESS 255 // Set BRIGHTNESS to about 1/5 (max = 255) Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRBW + NEO_KHZ800); #define TS_MINX 150 #define TS_MINY 120 #define TS_MAXX 920 #define TS_MAXY 940 #define MINPRESSURE 10 #define MAXPRESSURE 1000 //Touchscreen X+ X- Y+ Y- pins #define YP A2 // must be an analog pin, use "An" notation! #define XM A0 // must be an analog pin, use "An" notation! #define YM D1 // can be a digital pin #define XP D3 // can be a digital pin TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); enum ButtonState { RED, GREEN, YELLOW }; ButtonState currentButtonState = RED; void setup() { pinMode(TFT_DC, OUTPUT); pinMode(TFT_CS, OUTPUT); pinMode(TFT_SCLK, OUTPUT); pinMode(TFT_MOSI, OUTPUT); Serial.begin(9600); tft.begin(); strip.begin(); strip.show(); // Initialize all pixels to 'off' strip.setBrightness(BRIGHTNESS); // Clear the screen tft.fillScreen(ILI9341_BLUE); tft.setRotation(1); tft.setCursor(80, 12); tft.setTextColor(ILI9341_ORANGE); tft.setTextSize(3); tft.println("Serenity"); tft.setCursor(110, 50); tft.setTextColor(ILI9341_ORANGE); tft.setTextSize(4); tft.println("NOW!"); tft.setCursor(60, 100); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.println("Fab Academy 2023"); // Draw buttons on the screen drawButtons(); } void loop() { TSPoint touch = ts.getPoint(); //tft.setRotation(2); if (touch.z >= MINPRESSURE && touch.z <= MAXPRESSURE) { // Touch event detected int16_t x = map(touch.x, TS_MINX, TS_MAXX, 0, tft.width()); int16_t y = map(touch.y, TS_MINY, TS_MAXY, 0, tft.height()); // Check if the touch is within the button areas if (x > 40 && x < 120 && y > 40 && y < 120) { // Button 1 is pressed setButtonState(RED); } else if (x > 40 && x < 120 && y > 40 && y < 240){ // Button 2 is pressed setButtonState(GREEN); } else if (x > 280 && x < 360 && y > 40 && y < 120){ // Button 3 is pressed setButtonState(YELLOW); } // Add more button checks as needed } { // a point object holds x y and z coordinates TSPoint p = ts.getPoint(); // we have some minimum pressure we consider 'valid' // pressure of 0 means no pressing! if (p.z > ts.pressureThreshhold) { Serial.print("X = "); Serial.print(p.x); Serial.print("\tY = "); Serial.print(p.y); Serial.print("\tPressure = "); Serial.println(p.z); } delay(100); } } void setButtonState(ButtonState newState) { if (newState != currentButtonState) { // Change the Neopixel color based on the button state switch (newState) { case RED: setColor(0, 255, 0); // Red break; case GREEN: setColor(255, 0, 0); // Green break; case YELLOW: setColor(255, 255, 0); // Yellow break; } currentButtonState = newState; } } void setColor(uint8_t red, uint8_t green, uint8_t blue) { for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, strip.Color(red, green, blue)); } strip.show(); } void drawButtons() { // Draw button 1 tft.fillRect(0, 145, 60, 100, ILI9341_YELLOW); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.setCursor(55, 70); //tft.print("RED"); // Draw button 2 tft.fillRect(80, 145, 60, 100, ILI9341_GREEN); tft.setCursor(175, 70); //tft.print("GREEN"); // Draw button 3 tft.fillRect(160, 145, 60, 100, ILI9341_RED); tft.setCursor(295, 70); //tft.print("YELLOW"); }