Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // see the video at: https://youtu.be/_3wJfPCTEe0
- #include <SoftwareSerial.h>
- #include "ELMduino.h"
- #include <SSD1283A.h>
- SoftwareSerial mySerial(2, 3); // RX, TX
- #define ELM_PORT mySerial
- ELM327 myELM327;
- SSD1283A tft(/*CS=10*/ 10, /*A0 (DC)=*/ 9, /*RST=*/ 8, /*LED=*/ 7);
- uint32_t rpm = 0;
- const int pResistor = A6;
- int bright;
- int lux = 0;
- const int MIN_LIGHT=1;
- const int MAX_LIGHT=1023;
- int pwm_pin = 5;
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- uint8_t rotation = 3; // rotate the display (1 to 4)
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, HIGH);
- pinMode(pwm_pin, OUTPUT);
- pinMode(pResistor, INPUT);
- brightness();
- tft.init();
- tft.setRotation(rotation);
- delay(500);
- tft.fillScreen(BLACK);
- ELM_PORT.begin(38400);
- delay(200);
- tft.setCursor(3, 3);
- tft.setTextColor(WHITE);
- tft.setTextSize(2);
- tft.print("Attemptingto connectto OBDII");
- if (!myELM327.begin(ELM_PORT, '6'))
- {
- tft.fillScreen(BLACK);
- tft.setCursor(3, 3);
- tft.println("Couldn't");
- tft.println("connect to");
- tft.print("OBDII !");
- while (1);
- }
- tft.fillScreen(BLACK);
- tft.print("Connected to ELM327");
- delay(5000);
- tft.fillScreen(BLACK);
- tft.setTextColor(GREEN);
- tft.setTextSize(2);
- tft.setCursor(3, 70);
- tft.print("RPM");
- }
- void brightness(void)
- {
- 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 if no light is shining
- 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
- }
- void loop()
- {
- float tempRPM = myELM327.rpm();
- if (myELM327.status == ELM_SUCCESS)
- {
- rpm = (uint32_t)tempRPM;
- tft.setCursor(10, 10);
- tft.setTextColor(WHITE);
- tft.setTextSize(5);
- tft.print(rpm);
- delay(500);
- tft.fillRect(3,3,100,60,BLACK); // x start, y start, x width, y length
- //Serial.print("RPM: "); Serial.println(rpm);
- }
- else
- {
- tft.fillScreen(BLACK);
- tft.setTextColor(GREEN);
- tft.setTextSize(2);
- tft.setCursor(3, 3);
- tft.println(myELM327.status);
- delay(2000);
- tft.fillScreen(BLACK);
- tft.setTextColor(GREEN);
- tft.setTextSize(2);
- tft.setCursor(3, 70);
- tft.print("RPM");
- }
- }
Add Comment
Please, Sign In to add comment