Cabrio_Bob

2020-06-28 arduino code

Jun 28th, 2020
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.02 KB | None | 0 0
  1. // see the youtube video: https://youtu.be/B29AXVhaoNs
  2.  
  3. #include <SoftwareSerial.h>
  4. #include "ELMduino.h"                 // modified version
  5. #include <SSD1283A.h>
  6. #include "dsdigital.c"                // custom made DS-digital font
  7. #include <U8g2_for_Adafruit_GFX.h>
  8. #include <Bounce2.h>
  9. #include "jaguar.h"                   //defines the jaguar logo file
  10.  
  11. SoftwareSerial mySerial(2, 3); // RX, TX
  12. #define ELM_PORT mySerial
  13. ELM327 myELM327;
  14.  
  15. SSD1283A tft(/*CS=10*/ 10, /*A0 (DC)=*/ 9, /*RST=*/ 8, /*LED=*/ 7); // pins to NANO: hardware spi,cs,cd,reset, led (not used)
  16. U8G2_FOR_ADAFRUIT_GFX gfx;
  17.  
  18. uint32_t speed = 0;
  19. uint32_t Coolant = 0;                 // added to ELMduino.cpp and ELMDuino.h [float Coolant();]
  20. float volt = 0;                       // added to ELMduino.cpp and ELMDuino.h [float volt();]
  21. uint32_t driving = 0;                 // added to ELMduino.cpp and ELMDuino.h [float driving();]
  22. uint32_t Intake = 0;                  // added to ELMduino.cpp and ELMDuino.h [float Intake();]
  23. uint32_t Ambient = 0;                 // added to ELMduino.cpp and ELMDuino.h [float Ambient();]
  24.  
  25. const int pResistor = A6;             // Photo resistor connected to Arduino analog pin A6, divided with resistor 8.3KΩ
  26. int bright;                           // Store brightness value from photo resistor (0-1023)
  27. int lux = 0;                          // recalculated brightness value (0-255)
  28. const int MIN_LIGHT = 1;              // Minimum expected brightness
  29. const int MAX_LIGHT = 1023;           // Maximum expected brightness
  30. int pwm_pin = 5;                      // pin from LED (display) to Nano digital pin 5
  31.  
  32. int passFlag1 = 0;                    // refresh static data only once
  33. int passFlag2 = 0;
  34. int passFlag3 = 0;
  35. int passFlag4 = 0;
  36. int passFlag5 = 0;
  37. int passFlag6 = 0;
  38.  
  39. float lastCoolant = 1000;
  40. float lastSpeed = 1000;
  41. float lastVolt = 0.0;
  42. float lastDriving = 10000;
  43. float lastAmbient = 1000;
  44. float lastIntake = 1000;
  45.  
  46. #define BLACK   0x0000                // define custom color <RGB565>, visit: http://www.barth-dev.de/online/rgb565-color-picker/
  47. #define WHITE   0xFFFF
  48. #define ORANGE  0xFA60
  49. #define LIME    0xAFEB
  50.  
  51. uint8_t rotation = 2;                 // rotate the display (1 to 4)
  52.  
  53. const int  buttonPin = A0;
  54. int buttonPushCounter = 0;            // counter for the number of button presses
  55. int buttonState = 0;                  // current state of the button
  56. int lastButtonState = 0;              // previous state of the button
  57. int period = 400;                     // refresh the screen in milliseconds
  58. unsigned long time_now = 0;
  59. Bounce debouncer = Bounce();
  60.  
  61. //_____________________________________________________________
  62.  
  63. void setup()
  64. {
  65.   pinMode(pwm_pin, OUTPUT);
  66.   pinMode(pResistor, INPUT);
  67.   pinMode(buttonPin, INPUT);
  68.   debouncer.attach(buttonPin);
  69.   debouncer.interval(5);                              // interval in ms
  70.   brightness();
  71.   tft.init();
  72.   gfx.begin(tft);
  73.   tft.setRotation(rotation);
  74.   delay(500);
  75.   tft.fillScreen(BLACK);
  76.  
  77.   ELM_PORT.begin(38400);
  78.   delay(200);
  79.  
  80.   tft.fillScreen(BLACK);
  81.   tft.setTextColor(WHITE);
  82.   tft.setTextSize(2);
  83.   tft.setCursor(3, 3);
  84.   tft.println("Attempting");
  85.   tft.println("to connect");
  86.   tft.print("to OBD2...");
  87.  
  88.   if (!myELM327.begin(ELM_PORT, '6'))               // '6' refers to OBD protocol ISO_15765_11_BIT_500_KBAUD in ELMDuino.h
  89.   {
  90.     tft.fillScreen(BLACK);
  91.     tft.setTextColor(ORANGE);
  92.     tft.setCursor(3, 3);
  93.     tft.println("Couldn't");
  94.     tft.println("connect to");
  95.     tft.print("OBD2!");
  96.     //while (1);
  97.     delay(3000);
  98.     tft.fillScreen(BLACK);
  99.     tft.setTextColor(LIME);
  100.     tft.setCursor(15, 60);
  101.     tft.println("Retrying");
  102.     delay(3000);
  103.     setup();
  104.   }
  105.   tft.fillScreen(BLACK);
  106.   tft.setTextColor(LIME);
  107.   tft.setCursor(3, 3);
  108.   tft.println("Connected");
  109.   tft.print("to OBD2!");
  110.   delay(3000);
  111.   tft.fillScreen(BLACK);
  112.   tft.drawBitmap(2, 40, jaguar1, 126, 60, LIME);      // comment out if you want the other logo
  113.   //tft.drawBitmap(5, 5, jaguar2, 120, 120, WHITE);   // uncomment if you want the other logo
  114.   delay(5000);
  115. }
  116.  
  117. //_____________________________________________________________
  118.  
  119. void deBounce() {           //debounce the button
  120.   time_now = millis();
  121.   while (millis() < time_now + period) {
  122.     debouncer.update();
  123.   }
  124.   int value = debouncer.read();
  125.   buttonState = value;
  126.   if (buttonState != lastButtonState) {
  127.     if (buttonState == HIGH) {
  128.       buttonPushCounter++;
  129.     }
  130.   }
  131.   if (buttonPushCounter == 6) {
  132.     buttonPushCounter = 0;
  133.   }
  134. }
  135.  
  136. //_____________________________________________________________
  137.  
  138. void loop() {
  139.   brightness();
  140.   deBounce();
  141.   if (buttonPushCounter == 0) {
  142.     showCoolant();
  143.   }
  144.   else if (buttonPushCounter == 1) {
  145.     showSpeed();
  146.   }
  147.   else if (buttonPushCounter == 2) {
  148.     showVolt();
  149.   }
  150.   else if (buttonPushCounter == 3) {
  151.     showDriving();
  152.   }
  153.   else if (buttonPushCounter == 4) {
  154.     showAmbient();
  155.   }
  156.   else if (buttonPushCounter == 5) {
  157.     showIntake();
  158.   }
  159.   lastButtonState = buttonState;
  160. }
  161.  
  162. //_____________________________________________________________
  163.  
  164. void showCoolant()
  165. { float tempCoolant = myELM327.Coolant();
  166.   if (myELM327.status == ELM_SUCCESS)
  167.   {
  168.     Coolant = (uint32_t)tempCoolant;
  169.     // Coolant = (Coolant * 9 / 5) + 32; //uncomment if you use Fahrenheit
  170.   }
  171.   else {
  172.     elmError();
  173.   }
  174.   if (passFlag1 == 0) {                   //draw the static text only once
  175.     tft.fillScreen(BLACK);
  176.     tft.setTextColor(LIME);
  177.     tft.setTextSize(2);
  178.     tft.setCursor(3, 90);
  179.     tft.print("Coolant  C");              // change C to F if you use Fahrenheit
  180.     tft.drawCircle(104, 92, 3, LIME);     // comment out if you use Fahrenheit
  181.     passFlag1++;
  182.     passFlag6 = 0;
  183.   }
  184.   if (Coolant != lastCoolant) {           // prevent display refresh when the same value is read
  185.     tft.fillRect(13, 12, 103, 46, BLACK); // x start, y start, x width, y height
  186.     gfx.setFont(dsdigital_54);
  187.     gfx.setCursor(9, 58);
  188.     gfx.setForegroundColor(WHITE);
  189.     gfx.print(String(Coolant));
  190.   }
  191.   lastCoolant = Coolant;
  192.   lastIntake = 1000;
  193. }
  194.  
  195. //_____________________________________________________________
  196.  
  197. void showSpeed()
  198. { float tempSPEED = myELM327.speed();
  199.   if (myELM327.status == ELM_SUCCESS)
  200.   {
  201.     speed = (uint32_t)tempSPEED;
  202.     // speed = speed * 0.6213711922;        //uncomment if you use MPH
  203.   }
  204.   else {
  205.     elmError();
  206.   }
  207.   if (passFlag2 == 0) {
  208.     tft.fillScreen(BLACK);
  209.     tft.setTextColor(LIME);
  210.     tft.setTextSize(3);
  211.     tft.setCursor(35, 90);
  212.     tft.print("KPH");                   // change "KPH" to "MPH" if you use miles
  213.     passFlag2++;
  214.     passFlag1 = 0;
  215.   }
  216.   if (speed != lastSpeed) {
  217.     tft.fillRect(13, 12, 103, 46, BLACK);
  218.     gfx.setFont(dsdigital_54);
  219.     gfx.setCursor(9, 58);
  220.     gfx.setForegroundColor(WHITE);
  221.     gfx.print(String(speed));
  222.   }
  223.   lastSpeed = speed;
  224.   lastCoolant = 1000;
  225. }
  226.  
  227. //_____________________________________________________________
  228.  
  229. void showVolt()
  230. { float tempVOLT = myELM327.volt();
  231.   if (myELM327.status == ELM_SUCCESS)
  232.   {
  233.     volt = (uint32_t)tempVOLT;
  234.   }
  235.   else {
  236.     elmError();
  237.   }
  238.   if (passFlag3 == 0) {
  239.     tft.fillScreen(BLACK);
  240.     tft.setTextColor(LIME);
  241.     tft.setTextSize(3);
  242.     tft.setCursor(28, 90);
  243.     tft.print("Volt");
  244.     passFlag3++;
  245.     passFlag2 = 0;
  246.   }
  247.   if (volt != lastVolt) {
  248.     tft.fillRect(7, 12, 116, 46, BLACK);
  249.     gfx.setCursor(3, 58);
  250.     gfx.setForegroundColor(WHITE);
  251.     gfx.print(volt, 1);
  252.   }
  253.   lastVolt = volt;
  254.   lastSpeed = 1000;
  255. }
  256.  
  257. //_____________________________________________________________
  258.  
  259. void showDriving()
  260. { float tempDRIVE = myELM327.driving();
  261.   if (myELM327.status == ELM_SUCCESS)
  262.   {
  263.     driving = (uint32_t)tempDRIVE;
  264.   }
  265.   else {
  266.     elmError();
  267.   }
  268.   if (passFlag4 == 0) {
  269.     tft.fillScreen(BLACK);
  270.     tft.setTextColor(LIME);
  271.     tft.setTextSize(2);
  272.     tft.setCursor(20, 70);
  273.     tft.print("Minutes");
  274.     tft.setCursor(32, 87);
  275.     tft.print("since");
  276.     tft.setCursor(32, 104);
  277.     tft.print("start");
  278.     passFlag4++;
  279.     passFlag3 = 0;
  280.   }
  281.   if (driving != lastDriving) {
  282.     tft.fillRect(13, 12, 103, 46, BLACK);
  283.     gfx.setFont(dsdigital_54);
  284.     gfx.setCursor(9, 58);
  285.     gfx.setForegroundColor(WHITE);
  286.     gfx.print(String(driving));
  287.   }
  288.   lastDriving = driving;
  289.   lastVolt = 0.0;
  290. }
  291.  
  292. //_____________________________________________________________
  293.  
  294. void showAmbient()
  295. { float tempAMBIENT = myELM327.Ambient();
  296.   if (myELM327.status == ELM_SUCCESS)
  297.   {
  298.     Ambient = (uint32_t)tempAMBIENT;
  299.     // Ambient = (Ambient * 9 / 5) + 32;  //uncomment if you use Fahrenheit
  300.   }
  301.   else {
  302.     elmError();
  303.   }
  304.   if (passFlag5 == 0) {
  305.     tft.fillScreen(BLACK);
  306.     tft.setTextColor(LIME);
  307.     tft.setTextSize(2);
  308.     tft.setCursor(3, 90);
  309.     tft.print("Outside  C");            // change C to F if you use Fahrenheit
  310.     tft.drawCircle(105, 92, 3, LIME);   // comment out if you use Fahrenheit
  311.     passFlag5++;
  312.     passFlag4 = 0;
  313.   }
  314.   if (Ambient != lastAmbient) {
  315.     tft.fillRect(13, 12, 103, 46, BLACK);
  316.     gfx.setFont(dsdigital_54);
  317.     gfx.setCursor(9, 58);
  318.     gfx.setForegroundColor(WHITE);
  319.     gfx.print(String(Ambient));
  320.   }
  321.   lastAmbient = Ambient;
  322.   lastDriving = 10000;
  323. }
  324.  
  325. //_____________________________________________________________
  326.  
  327. void showIntake()
  328. { float tempINTAKE = myELM327.Intake();
  329.   if (myELM327.status == ELM_SUCCESS)
  330.   {
  331.     Intake = (uint32_t)tempINTAKE;
  332.     // Intake = (Intake * 9 / 5) + 32;  //uncomment if you use Fahrenheit
  333.   }
  334.   else {
  335.     elmError();
  336.   }
  337.   if (passFlag6 == 0) {
  338.     tft.fillScreen(BLACK);
  339.     tft.setTextColor(LIME);
  340.     tft.setTextSize(2);
  341.     tft.setCursor(3, 90);
  342.     tft.print("Intake   C");            // change C to F if you use Fahrenheit
  343.     tft.drawCircle(105, 92, 3, LIME);   // comment out if you use Fahrenheit
  344.     passFlag6++;
  345.     passFlag5 = 0;
  346.   }
  347.   if (Intake != lastIntake) {
  348.     tft.fillRect(13, 12, 103, 46, BLACK);
  349.     gfx.setFont(dsdigital_54);
  350.     gfx.setCursor(9, 58);
  351.     gfx.setForegroundColor(WHITE);
  352.     gfx.print(String(Intake));
  353.   }
  354.   lastIntake = Intake;
  355.   lastAmbient = 1000;
  356. }
  357.  
  358. //_____________________________________________________________
  359.  
  360. void elmError() {
  361.  
  362.   tft.fillScreen(BLACK);
  363.   tft.setTextColor(ORANGE);
  364.   tft.setTextSize(3);
  365.   tft.setCursor(3, 3);
  366.   tft.print("Error:");
  367.   tft.setCursor(40, 50);
  368.   tft.setTextSize(5);
  369.   tft.println(myELM327.status);
  370.   delay(2000);
  371.   loop();
  372. }
  373.  
  374. //_____________________________________________________________
  375.  
  376. void brightness()
  377. {
  378.   bright = analogRead(pResistor);                           // readout the sensor
  379.   lux = map(bright, MIN_LIGHT, MAX_LIGHT, 10, 255);         // map the light reading (1, 1023, 10, 255)
  380.   if (lux < 1) {
  381.     analogWrite(pwm_pin, 10);                               // brightness value at night
  382.   }
  383.   else if (lux >= 1 && lux < 254) {
  384.     analogWrite(pwm_pin, lux);                              // brightness value mapping from 1 to 254
  385.   }
  386.   else if (lux >= 254) {
  387.     analogWrite(pwm_pin, 255);                              // brightness if maximum light is reached
  388.   }
  389. }
  390.  
  391. unsigned long testCircles(uint8_t radius, uint16_t color) {
  392.   unsigned long start;
  393.   int           x, y, r2 = radius, w, h;
  394. }
Add Comment
Please, Sign In to add comment