#include <CAN.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_TFTLCD.h>
#include <SPI.h>
#include <Wire.h>
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0xD6BA
#define TFT_CS 10
#define TFT_RST 8
#define TFT_DC 9
double VoutFL1 = 0;
double CurrentFL1 = 0;
double VoutFL2 = 0;
double CurrentFL2 = 0;
double VoutFL3 = 0;
double CurrentFL3 = 0;
double VoutFL4 = 0;
double CurrentFL4 = 0;
double VoutFL5 = 0;
double CurrentFL5 = 0;
double VoutFL6 = 0;
double CurrentFL6 = 0;
double VoutFL7 = 0;
double CurrentFL7 = 0;
double VoutFL8 = 0;
double CurrentFL8 = 0;
double VoutRL1 = 0;
double CurrentRL1 = 0;
double VoutRL2 = 0;
double CurrentRL2 = 0;
double VoutRL3 = 0;
double CurrentRL3 = 0;
double VoutRL4 = 0;
double CurrentRL4 = 0;
double VoutRL5 = 0;
double CurrentRL5 = 0;
double VoutRL6 = 0;
double CurrentRL6 = 0;
double VoutRL7 = 0;
double CurrentRL7 = 0;
double VoutRL8 = 0;
double CurrentRL8 = 0;
//const double scale_factor = 0.185; // 5A
const double scale_factor = 0.1; // 20A
//const double scale_factor = 0.066; // 30A
const double vRef = 5.00;
const double resConvert = 1024;
double resADC = vRef/resConvert;
double zeroPoint = vRef/2;
const int ButtonPin = 3;
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = LOW; // current state of the button
boolean lastButtonState = LOW; // previous state of the button
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int analogValues[32] = {0}; // Array to store analog values for FL1-FL8, RL1-RL8, ES1-ES8, and BS1-BS8
int FL1 = analogValues[0] * 4.0117;
int FL2 = analogValues[1] * 4.0117;
int FL3 = analogValues[2] * 4.0117;
int FL4 = analogValues[3] * 4.0117;
int FL5 = analogValues[4] * 4.0117;
int FL6 = analogValues[5] * 4.0117;
int FL7 = analogValues[6] * 4.0117;
int FL8 = analogValues[7] * 4.0117;
int RL1 = analogValues[8] * 4.0117;
int RL2 = analogValues[9] * 4.0117;
int RL3 = analogValues[10] * 4.0117;
int RL4 = analogValues[11] * 4.0117;
int RL5 = analogValues[12] * 4.0117;
int RL6 = analogValues[13] * 4.0117;
int RL7 = analogValues[14] * 4.0117;
int RL8 = analogValues[15] * 4.0117;
int ES1 = analogValues[16] * 4.0117;
int ES2 = analogValues[17] * 4.0117;
int ES3 = analogValues[18] * 4.0117;
int ES4 = analogValues[19] * 4.0117;
int ES5 = analogValues[20] * 4.0117;
int ES6 = analogValues[21] * 4.0117;
int ES7 = analogValues[22] * 4.0117;
int ES8 = analogValues[23] * 4.0117;
int BS1 = analogValues[24] * 4.0117;
int BS2 = analogValues[25] * 4.0117;
int BS3 = analogValues[26] * 4.0117;
int BS4 = analogValues[27] * 4.0117;
int BS5 = analogValues[28] * 4.0117;
int BS6 = analogValues[29] * 4.0117;
int BS7 = analogValues[30] * 4.0117;
int BS8 = analogValues[31] * 4.0117;
void setup() {
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(ButtonPin, INPUT);
// Initialize ILI9341
tft.begin();
tft.setRotation(0); // Rotate display if needed (0, 1, 2, 3)
// Set text color and size
// Clear screen and display text
//tft.fillScreen(BLACK);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.setCursor(32, 107); // Set text position
tft.println("ART OF MK2");
tft.drawRect(0,0,240,320,CYAN);
//tft.drawLine(120, 0, 120, 320, GREEN);
tft.drawRect(0,0,240,320,CYAN);
delay (1500);
while (!Serial);
Serial.println("CAN Receiver");
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(5, 5); // Set text position
tft.println("Starting CAN failed");
tft.drawRect(0,0,240,320,CYAN);
while (1);
}
CAN.onReceive(onReceive);
}
void loop() {
{
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL1)));
delay(1);
}
// Get Vout in mv
VoutFL1 = VoutFL1 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL1 = (VoutFL1 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL1,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL1,2);
Serial.println(" Amps");
if (VoutFL1 >10 && CurrentFL1 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL2)));
delay(1);
}
// Get Vout in mv
VoutFL2 = VoutFL2 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL2 = (VoutFL2 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL2,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL2,2);
Serial.println(" Amps");
if (VoutFL2 >10 && CurrentFL2 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Get Vout in mv
VoutFL3 = VoutFL3 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL3 = (VoutFL3 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL3,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL3,2);
Serial.println(" Amps");
if (VoutFL3 >10 && CurrentFL3 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL4)));
delay(1);
}
// Get Vout in mv
VoutFL4 = VoutFL4 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL4 = (VoutFL4 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL4,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL4,2);
Serial.println(" Amps");
if (VoutFL4 >10 && CurrentFL4 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL5)));
delay(1);
}
// Get Vout in mv
VoutFL5 = VoutFL5 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL5 = (VoutFL5 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL5,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL5,2);
Serial.println(" Amps");
if (VoutFL5 >10 && CurrentFL5 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL6)));
delay(1);
}
// Get Vout in mv
VoutFL6 = VoutFL6 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL6 = (VoutFL6 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL6,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL6,2);
Serial.println(" Amps");
if (VoutFL6 >10 && CurrentFL6 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL7)));
delay(1);
}
// Get Vout in mv
VoutFL7 = VoutFL7 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL7 = (VoutFL7 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL7,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL7,2);
Serial.println(" Amps");
if (VoutFL7 >10 && CurrentFL7 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutFL1 = (VoutFL1 + (resADC * analogRead(FL8)));
delay(1);
}
// Get Vout in mv
VoutFL8 = VoutFL8 /1000;
// Convert Vout into Current using Scale Factor
CurrentFL8 = (VoutFL8 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutFL8,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentFL8,2);
Serial.println(" Amps");
if (VoutFL8 >10 && CurrentFL8 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL1 = (VoutRL1 + (resADC * analogRead(RL1)));
delay(1);
}
// Get Vout in mv
VoutRL1 = VoutRL1 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL1 = (VoutRL1 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL1,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL1,2);
Serial.println(" Amps");
if (VoutRL1 >10 && CurrentRL1 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL1 = (VoutRL1 + (resADC * analogRead(RL2)));
delay(1);
}
// Get Vout in mv
VoutRL2 = VoutRL2 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL2 = (VoutRL2 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL2,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL2,2);
Serial.println(" Amps");
if (VoutRL2 >10 && CurrentRL2 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL3 = (VoutRL3 + (resADC * analogRead(RL3)));
delay(1);
}
// Get Vout in mv
VoutRL3 = VoutRL3 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL3 = (VoutRL3 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL3,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL3,2);
Serial.println(" Amps");
if (VoutRL3 >10 && CurrentRL3 <2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL4 = (VoutRL4 + (resADC * analogRead(RL4)));
delay(1);
}
// Get Vout in mv
VoutRL4 = VoutRL4 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL4 = (VoutRL4 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL4,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL4,2);
Serial.println(" Amps");
if (VoutRL4 >10 && CurrentRL4 < 2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL5 = (VoutRL5 + (resADC * analogRead(RL5)));
delay(1);
}
// Get Vout in mv
VoutRL5 = VoutRL5 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL5 = (VoutRL5 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL5,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL5,2);
Serial.println(" Amps");
if (VoutRL5 > 10 && CurrentRL5 < 2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL6 = (VoutRL6 + (resADC * analogRead(RL6)));
delay(1);
}
// Get Vout in mv
VoutRL6 = VoutRL6 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL6 = (VoutRL6 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL6,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL6,2);
Serial.println(" Amps");
if (VoutRL6 > 10 && CurrentRL6 < 2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL7 = (VoutRL7 + (resADC * analogRead(RL7)));
delay(1);
}
// Get Vout in mv
VoutRL7 = VoutRL7 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL7 = (VoutRL7 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL7,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL7,2);
Serial.println(" Amps");
if (VoutRL7 > 10 && CurrentRL7 < 2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
VoutRL8 = (VoutRL8 + (resADC * analogRead(RL8)));
delay(1);
}
// Get Vout in mv
VoutRL8 = VoutRL8 /1000;
// Convert Vout into Current using Scale Factor
CurrentRL8 = (VoutRL8 - zeroPoint)/ scale_factor;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(VoutRL8,2);
Serial.print(" Volts");
Serial.print("\t Current = ");
Serial.print(CurrentRL8,2);
Serial.println(" Amps");
if (VoutRL8 > 10 && CurrentRL8 < 2) {
//if current =>2 but volt =<10 display
tft.setTextColor(WHITE);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(113, 17);
tft.println("!");
tft.fillTriangle(120, 10, 100, 42, 140, 42, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(113, 17); // Set text position
tft.println("!");
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(80, 48); // Set text position
tft.println("FAULT");
}
//tft.drawLine(120, 0, 120, 320, GREEN);
tft.drawRect(0,0,240,320,CYAN);
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(5, 5); // Set text position
tft.println("COOLANT");
tft.setTextColor(RED);
tft.setTextSize(3);
// Clear screen and display text
tft.setCursor(5, 23); // Set text position
tft.println("XXX'C");
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(200, 5); // Set text position
tft.println("OIL");
// Set text color and size
tft.setTextColor(BLUE);
tft.setTextSize(3);
// Clear screen and display tex
tft.setCursor(150, 23); // Set text position
tft.println("XXX'C");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(5);
// Clear screen and display text
tft.setCursor(78, 60); // Set text position
tft.println("XXX");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(98, 100); // Set text position
tft.println("KM/H");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(5, 300); // Set text position
tft.println("XX.X'C");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(140, 300); // Set text position
tft.println("XX.X BAR");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(140, 280); // Set text position
tft.println("XX.X AFR");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display text
tft.setCursor(5, 280); // Set text position
tft.println("XX.X V");
// Set text color and size
tft.setTextColor(RED);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(85, 260); // Set text position
tft.println("O");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(100, 260); // Set text position
tft.println("O");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(115, 260); // Set text position
tft.println("O");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(130, 260); // Set text position
tft.println("O");
// Set text color and size
tft.setTextColor(GREEN);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(145, 260); // Set text position
tft.println("O");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(115, 242); // Set text position
tft.println("O");
}
// read the state of the pushbutton value:
buttonState = digitalRead(ButtonPin);
Serial.println(buttonPushCounter);
switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
{
case 0:
tft.drawCircle(120, 178, 60, YELLOW);
break;
case 1:
tft.drawRect(80, 120, 80, 120, WHITE);
tft.drawLine(130, 120, 158, 170, WHITE);
tft.drawLine(110, 120, 81, 170, WHITE);
tft.drawLine(158, 170, 81, 170, WHITE);
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 120); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 137); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 154); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 120); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 137); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 154); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 191); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 208); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(44, 225); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 191); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 208); // Set text position
tft.println("000");
// Set text color and size
tft.setTextColor(WHITE);
tft.setTextSize(2);
// Clear screen and display tex
tft.setCursor(162, 225); // Set text position
tft.println("000");
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++; // add one to counter
tft.fillScreen(BLACK);
if (buttonPushCounter > 5)
{
buttonPushCounter = 0;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
void onReceive(int packetSize) {
if (packetSize == 3) {
int sensorType = CAN.read(); // Read the sensor type
int sensorIndex = CAN.read(); // Read the sensor index
int sensorValue = CAN.read(); // Read the sensor value
if (sensorType == 0 && sensorIndex >= 0 && sensorIndex < 8) {
int pinIndex = sensorIndex; // Calculate the corresponding pin index for FL1-FL8
analogValues[pinIndex] = sensorValue; // Update the analog value
Serial.print("Received analog value for FL");
Serial.print(sensorIndex + 1);
Serial.print(": ");
Serial.println(sensorValue);
}
else if (sensorType == 1 && sensorIndex >= 0 && sensorIndex < 8) {
int pinIndex = 8 + sensorIndex; // Calculate the corresponding pin index for RL1-RL8
analogValues[pinIndex] = sensorValue; // Update the analog value
Serial.print("Received analog value for RL");
Serial.print(sensorIndex + 1);
Serial.print(": ");
Serial.println(sensorValue);
}
else if (sensorType == 2 && sensorIndex >= 0 && sensorIndex < 8) {
int pinIndex = 16 + sensorIndex; // Calculate the corresponding pin index for ES1-ES8
analogValues[pinIndex] = sensorValue; // Update the analog value
Serial.print("Received analog value for ES");
Serial.print(sensorIndex + 1);
Serial.print(": ");
Serial.println(sensorValue);
}
else if (sensorType == 3 && sensorIndex >= 0 && sensorIndex < 8) {
int pinIndex = 24 + sensorIndex; // Calculate the corresponding pin index for BS1-BS8
analogValues[pinIndex] = sensorValue; // Update the analog value
Serial.print("Received analog value for BS");
Serial.print(sensorIndex + 1);
Serial.print(": ");
Serial.println(sensorValue);
}
}
}