/***************************************************
  This is our GFX example for the Adafruit ILI9341 Breakout and Shield
  ----> http://www.adafruit.com/products/1651

  Check out the links above for our tutorials and wiring diagrams
  These displays use SPI to communicate, 4 or 5 pins are required to
  interface (RST is optional)
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************

  This is a free software with NO WARRANTY.
  https://simple-circuit.com/

 ****************************************************/

#include <Adafruit_GFX.h>     // include Adafruit graphics library
#include <Adafruit_ILI9341.h> // include Adafruit ILI9341 TFT library

#define TFT_CS 8  // TFT CS  pin is connected to arduino pin 8
#define TFT_RST 9 // TFT RST pin is connected to arduino pin 9
#define TFT_DC 10 // TFT DC  pin is connected to arduino pin 10
// initialize ILI9341 TFT library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

const int BUTTON_PIN_1 = 2;
const int BUTTON_PIN_2 = 3;
const int BUTTON_PIN_3 = 4;
const int BUTTON_PIN_4 = 5;
const int BUTTON_PIN_5 = 6;

volatile bool button_states[5] = {false, false, false, false, false};

int temp = 25;
int RPM = 0;
int desiredTemp = 25;
int desiredRPM = 100;
bool status = false;

long start_time = 0;
long current_time = micros();
int measurement_time = 0;
short visc = 0;
short tempFluctuation = temp;
int viscFluctuation = visc;
int RPMFluctuation = RPM;

void setup()
{
    Serial.begin(9600);
    Serial.println("ILI9341 Test!");

    // initialize digital pin LED_BUILTIN as an output.
    pinMode(BUTTON_PIN_1, INPUT_PULLUP);
    pinMode(BUTTON_PIN_2, INPUT_PULLUP);
    pinMode(BUTTON_PIN_3, INPUT_PULLUP);
    pinMode(BUTTON_PIN_4, INPUT_PULLUP);
    pinMode(BUTTON_PIN_5, INPUT_PULLUP);

    // enable pin change interrupts for buttons
    PCMSK2 |= (1 << PCINT18); // BUTTON_PIN_1
    PCMSK2 |= (1 << PCINT19); // BUTTON_PIN_2
    PCMSK2 |= (1 << PCINT20); // BUTTON_PIN_3
    PCMSK2 |= (1 << PCINT21); // BUTTON_PIN_4
    PCMSK2 |= (1 << PCINT22); // BUTTON_PIN_5

    // enable pin change interrupt 2
    PCICR |= (1 << PCIE2);

    tft.begin();

    // read diagnostics (optional but can help debug problems)
    uint8_t x = tft.readcommand8(ILI9341_RDMODE);
    Serial.print("Display Power Mode: 0x");
    Serial.println(x, HEX);
    x = tft.readcommand8(ILI9341_RDMADCTL);
    Serial.print("MADCTL Mode: 0x");
    Serial.println(x, HEX);
    x = tft.readcommand8(ILI9341_RDPIXFMT);
    Serial.print("Pixel Format: 0x");
    Serial.println(x, HEX);
    x = tft.readcommand8(ILI9341_RDIMGFMT);
    Serial.print("Image Format: 0x");
    Serial.println(x, HEX);
    x = tft.readcommand8(ILI9341_RDSELFDIAG);
    Serial.print("Self Diagnostic: 0x");
    Serial.println(x, HEX);

    Serial.println(F("Benchmark                Time (microseconds)"));

    Serial.print(F("Initialization           "));
    Serial.println(initialWindow());
    delay(2000);
    Serial.print(F("Main window              "));
    Serial.println(mainWindow(measurement_time, tempFluctuation, viscFluctuation, RPMFluctuation));

    Serial.println(F("Done!"));
}

void loop(void)
{
    // Print the current state of the button array
    if (button_states[0] || button_states[1] || button_states[2] || button_states[3] || button_states[4])
    {
        desiredTemp = desiredTemp - (button_states[0] ? 1 : 0) + (button_states[1] ? 1 : 0);
        desiredRPM = desiredRPM - 10 * (button_states[2] ? 1 : 0) + 10 * (button_states[3] ? 1 : 0);
        status = button_states[4] ? !status : status;
        Serial.println("Start: " + String(status) + ", Temperature: " + String(desiredTemp) + ", RPM: " + String(desiredRPM));
    }

    current_time = micros();
    if (status)
    {
        if (start_time == 0)
        {
            start_time = current_time;
            Serial.println(start_time);
            visc = random(0, 100000);
        }
        if (temp < desiredTemp)
        {
            temp++;
            viscFluctuation = 0;
        }
        else if (temp > desiredTemp)
        {
            temp--;
            viscFluctuation = 0;
        }
        else
        {
            temp = desiredTemp;
            tempFluctuation = random(temp - 1, temp + 1);
            viscFluctuation = random(visc - 1, visc + 1);
        }
        if (RPM < desiredRPM && temp == desiredTemp)
        {
            RPM=RPM+10;
            RPMFluctuation = random(RPM - 2, RPM + 2);
        }
        else if (RPM > desiredRPM && temp == desiredTemp)
        {
            RPM=RPM-10;
            RPMFluctuation = random(RPM - 2, RPM + 2);
        }
        else
        {
            RPMFluctuation = RPM;
        }
        measurement_time = (current_time - start_time) / 1000000;
    }
    else
    {
        start_time = 0;
        measurement_time = 0;
        tempFluctuation = desiredTemp;
        viscFluctuation = visc;
        RPMFluctuation = desiredRPM;
    }

    updatewindow(measurement_time, tempFluctuation, viscFluctuation, RPMFluctuation);
    delay(100);
}

unsigned long initialWindow()
{
    tft.setRotation(3);
    tft.fillScreen(ILI9341_BLACK);
    unsigned long start = micros();
    tft.setCursor(0, 0);
    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(4);
    tft.println();
    tft.println("    ME 407");
    tft.println("   Group S2");
    tft.println();
    tft.println("   Benchtop");
    tft.println("  Viscometer");
    return micros() - start;
}

unsigned long mainWindow(uint8_t measurement_time, uint8_t temp, uint8_t visc, uint8_t RPMFluctuation)
{
    int xpos = 0;
    int dx = 250;
    int ypos = 0;
    int dy = 40;
    tft.setRotation(3);
    tft.fillScreen(ILI9341_BLACK);
    unsigned long start = micros();
    int font_size = 2;
    printText("Viscometer", ILI9341_GREEN, xpos, ypos, 4);
    tft.println();
    printText("Time(sec): ", ILI9341_WHITE, xpos, ypos + dy, font_size);
    printTextNumber(measurement_time, ILI9341_WHITE, xpos + dx, ypos + dy, font_size);
    printText("Temperature(C): ", ILI9341_WHITE, xpos, ypos + 2 * dy, font_size);
    printTextNumber(temp, ILI9341_WHITE, xpos + dx, ypos + 2 * dy, font_size);
    printText("Motor Speed(RPM): ", ILI9341_WHITE, xpos, ypos + 3 * dy, font_size);
    printTextNumber(RPMFluctuation, ILI9341_WHITE, xpos + dx, ypos + 3 * dy, font_size);
    printText("Viscosity(cP): ", ILI9341_WHITE, xpos, ypos + 4 * dy, font_size);
    printTextNumber(visc, ILI9341_WHITE, xpos + dx, ypos + 4 * dy, font_size);
    return micros() - start;
}

unsigned long updatewindow(uint8_t measurement_time, uint8_t temp, uint8_t visc, uint8_t RPMFluctuation)
{
    int width = 80;
    int height = 160;
    int xBox = 250;
    int yBox = 40;
    int xpos = 0;
    int dx = 250;
    int ypos = 0;
    int dy = 40;
    tft.setRotation(3);
    unsigned long start = micros();
    tft.fillRect(xBox, yBox, width, height, ILI9341_BLACK);
    printTextNumber(measurement_time, ILI9341_WHITE, xpos + dx, ypos + dy, 3);
    printTextNumber(temp, ILI9341_WHITE, xpos + dx, ypos + 2 * dy, 3);
    printTextNumber(RPMFluctuation, ILI9341_WHITE, xpos + dx, ypos + 3 * dy, 3);
    printTextNumber(visc, ILI9341_WHITE, xpos + dx, ypos + 4 * dy, 3);
    return micros() - start;
}

void printText(char *text, uint16_t color, int x, int y, int textSize)
{
    tft.setCursor(x, y);
    tft.setTextColor(color);
    tft.setTextSize(textSize);
    tft.setTextWrap(true);
    tft.print(text);
}

void printTextNumber(uint8_t text, uint16_t color, int x, int y, int textSize)
{
    tft.setCursor(x, y);
    tft.setTextColor(color);
    tft.setTextSize(textSize);
    tft.setTextWrap(true);
    tft.print(text);
}

// Interrupt service routine for pin-change interrupts on PCINT18-PCINT21 (pins 2-5)
ISR(PCINT2_vect)
{
    bool state_1 = digitalRead(BUTTON_PIN_1);
    bool state_2 = digitalRead(BUTTON_PIN_2);
    bool state_3 = digitalRead(BUTTON_PIN_3);
    bool state_4 = digitalRead(BUTTON_PIN_4);
    bool state_5 = digitalRead(BUTTON_PIN_5);

    button_states[0] = state_1 ? false : true;
    button_states[1] = state_2 ? false : true;
    button_states[2] = state_3 ? false : true;
    button_states[3] = state_4 ? false : true;
    button_states[4] = state_5 ? false : true;
}
Loading
ili9341-cap-touch