#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 8
#define TFT_RST 9
#include <SPI.h>
#define LTBLUE 0xB6DF
#define LTTEAL 0xBF5F
#define LTGREEN 0xBFF7
#define LTCYAN 0xC7FF
#define LTRED 0xFD34
#define LTMAGENTA 0xFD5F
#define LTYELLOW 0xFFF8
#define LTORANGE 0xFE73
#define LTPINK 0xFDDF
#define LTPURPLE 0xCCFF
#define LTGREY 0xE71C
#define BLUE 0x001F
#define TEAL 0x0438
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFC00
#define PINK 0xF81F
#define PURPLE 0x8010
#define GREY 0xC618
#define WHITE 0xFFFF
#define BLACK 0x0000
#define DKBLUE 0x000D
#define DKTEAL 0x020C
#define DKGREEN 0x03E0
#define DKCYAN 0x03EF
#define DKRED 0x6000
#define DKMAGENTA 0x8008
#define DKYELLOW 0x8400
#define DKORANGE 0x8200
#define DKPINK 0x9009
#define DKPURPLE 0x4010
#define DKGREY 0x4A49
#define ADJ_PIN A5
double a1, b1, c1, d1, r2, r1, vo, tempC, tempF, tempK;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
boolean display1 = true;
boolean display2 = true;
boolean display3 = true;
boolean display4 = true;
boolean display5 = true;
boolean display6 = true;
boolean display7 = true;
boolean display8 = true;
boolean display9 = true;
double ox, oy;
void setup() {
Serial.begin(9600);
pinMode(ADJ_PIN, INPUT);
tft.begin();
double x, y;
for (x = 0; x <= 6.3; x += 0.1) {
y = sin(x);
Graph(x, y, 60, 290, 390, 260, 0, 6.5, 1, -1, 1, 0.25, "Sin Function", "x", "sin(x)", DKBLUE, RED, YELLOW, WHITE, BLACK, display1);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0; x <= 6.3; x += 0.1) {
y = sin(x);
Graph(x, y, 100, 280, 100, 240, 0, 6.5, 3.25, -1, 1, 0.25, "Sin Function", "x", "sin(x)", GREY, GREEN, RED, YELLOW, BLACK, display9);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0; x <= 25.2; x += 0.1) {
y = sin(x);
Graph(x, y, 50, 190, 400, 60, 0, 25, 5, -1, 1, 0.5, "Sin Function", "x", "sin(x)", DKYELLOW, YELLOW, GREEN, WHITE, BLACK, display8);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0.001; x <= 10; x += 0.1) {
y = log(x);
Graph(x, y, 50, 240, 300, 180, 0, 10, 1, -10, 5, 1, "Natural Log Function", "x", "ln(x)", BLUE, RED, WHITE, WHITE, BLACK, display2);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0; x <= 10; x += 1) {
y = x * x;
Graph(x, y, 50, 290, 390, 260, 0, 10, 1, 0, 100, 10, "Square Function", "x", "x^2", DKRED, RED, YELLOW, WHITE, BLACK, display3);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0.00; x <= 20; x += 0.01) {
y = ((sin(x)) * x + cos(x)) - log(x);
Graph(x, y, 50, 290, 390, 260, 0, 20, 1, -20, 20, 5, "Weird Function", "x", " y = sin(x) + cos(x) - log(x)", ORANGE, YELLOW, CYAN, WHITE, BLACK, display4);
}
delay(1000);
tft.fillScreen(BLACK);
tft.setRotation(2);
for (x = 0; x <= 12.6; x += 0.1) {
y = sin(x);
Graph(x, y, 50, 250, 150, 150, 0, 13, 3.5, -1, 1, 1, "Sin(x)", "x", "sin(x)", DKBLUE, RED, YELLOW, WHITE, BLACK, display5);
}
tft.setRotation(3);
delay(1000);
tft.fillScreen(WHITE);
for (x = 0; x <= 6.3; x += 0.05) {
y = cos(x);
Graph(x, y, 100, 250, 300, 200, 0, 6.5, 3.25, -1, 1, 1, "Cos Function", "x", "cos(x)", DKGREY, GREEN, BLUE, BLACK, WHITE, display6);
}
delay(1000);
tft.fillScreen(BLACK);
for (x = 0; x <= 60; x += 1) {
vo = analogRead(ADJ_PIN) / 204.6;
double r1 = 9940;
double r2 = (vo * r1) / (5 - vo);
double tempK = 1.0 / (a1 + (b1 * (log(r2 / 10000.0))) + (c1 * pow(log(r2 / 10000.0), 2)) + (d1 * pow(log(r2 / 10000.0), 3)));
double tempC = ((tempK - 273.15) );
y = tempF = (tempC * 1.8000) + 32.00;
Graph(x, y, 50, 290, 390, 260, 0, 60, 10, 70, 90, 5, "Room Temperature", " Time [s]", "Temperature [deg F]", DKBLUE, RED, GREEN, WHITE, BLACK, display7);
delay(250);
}
delay(1000);
tft.fillScreen(BLACK);
}
void loop(void) {
// Empty loop
}
void Graph(double x, double y, double gx, double gy, double w, double h, double xlo, double xhi, double xinc, double ylo, double yhi, double yinc, String title, String xlabel, String ylabel, unsigned int gcolor, unsigned int acolor, unsigned int pcolor, unsigned int tcolor, unsigned int bcolor, boolean &redraw) {
double ydiv, xdiv;
// initialize old x and old y in order to draw the first point of the graph
// but save the transformed value
// note my transform funcition is the same as the map function, except the map uses long and we need doubles
//static double ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
//static double oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
double i;
double temp;
if (redraw == true) {
redraw = false;
ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
// draw y scale
for ( i = ylo; i <= yhi; i += yinc) {
// compute the transform
temp = (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
if (i == 0) {
tft.drawLine(gx, temp, gx + w, temp, acolor);
}
else {
tft.drawLine(gx, temp, gx + w, temp, gcolor);
}
tft.setTextSize(1);
tft.setTextColor(tcolor, bcolor);
tft.setCursor(gx - 40, temp);
// precision is default Arduino--this could really use some format control
tft.println(i);
}
// draw x scale
for (i = xlo; i <= xhi; i += xinc) {
// compute the transform
temp = (i - xlo) * ( w) / (xhi - xlo) + gx;
if (i == 0) {
tft.drawLine(temp, gy, temp, gy - h, acolor);
}
else {
tft.drawLine(temp, gy, temp, gy - h, gcolor);
}
tft.setTextSize(1);
tft.setTextColor(tcolor, bcolor);
tft.setCursor(temp, gy + 10);
// precision is default Arduino--this could really use some format control
tft.println(i);
}
//now draw the labels
tft.setTextSize(2);
tft.setTextColor(tcolor, bcolor);
tft.setCursor(gx , gy - h - 30);
tft.println(title);
tft.setTextSize(1);
tft.setTextColor(acolor, bcolor);
tft.setCursor(gx , gy + 20);
tft.println(xlabel);
tft.setTextSize(1);
tft.setTextColor(acolor, bcolor);
tft.setCursor(gx - 30, gy - h - 10);
tft.println(ylabel);
}
//graph drawn now plot the data
// the entire plotting code are these few lines...
// recall that ox and oy are initialized as static above
x = (x - xlo) * ( w) / (xhi - xlo) + gx;
y = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
tft.drawLine(ox, oy, x, y, pcolor);
tft.drawLine(ox, oy + 1, x, y + 1, pcolor);
tft.drawLine(ox, oy - 1, x, y - 1, pcolor);
ox = x;
oy = y;
}