Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // see the youtube video: https://youtu.be/B29AXVhaoNs
- #include <SoftwareSerial.h>
- #include "ELMduino.h" // modified version
- #include <SSD1283A.h>
- #include "dsdigital.c" // custom made DS-digital font
- #include <U8g2_for_Adafruit_GFX.h>
- #include <Bounce2.h>
- #include "jaguar.h" //defines the jaguar logo file
- SoftwareSerial mySerial(2, 3); // RX, TX
- #define ELM_PORT mySerial
- ELM327 myELM327;
- SSD1283A tft(/*CS=10*/ 10, /*A0 (DC)=*/ 9, /*RST=*/ 8, /*LED=*/ 7); // pins to NANO: hardware spi,cs,cd,reset, led (not used)
- U8G2_FOR_ADAFRUIT_GFX gfx;
- uint32_t speed = 0;
- uint32_t Coolant = 0; // added to ELMduino.cpp and ELMDuino.h [float Coolant();]
- float volt = 0; // added to ELMduino.cpp and ELMDuino.h [float volt();]
- uint32_t driving = 0; // added to ELMduino.cpp and ELMDuino.h [float driving();]
- uint32_t Intake = 0; // added to ELMduino.cpp and ELMDuino.h [float Intake();]
- uint32_t Ambient = 0; // added to ELMduino.cpp and ELMDuino.h [float Ambient();]
- const int pResistor = A6; // Photo resistor connected to Arduino analog pin A6, divided with resistor 8.3KΩ
- int bright; // Store brightness value from photo resistor (0-1023)
- int lux = 0; // recalculated brightness value (0-255)
- const int MIN_LIGHT = 1; // Minimum expected brightness
- const int MAX_LIGHT = 1023; // Maximum expected brightness
- int pwm_pin = 5; // pin from LED (display) to Nano digital pin 5
- int passFlag1 = 0; // refresh static data only once
- int passFlag2 = 0;
- int passFlag3 = 0;
- int passFlag4 = 0;
- int passFlag5 = 0;
- int passFlag6 = 0;
- float lastCoolant = 1000;
- float lastSpeed = 1000;
- float lastVolt = 0.0;
- float lastDriving = 10000;
- float lastAmbient = 1000;
- float lastIntake = 1000;
- #define BLACK 0x0000 // define custom color <RGB565>, visit: http://www.barth-dev.de/online/rgb565-color-picker/
- #define WHITE 0xFFFF
- #define ORANGE 0xFA60
- #define LIME 0xAFEB
- uint8_t rotation = 2; // rotate the display (1 to 4)
- const int buttonPin = A0;
- int buttonPushCounter = 0; // counter for the number of button presses
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- int period = 400; // refresh the screen in milliseconds
- unsigned long time_now = 0;
- Bounce debouncer = Bounce();
- //_____________________________________________________________
- void setup()
- {
- pinMode(pwm_pin, OUTPUT);
- pinMode(pResistor, INPUT);
- pinMode(buttonPin, INPUT);
- debouncer.attach(buttonPin);
- debouncer.interval(5); // interval in ms
- brightness();
- tft.init();
- gfx.begin(tft);
- tft.setRotation(rotation);
- delay(500);
- tft.fillScreen(BLACK);
- ELM_PORT.begin(38400);
- delay(200);
- tft.fillScreen(BLACK);
- tft.setTextColor(WHITE);
- tft.setTextSize(2);
- tft.setCursor(3, 3);
- tft.println("Attempting");
- tft.println("to connect");
- tft.print("to OBD2...");
- if (!myELM327.begin(ELM_PORT, '6')) // '6' refers to OBD protocol ISO_15765_11_BIT_500_KBAUD in ELMDuino.h
- {
- tft.fillScreen(BLACK);
- tft.setTextColor(ORANGE);
- tft.setCursor(3, 3);
- tft.println("Couldn't");
- tft.println("connect to");
- tft.print("OBD2!");
- //while (1);
- delay(3000);
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setCursor(15, 60);
- tft.println("Retrying");
- delay(3000);
- setup();
- }
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setCursor(3, 3);
- tft.println("Connected");
- tft.print("to OBD2!");
- delay(3000);
- tft.fillScreen(BLACK);
- tft.drawBitmap(2, 40, jaguar1, 126, 60, LIME); // comment out if you want the other logo
- //tft.drawBitmap(5, 5, jaguar2, 120, 120, WHITE); // uncomment if you want the other logo
- delay(5000);
- }
- //_____________________________________________________________
- void deBounce() { //debounce the button
- time_now = millis();
- while (millis() < time_now + period) {
- debouncer.update();
- }
- int value = debouncer.read();
- buttonState = value;
- if (buttonState != lastButtonState) {
- if (buttonState == HIGH) {
- buttonPushCounter++;
- }
- }
- if (buttonPushCounter == 6) {
- buttonPushCounter = 0;
- }
- }
- //_____________________________________________________________
- void loop() {
- brightness();
- deBounce();
- if (buttonPushCounter == 0) {
- showCoolant();
- }
- else if (buttonPushCounter == 1) {
- showSpeed();
- }
- else if (buttonPushCounter == 2) {
- showVolt();
- }
- else if (buttonPushCounter == 3) {
- showDriving();
- }
- else if (buttonPushCounter == 4) {
- showAmbient();
- }
- else if (buttonPushCounter == 5) {
- showIntake();
- }
- lastButtonState = buttonState;
- }
- //_____________________________________________________________
- void showCoolant()
- { float tempCoolant = myELM327.Coolant();
- if (myELM327.status == ELM_SUCCESS)
- {
- Coolant = (uint32_t)tempCoolant;
- // Coolant = (Coolant * 9 / 5) + 32; //uncomment if you use Fahrenheit
- }
- else {
- elmError();
- }
- if (passFlag1 == 0) { //draw the static text only once
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(2);
- tft.setCursor(3, 90);
- tft.print("Coolant C"); // change C to F if you use Fahrenheit
- tft.drawCircle(104, 92, 3, LIME); // comment out if you use Fahrenheit
- passFlag1++;
- passFlag6 = 0;
- }
- if (Coolant != lastCoolant) { // prevent display refresh when the same value is read
- tft.fillRect(13, 12, 103, 46, BLACK); // x start, y start, x width, y height
- gfx.setFont(dsdigital_54);
- gfx.setCursor(9, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(String(Coolant));
- }
- lastCoolant = Coolant;
- lastIntake = 1000;
- }
- //_____________________________________________________________
- void showSpeed()
- { float tempSPEED = myELM327.speed();
- if (myELM327.status == ELM_SUCCESS)
- {
- speed = (uint32_t)tempSPEED;
- // speed = speed * 0.6213711922; //uncomment if you use MPH
- }
- else {
- elmError();
- }
- if (passFlag2 == 0) {
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(3);
- tft.setCursor(35, 90);
- tft.print("KPH"); // change "KPH" to "MPH" if you use miles
- passFlag2++;
- passFlag1 = 0;
- }
- if (speed != lastSpeed) {
- tft.fillRect(13, 12, 103, 46, BLACK);
- gfx.setFont(dsdigital_54);
- gfx.setCursor(9, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(String(speed));
- }
- lastSpeed = speed;
- lastCoolant = 1000;
- }
- //_____________________________________________________________
- void showVolt()
- { float tempVOLT = myELM327.volt();
- if (myELM327.status == ELM_SUCCESS)
- {
- volt = (uint32_t)tempVOLT;
- }
- else {
- elmError();
- }
- if (passFlag3 == 0) {
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(3);
- tft.setCursor(28, 90);
- tft.print("Volt");
- passFlag3++;
- passFlag2 = 0;
- }
- if (volt != lastVolt) {
- tft.fillRect(7, 12, 116, 46, BLACK);
- gfx.setCursor(3, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(volt, 1);
- }
- lastVolt = volt;
- lastSpeed = 1000;
- }
- //_____________________________________________________________
- void showDriving()
- { float tempDRIVE = myELM327.driving();
- if (myELM327.status == ELM_SUCCESS)
- {
- driving = (uint32_t)tempDRIVE;
- }
- else {
- elmError();
- }
- if (passFlag4 == 0) {
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(2);
- tft.setCursor(20, 70);
- tft.print("Minutes");
- tft.setCursor(32, 87);
- tft.print("since");
- tft.setCursor(32, 104);
- tft.print("start");
- passFlag4++;
- passFlag3 = 0;
- }
- if (driving != lastDriving) {
- tft.fillRect(13, 12, 103, 46, BLACK);
- gfx.setFont(dsdigital_54);
- gfx.setCursor(9, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(String(driving));
- }
- lastDriving = driving;
- lastVolt = 0.0;
- }
- //_____________________________________________________________
- void showAmbient()
- { float tempAMBIENT = myELM327.Ambient();
- if (myELM327.status == ELM_SUCCESS)
- {
- Ambient = (uint32_t)tempAMBIENT;
- // Ambient = (Ambient * 9 / 5) + 32; //uncomment if you use Fahrenheit
- }
- else {
- elmError();
- }
- if (passFlag5 == 0) {
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(2);
- tft.setCursor(3, 90);
- tft.print("Outside C"); // change C to F if you use Fahrenheit
- tft.drawCircle(105, 92, 3, LIME); // comment out if you use Fahrenheit
- passFlag5++;
- passFlag4 = 0;
- }
- if (Ambient != lastAmbient) {
- tft.fillRect(13, 12, 103, 46, BLACK);
- gfx.setFont(dsdigital_54);
- gfx.setCursor(9, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(String(Ambient));
- }
- lastAmbient = Ambient;
- lastDriving = 10000;
- }
- //_____________________________________________________________
- void showIntake()
- { float tempINTAKE = myELM327.Intake();
- if (myELM327.status == ELM_SUCCESS)
- {
- Intake = (uint32_t)tempINTAKE;
- // Intake = (Intake * 9 / 5) + 32; //uncomment if you use Fahrenheit
- }
- else {
- elmError();
- }
- if (passFlag6 == 0) {
- tft.fillScreen(BLACK);
- tft.setTextColor(LIME);
- tft.setTextSize(2);
- tft.setCursor(3, 90);
- tft.print("Intake C"); // change C to F if you use Fahrenheit
- tft.drawCircle(105, 92, 3, LIME); // comment out if you use Fahrenheit
- passFlag6++;
- passFlag5 = 0;
- }
- if (Intake != lastIntake) {
- tft.fillRect(13, 12, 103, 46, BLACK);
- gfx.setFont(dsdigital_54);
- gfx.setCursor(9, 58);
- gfx.setForegroundColor(WHITE);
- gfx.print(String(Intake));
- }
- lastIntake = Intake;
- lastAmbient = 1000;
- }
- //_____________________________________________________________
- void elmError() {
- tft.fillScreen(BLACK);
- tft.setTextColor(ORANGE);
- tft.setTextSize(3);
- tft.setCursor(3, 3);
- tft.print("Error:");
- tft.setCursor(40, 50);
- tft.setTextSize(5);
- tft.println(myELM327.status);
- delay(2000);
- loop();
- }
- //_____________________________________________________________
- void brightness()
- {
- bright = analogRead(pResistor); // readout the sensor
- lux = map(bright, MIN_LIGHT, MAX_LIGHT, 10, 255); // map the light reading (1, 1023, 10, 255)
- if (lux < 1) {
- analogWrite(pwm_pin, 10); // brightness value at night
- }
- else if (lux >= 1 && lux < 254) {
- analogWrite(pwm_pin, lux); // brightness value mapping from 1 to 254
- }
- else if (lux >= 254) {
- analogWrite(pwm_pin, 255); // brightness if maximum light is reached
- }
- }
- unsigned long testCircles(uint8_t radius, uint16_t color) {
- unsigned long start;
- int x, y, r2 = radius, w, h;
- }
Add Comment
Please, Sign In to add comment