// https://forum.arduino.cc/t/need-help-deciphering-my-error-message/1410064/
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_ILI9341.h> // https://github.com/adafruit/Adafruit_ILI9341
// https://github.com/arduino/ArduinoCore-avr/blob/master/variants/standard/pins_arduino.h
#define TFT_RST 8 // RESET
#define TFT_DC 9 // DATA/COMMAND
#define TFT_CS 10 // SYSTEM SELECT
#define TFT_MOSI 11 // DATA OUT (from controller to device)
#define TFT_MISO 12 // DATA IN (from device to controller)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); // create ILI9341 instance/object
int sensorValueX, sensorValueY, pixelX, pixelY, pixelXold, pixelYold; // sensor and plotting variables
unsigned long timer, dottimer, timeout = 100; // timer for reading sensors
bool blink; // for blinkdots
void setup() {
tft.begin();
tft.setRotation(1);
tft.setTextSize(1);
tft.setCursor(2, ILI9341_TFTWIDTH / 2 - 9);
tft.setTextColor(ILI9341_GREEN);
tft.print("Hello, ");
tft.print(ILI9341_TFTHEIGHT);
tft.print("x");
tft.print(ILI9341_TFTWIDTH);
tft.print(" TFT!");
tft.drawLine(2, ILI9341_TFTWIDTH / 2, ILI9341_TFTHEIGHT - 3, ILI9341_TFTWIDTH / 2, ILI9341_GREEN); // x axis
tft.drawRect(0, 0, ILI9341_TFTHEIGHT, ILI9341_TFTWIDTH, ILI9341_YELLOW); // border
tft.drawLine(2, 2, 22, 22, ILI9341_RED); // arrow shaft
tft.drawLine(2, 2, 7, 4, ILI9341_RED); // arrow blade
tft.drawLine(2, 2, 4, 7, ILI9341_RED); // arrow blade
tft.setCursor(23, 23);
tft.setTextColor(ILI9341_CYAN);
tft.print("(0,0)");
tft.drawLine(ILI9341_TFTHEIGHT - 3, ILI9341_TFTWIDTH - 3, ILI9341_TFTHEIGHT - 23, ILI9341_TFTWIDTH - 23, ILI9341_RED); // arrow shaft
tft.drawLine(ILI9341_TFTHEIGHT - 3, ILI9341_TFTWIDTH - 3, ILI9341_TFTHEIGHT - 10, ILI9341_TFTWIDTH - 7, ILI9341_RED); // arrow blade
tft.drawLine(ILI9341_TFTHEIGHT - 3, ILI9341_TFTWIDTH - 3, ILI9341_TFTHEIGHT - 7, ILI9341_TFTWIDTH - 10, ILI9341_RED); // arrow blade
tft.setCursor(ILI9341_TFTHEIGHT - 10 * 6, ILI9341_TFTWIDTH - (24 + 9)); // 6x8 dot/char
tft.setTextColor(ILI9341_CYAN);
tft.print("(");
tft.print(ILI9341_TFTHEIGHT - 1);
tft.print(",");
tft.print(ILI9341_TFTWIDTH - 1);
tft.print(")");
}
void loop() {
if (millis() - dottimer > timeout * 5) {
dottimer = millis();
blinkdots(); // blink the corner pixels
}
if (millis() - timer > timeout) {
timer = millis();
readSensors(); // read potentiometers
}
}
void readSensors() {
sensorValueX = analogRead(PIN_A1); // see pins_arduino.h
sensorValueY = analogRead(PIN_A0);
pixelX = map(sensorValueX, 0, 1023, 2, ILI9341_TFTHEIGHT - 4); // https://docs.arduino.cc/language-reference/en/functions/math/map/
pixelY = map(sensorValueY, 0, 1023, 2, ILI9341_TFTWIDTH - 4);
tft.drawPixel(pixelX, pixelY, ILI9341_YELLOW); // https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
tft.drawPixel(pixelX + 1, pixelY, ILI9341_BLUE);
tft.drawPixel(pixelX + 1, pixelY + 1, ILI9341_GREEN);
tft.drawPixel(pixelX, pixelY + 1, ILI9341_RED);
}
void blinkdots() {
blink = !blink;
if (blink) {
tft.drawPixel(0, 0, ILI9341_RED); // origin dot
tft.drawPixel(ILI9341_TFTHEIGHT - 1, ILI9341_TFTWIDTH - 1, ILI9341_RED); // terminal dot
} else {
tft.drawPixel(0, 0, ILI9341_YELLOW); // origin dot
tft.drawPixel(ILI9341_TFTHEIGHT - 1, ILI9341_TFTWIDTH - 1, ILI9341_YELLOW); // terminal dot
}
}