Cabrio_Bob

Arduino nano, HC-05, ELM327, display RPM on a display

Jun 21st, 2020
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. // see the video at:        https://youtu.be/_3wJfPCTEe0
  2.  
  3. #include <SoftwareSerial.h>
  4. #include "ELMduino.h"
  5. #include <SSD1283A.h>
  6.  
  7. SoftwareSerial mySerial(2, 3); // RX, TX
  8. #define ELM_PORT mySerial
  9.  
  10. ELM327 myELM327;
  11.  
  12. SSD1283A tft(/*CS=10*/ 10, /*A0 (DC)=*/ 9, /*RST=*/ 8, /*LED=*/ 7);
  13.  
  14. uint32_t rpm = 0;
  15.  
  16. const int pResistor = A6;
  17. int bright;
  18. int lux = 0;
  19. const int MIN_LIGHT=1;
  20. const int MAX_LIGHT=1023;
  21. int pwm_pin = 5;
  22.  
  23. #define  BLACK   0x0000
  24. #define BLUE    0x001F
  25. #define RED     0xF800
  26. #define GREEN   0x07E0
  27. #define CYAN    0x07FF
  28. #define MAGENTA 0xF81F
  29. #define YELLOW  0xFFE0
  30. #define WHITE   0xFFFF
  31.  
  32. uint8_t rotation = 3;   // rotate the display (1 to 4)
  33.  
  34. void setup()
  35. {
  36.     pinMode(LED_BUILTIN, OUTPUT);
  37.   digitalWrite(LED_BUILTIN, HIGH);
  38.   pinMode(pwm_pin, OUTPUT);
  39.   pinMode(pResistor, INPUT);
  40.   brightness();
  41.   tft.init();
  42.     tft.setRotation(rotation);
  43.   delay(500);
  44.   tft.fillScreen(BLACK);
  45.  
  46.   ELM_PORT.begin(38400);
  47.  
  48.   delay(200);
  49.  
  50.   tft.setCursor(3, 3);
  51.   tft.setTextColor(WHITE);
  52.   tft.setTextSize(2);
  53.  
  54.   tft.print("Attemptingto connectto OBDII");
  55.  
  56.   if (!myELM327.begin(ELM_PORT, '6'))
  57.   {
  58.     tft.fillScreen(BLACK);
  59.     tft.setCursor(3, 3);
  60.     tft.println("Couldn't");
  61.     tft.println("connect to");
  62.     tft.print("OBDII !");
  63.     while (1);
  64.   }
  65.     tft.fillScreen(BLACK);
  66.   tft.print("Connected to ELM327");
  67.   delay(5000);
  68.   tft.fillScreen(BLACK);
  69.     tft.setTextColor(GREEN);
  70.   tft.setTextSize(2);
  71.   tft.setCursor(3, 70);
  72.   tft.print("RPM");
  73.  
  74. }
  75.  
  76.  void brightness(void)
  77.  {
  78.   bright = analogRead(pResistor);                  // readout the sensor
  79.   lux = map(bright, MIN_LIGHT, MAX_LIGHT, 10, 255);         // map the light reading (1, 1023, 10, 255)
  80.   if (lux < 1){ analogWrite(pwm_pin, 10); }             // brightness value if no light is shining
  81.   else if (lux >= 1 && lux < 254){ analogWrite(pwm_pin, lux); }   // brightness value mapping from 1 to 254
  82.   else if (lux >= 254){ analogWrite(pwm_pin, 255); }        // brightness if maximum light is reached
  83.  }
  84.  
  85. void loop()
  86. {
  87.   float tempRPM = myELM327.rpm();
  88.  
  89.   if (myELM327.status == ELM_SUCCESS)
  90.   {
  91.     rpm = (uint32_t)tempRPM;
  92.    
  93.       tft.setCursor(10, 10);
  94.   tft.setTextColor(WHITE);
  95.   tft.setTextSize(5);
  96.   tft.print(rpm);
  97.   delay(500);
  98.   tft.fillRect(3,3,100,60,BLACK); // x start, y start, x width, y length  
  99.     //Serial.print("RPM: "); Serial.println(rpm);
  100.   }
  101.   else
  102.   {
  103.       tft.fillScreen(BLACK);
  104.     tft.setTextColor(GREEN);
  105.   tft.setTextSize(2);
  106.   tft.setCursor(3, 3);
  107.     tft.println(myELM327.status);
  108.     delay(2000);
  109.       tft.fillScreen(BLACK);
  110.     tft.setTextColor(GREEN);
  111.   tft.setTextSize(2);
  112.   tft.setCursor(3, 70);
  113.   tft.print("RPM");
  114.   }
  115. }
Add Comment
Please, Sign In to add comment