You are on page 1of 9

//A5 för bater, D4 för hall sensor

//A10 för blinkers

#include <Adafruit_GPS.h>

#include <esp_now.h>
#include <WiFi.h>

#include <SPI.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_HX8357.h>
#include <Adafruit_STMPE610.h>
#include <Fonts/DSEG7_Classic_Bold_60.h>

#include <Fonts/Nimbus_Mono_L_Bold_40.h>

//Här kollar så matchar man skärmen med rätt

#ifdef ESP8266
#define STMPE_CS 16
#define TFT_CS 0
#define TFT_DC 15
#define SD_CS 2
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
#define STMPE_CS 32
#define TFT_CS 15
#define TFT_DC 33
#define SD_CS 14
#elif defined(TEENSYDUINO)
#define TFT_DC 10
#define TFT_CS 4
#define STMPE_CS 3
#define SD_CS 8
#elif defined(ARDUINO_STM32_FEATHER)
#define TFT_DC PB4
#define TFT_CS PA15
#define STMPE_CS PC7
#define SD_CS PC5
#elif defined(ARDUINO_NRF52832_FEATHER)
#define TFT_DC 11
#define TFT_CS 31
#define STMPE_CS 30
#define SD_CS 27
#elif defined(ARDUINO_MAX32620FTHR) || defined(ARDUINO_MAX32630FTHR)
#define TFT_DC P5_4
#define TFT_CS P5_3
#define STMPE_CS P3_3
#define SD_CS P3_2
#else
// Anything else, defaults!
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
#endif

#define TFT_RST -1
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);

// TFT skärmens bredd och höjd


#define TS_MINX 3800
#define TS_MAXX 100
#define TS_MINY 100
#define TS_MAXY 3750
#define GPSSerial Serial1

Adafruit_GPS GPS(&GPSSerial);

const int analogInPin = A5; // Analog input pin that the potentiometer is attached
to

int sensorValue = 0;

uint32_t timer = millis();

int sensPin=4;

int revolutions=0;
int rpm=0; // max value 32,767 16 bit
long startTime=0;
long elapsedTime;

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //Jag hadde
bara en ESP32 hemma så jag kunde inte få tag på den andras mac-adress. Fylleri så
fort jag är i skolan

int sensorValueBli = 0; // value read from the pot

const int analogInPinBli = A10; // Analog input pin that the potentiometer is
attached to

// Structure example to send data


// Must match the receiver structure
typedef struct struct_message {
char a[32];
int b;
} struct_message;

// Create a struct_message called myData


struct_message myData;

esp_now_peer_info_t peerInfo;

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {


Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery
Fail");
}
void interruptFunction() //interrupt service routine
{
revolutions++;
}

void setup()
{
pinMode(sensPin, INPUT);
sensorValueBli = analogRead(analogInPinBli);

Serial.begin(250000);
Serial.println("HX8357D Featherwing touch test!");
if (!ts.begin()) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
Serial.println("Touchscreen started");

WiFi.mode(WIFI_STA);

// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}

// Once ESPNow is successfully Init, we will register for Send CB to


// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);

// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;

// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}

tft.begin();
tft.fillScreen(HX8357_BLACK);
tft.fillRect(0, 0, 400, 50, HX8357_RED);
tft.setCursor(50, 50);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(4);
tft.println("STARTING...");

delay(1000);
}

void loop() {

ts.begin();

sensorValueBli = analogRead(analogInPinBli);

strcpy(myData.a, "THIS IS A CHAR");


myData.b = sensorValueBli ;

// Send message via ESP-NOW


esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData,
sizeof(myData));

if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(2000);

revolutions=0; rpm=0;
startTime=millis();
attachInterrupt(digitalPinToInterrupt(4),interruptFunction,RISING);
delay(1000);
detachInterrupt(2);

//now let's see how many counts we've had from the hall effect sensor and calc the
RPM
elapsedTime=millis()-startTime; //finds the time, should be very close to 1 sec

if(revolutions>0)
{
rpm=(max(1, revolutions) * (80000 * 0.15)/2 ) / elapsedTime; //(80000 *
radien )/antal magneter
}

int outMsg = int("RPM :") + rpm;

TS_Point p = ts.getPoint();

p.y = map(p.y, 0, 1000, 100, tft.height());

sensorValue = analogRead(analogInPin);

Serial.print("\tY = ");
Serial.println(p.y);

if ( 172 < p.y && p.y < 320 ) {


sensorValue = analogRead(analogInPin);
tft.fillScreen(HX8357_BLACK);

tft.drawLine(0, 479, 1000, 479 , 0x0000FF);


tft.drawLine(0, 478, 1000, 478 , 0x0000FF);
tft.drawLine(0, 477, 1000, 477 , 0x0000FE);
tft.drawLine(0, 476, 1000, 476 , 0x0000FE);
tft.drawLine(0, 475, 1000, 475 , 0x0000FD);
tft.drawLine(0, 474, 1000, 474 , 0x0000FD);
tft.drawLine(0, 473, 1000, 473 , 0x0000FC);
tft.drawLine(0, 472, 1000, 472 , 0x0000FC);
tft.drawLine(0, 471, 1000, 471 , 0x0000FB);
tft.drawLine(0, 470, 1000, 470 , 0x0000FB);
tft.drawLine(0, 469, 1000, 469 , 0x0000FA);
tft.drawLine(0, 468, 1000, 468 , 0x0000FA);
tft.drawLine(0, 467, 1000, 467 , 0x0000F9);
tft.drawLine(0, 466, 1000, 466 , 0x0000F9);
tft.drawLine(0, 465, 1000, 465 , 0x0000F8);
tft.drawLine(0, 464, 1000, 464 , 0x0000F8);
tft.drawLine(0, 463, 1000, 463 , 0x0000F7);
tft.drawLine(0, 462, 1000, 462 , 0x0000F7);
tft.drawLine(0, 461, 1000, 461 , 0x0000F6);
tft.drawLine(0, 460, 1000, 460 , 0x0000F6);
tft.drawLine(0, 459, 1000, 459 , 0x0000F5);
tft.drawLine(0, 458, 1000, 458 , 0x0000F5);
tft.drawLine(0, 457, 1000, 457 , 0x0000F4);
tft.drawLine(0, 456, 1000, 456 , 0x0000F4);
tft.drawLine(0, 455, 1000, 455 , 0x0000F3);
tft.drawLine(0, 454, 1000, 454 , 0x0000F3);
tft.drawLine(0, 453, 1000, 453 , 0x0000F2);
tft.drawLine(0, 452, 1000, 452 , 0x0000F2);
tft.drawLine(0, 451, 1000, 451 , 0x0000F1);
tft.drawLine(0, 450, 1000, 450 , 0x0000F1);
tft.drawLine(0, 449, 1000, 449 , 0x0000EF);
tft.drawLine(0, 448, 1000, 448 , 0x0000EF);
tft.drawLine(0, 447, 1000, 447 , 0x0000EE);
tft.drawLine(0, 446, 1000, 446 , 0x0000EE);
tft.drawLine(0, 445, 1000, 445 , 0x0000ED);
tft.drawLine(0, 444, 1000, 444 , 0x0000ED);
tft.drawLine(0, 443, 1000, 443 , 0x0000EC);
tft.drawLine(0, 442, 1000, 442 , 0x0000EC);
tft.drawLine(0, 441, 1000, 441 , 0x0000EB);
tft.drawLine(0, 440, 1000, 440 , 0x0000EB);
tft.drawLine(0, 439, 1000, 439 , 0x0000EA);
tft.drawLine(0, 438, 1000, 438 , 0x0000EA);
tft.drawLine(0, 437, 1000, 437 , 0x0000E9);
tft.drawLine(0, 436, 1000, 456 , 0x0000E9);
tft.drawLine(0, 435, 1000, 435 , 0x0000E8);
tft.drawLine(0, 434, 1000, 434 , 0x0000E8);
tft.drawLine(0, 433, 1000, 433 , 0x0000E7);
tft.drawLine(0, 432, 1000, 432 , 0x0000E7);
tft.drawLine(0, 431, 1000, 431 , 0x0000E6);
tft.drawLine(0, 430, 1000, 430 , 0x0000E6);
tft.drawLine(0, 429, 1000, 429 , 0x0000E5);
tft.drawLine(0, 428, 1000, 428 , 0x0000E5);
tft.drawLine(0, 427, 1000, 427 , 0x0000E4);
tft.drawLine(0, 426, 1000, 426 , 0x0000E4);
tft.drawLine(0, 425, 1000, 425 , 0x0000E3);
tft.drawLine(0, 424, 1000, 424 , 0x0000E3);
tft.drawLine(0, 423, 1000, 423 , 0x0000E2);
tft.drawLine(0, 422, 1000, 422 , 0x0000E2);
tft.drawLine(0, 421, 1000, 421 , 0x0000E1);
tft.drawLine(0, 420, 1000, 420 , 0x0000E1);

tft.setCursor(54, 270);
TS_Point p = ts.getPoint();
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(2);
Serial.println(((sensorValue-3000)/10));
tft.setFont(&DSEG7_Classic_Bold_60);

tft.println(((sensorValue-3000)/10));
tft.setFont(&Nimbus_Mono_L_Bold_40);
tft.setCursor(250, 270);
tft.setTextSize(1);

tft.print("%");
delay(3000);
tft.fillScreen(HX8357_BLACK);
tft.fillRect(0, 0, 400, 50, HX8357_RED);
tft.setCursor(76, 30);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(1);
tft.setFont(&Nimbus_Mono_L_Bold_40);
tft.println("Battery");

tft.drawLine(0, 479, 1000, 479 , 0x0000FF);


tft.drawLine(0, 478, 1000, 478 , 0x0000FF);
tft.drawLine(0, 477, 1000, 477 , 0x0000FE);
tft.drawLine(0, 476, 1000, 476 , 0x0000FE);
tft.drawLine(0, 475, 1000, 475 , 0x0000FD);
tft.drawLine(0, 474, 1000, 474 , 0x0000FD);
tft.drawLine(0, 473, 1000, 473 , 0x0000FC);
tft.drawLine(0, 472, 1000, 472 , 0x0000FC);
tft.drawLine(0, 471, 1000, 471 , 0x0000FB);
tft.drawLine(0, 470, 1000, 470 , 0x0000FB);
tft.drawLine(0, 469, 1000, 469 , 0x0000FA);
tft.drawLine(0, 468, 1000, 468 , 0x0000FA);
tft.drawLine(0, 467, 1000, 467 , 0x0000F9);
tft.drawLine(0, 466, 1000, 466 , 0x0000F9);
tft.drawLine(0, 465, 1000, 465 , 0x0000F8);
tft.drawLine(0, 464, 1000, 464 , 0x0000F8);
tft.drawLine(0, 463, 1000, 463 , 0x0000F7);
tft.drawLine(0, 462, 1000, 462 , 0x0000F7);
tft.drawLine(0, 461, 1000, 461 , 0x0000F6);
tft.drawLine(0, 460, 1000, 460 , 0x0000F6);
tft.drawLine(0, 459, 1000, 459 , 0x0000F5);
tft.drawLine(0, 458, 1000, 458 , 0x0000F5);
tft.drawLine(0, 457, 1000, 457 , 0x0000F4);
tft.drawLine(0, 456, 1000, 456 , 0x0000F4);
tft.drawLine(0, 455, 1000, 455 , 0x0000F3);
tft.drawLine(0, 454, 1000, 454 , 0x0000F3);
tft.drawLine(0, 453, 1000, 453 , 0x0000F2);
tft.drawLine(0, 452, 1000, 452 , 0x0000F2);
tft.drawLine(0, 451, 1000, 451 , 0x0000F1);
tft.drawLine(0, 450, 1000, 450 , 0x0000F1);
tft.drawLine(0, 449, 1000, 449 , 0x0000EF);
tft.drawLine(0, 448, 1000, 448 , 0x0000EF);
tft.drawLine(0, 447, 1000, 447 , 0x0000EE);
tft.drawLine(0, 446, 1000, 446 , 0x0000EE);
tft.drawLine(0, 445, 1000, 445 , 0x0000ED);
tft.drawLine(0, 444, 1000, 444 , 0x0000ED);
tft.drawLine(0, 443, 1000, 443 , 0x0000EC);
tft.drawLine(0, 442, 1000, 442 , 0x0000EC);
tft.drawLine(0, 441, 1000, 441 , 0x0000EB);
tft.drawLine(0, 440, 1000, 440 , 0x0000EB);
tft.drawLine(0, 439, 1000, 439 , 0x0000EA);
tft.drawLine(0, 438, 1000, 438 , 0x0000EA);
tft.drawLine(0, 437, 1000, 437 , 0x0000E9);
tft.drawLine(0, 436, 1000, 456 , 0x0000E9);
tft.drawLine(0, 435, 1000, 435 , 0x0000E8);
tft.drawLine(0, 434, 1000, 434 , 0x0000E8);
tft.drawLine(0, 433, 1000, 433 , 0x0000E7);
tft.drawLine(0, 432, 1000, 432 , 0x0000E7);
tft.drawLine(0, 431, 1000, 431 , 0x0000E6);
tft.drawLine(0, 430, 1000, 430 , 0x0000E6);
tft.drawLine(0, 429, 1000, 429 , 0x0000E5);
tft.drawLine(0, 428, 1000, 428 , 0x0000E5);
tft.drawLine(0, 427, 1000, 427 , 0x0000E4);
tft.drawLine(0, 426, 1000, 426 , 0x0000E4);
tft.drawLine(0, 425, 1000, 425 , 0x0000E3);
tft.drawLine(0, 424, 1000, 424 , 0x0000E3);
tft.drawLine(0, 423, 1000, 423 , 0x0000E2);
tft.drawLine(0, 422, 1000, 422 , 0x0000E2);
tft.drawLine(0, 421, 1000, 421 , 0x0000E1);
tft.drawLine(0, 420, 1000, 420 , 0x0000E1);

if (outMsg > 0) {
int outMsg = int(rpm);

if (outMsg < 10) {

tft.fillRect(40, 100, 300, 200, HX8357_BLACK); //(x, y, xbredd,

tft.setCursor(128, 270);
tft.setTextColor(HX8357_WHITE);

tft.setTextSize(2);
tft.setFont(&DSEG7_Classic_Bold_60);
tft.print(outMsg , 0);
tft.setCursor(32, 270);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(2);
tft.setFont(&DSEG7_Classic_Bold_60);

tft.print("0");
tft.setTextSize(1);

tft.setFont(&Nimbus_Mono_L_Bold_40);
tft.setCursor(220, 270);

tft.print("km/h");

else {
tft.fillRect(40, 100, 300, 200, HX8357_BLACK); //(x, y, xbredd,
int outMsg = int(rpm);
Serial.println(outMsg);
Serial.println("");

tft.setCursor(32, 270);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(2);
tft.setFont(&DSEG7_Classic_Bold_60);

tft.print(outMsg, 0);

tft.setTextSize(1);

tft.setFont(&Nimbus_Mono_L_Bold_40);
tft.setCursor(220, 270);

tft.print("km/h");

}
}

else {

tft.fillRect(40, 100, 300, 200, HX8357_BLACK); //(x, y, xbredd,

tft.setCursor(32, 270);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(2);
tft.setFont(&DSEG7_Classic_Bold_60);
tft.print("0");

tft.setCursor(128, 270);
tft.setTextColor(HX8357_WHITE);
tft.setTextSize(2);
tft.setFont(&DSEG7_Classic_Bold_60);

tft.print("0");

tft.setTextSize(1);

tft.setFont(&Nimbus_Mono_L_Bold_40);
tft.setCursor(220, 270);
tft.print("km/h");

You might also like