#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET 4 // Reset pin 4 # (or -1 if sharing Arduino reset pin or ESP8266 D1)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// misc placeholders
#define log_all_codes false
#define PID_RPM 0x0C
#define PID_OutsideTemp 0x46
#define PID_FuelTank 0x2F
#define PID_EngineLoad 0x04
#define PID_CoolantTemp 0x05
#define PID_ECUvoltage 0x42

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET     4 // not sure what this is hahahaha
#define init_delay 3000 // duration that the startup screen is displayed.

float ECU_Volt = 99.99;

void Display_ECUvoltage(){
  int canMsg_can_id = 0x7E8;    
  if (canMsg_can_id == 0x7E8) { // response from ecu

      int canMsg_data2 = PID_ECUvoltage;
      if (canMsg_data2 == PID_ECUvoltage) { // ECU voltage +/- 12Vdc
        float canMsg_data3 = 47; 
        float canMsg_data4 = 0; 
        ECU_Volt = (canMsg_data3*256 + canMsg_data4)/1000;
        Serial.println(ECU_Volt);

      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      //display.print("Batt: ");
     // display.print(eng_temp);
      display.setTextSize(4);
      display.setCursor(0,0);
      display.print(ECU_Volt, 4);
      display.display(); // actually display all of the above      
      delay(2000);
      }
   }
  }


void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds
  
  // Clear the buffer
  //display.clearDisplay();
  Serial.println(__FILE__);

  Display_ECUvoltage();
  
  /*
  float Vcc = 12.7; 
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.println("Batt: ");
      display.setTextSize(4);
      //display.setTextColor(WHITE);
      display.setCursor(0,30);
      display.print(Vcc, 2);
      //display.print(" V");
      display.display(); // actually display all of the above

      delay(2000);
  */
  }


void loop() {
  // put your main code here, to run repeatedly:

  int canMsg_can_id = 0x7E8;    
  if (canMsg_can_id == 0x7E8) { // response from ecu

      int canMsg_data2 = PID_CoolantTemp;
      if (canMsg_data2 == PID_CoolantTemp) { // Coolant'
        int canMsg_data3 = 173; 
        int eng_temp = ((canMsg_data3 - 40)); // in Celcius
        Serial.println(eng_temp);

      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.setTextSize(1);
      display.print("Temp.Koelvloeistof:");     
      display.setTextSize(6);
      display.setCursor(10,20);
      display.print(eng_temp);
      display.display(); // actually display all of the above

      delay(500);

     
     }
  }
}

  

uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
Loading
ssd1306