#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SPI.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 //Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C //See datasheet for Address
#define SCREEN_ADDRESS1 0x3D //See datasheet for Address
// coolant sensor --
#define coolantsensorDivider 330 //defines the resistor value that is in series in the voltage divider
#define coolantsensorPin 1 //defines the analog pin of the input voltage from the voltage divider
#define NUMSAMPLES 5 //defines the number of samples to be taken for a smooth average
// --
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int mapsen = 0; // Set MAP sensor input on Analog port 0
int NUM_SAMPLES= 500; // number of samples
float sum = 0; // sum of samples taken
int sample_count = 0; // current sample number
float input_voltage = 4.72; //measured with multimeter
float peakBoost;
// coolent sensor --
const float steinconstA = 0.001459853293; //steinhart equation constant A, determined from wikipedia equations
const float steinconstB = 0.0002334241974; //steinhart equation constant B, determined from wikipedia equations
const float steinconstC = 0.00000008505461179; //steinhart equation constant C, determined from wikipedia equations
float peakECT;
int samples[NUMSAMPLES]; //coolant temp variable to store number of samples to be taken
// --
void setup() {
Serial.begin(115200);
// 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
}
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS1)) {
Serial.println(F("SSD1306 allocation no2 failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.display();
display1.clearDisplay();
display1.display();
delay(2000);
}
void loop() {
sum = 0;
sample_count = 0;
while (sample_count < NUM_SAMPLES) {
sum += float(analogRead(mapsen));
sample_count++;
delay(0.5);
}
float out_voltage = (sum / NUM_SAMPLES * input_voltage) / 1024;
float Pabs = (out_voltage*3294.1176/input_voltage-1052.4228)*0.0145; //absolute pressure in mbar *0.0145
if (Pabs < 1) {
Pabs = 0;
}
peakBoost = max(peakBoost,Pabs);
Serial.println(Pabs);
Serial.print("\t");
Serial.println(out_voltage,4);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("BOOST");
display.setTextSize(4);
display.setCursor(0, 12);
display.print(Pabs,1);
display.setCursor(80, 26);
display.setTextSize(2);
display.print(peakBoost,1);
display.fillRect(0, 47, (Pabs * 10), 16, WHITE);
display.drawRect(0, 47, 130, 16, WHITE);
display.drawRect((peakBoost * 10), 47, (129 - (peakBoost * 10)), 16, WHITE);
if (Pabs >= 10) {
display.setTextSize(1);
display.setCursor(68, 0);
display.print("OVER BOOST");
}
display.display();
// coolant sensor --
uint8_t i; //integer for loop
float average; //decimal for average
for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(coolantsensorPin); //takes samples at number defined with a short delay between samples
delay(10);
}
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i]; //adds all number of samples together
}
average /= NUMSAMPLES; //divides by number of samples to output the average
Serial.print("Average Analog Coolant Reading = ");
Serial.println(average); //analog value at analog pin into arduino
average = (coolantsensorDivider*average)/(1023-average); //conversion equation to read resistance from voltage divider
Serial.print("Coolant Sensor Resistance = ");
Serial.println(average);
float steinhart; //steinhart equation to estimate temperature value at any resistance from curve of thermistor sensor
steinhart = log(average); //lnR
steinhart = pow(steinhart,3); //(lnR)^3
steinhart *= steinconstC; //C*((lnR)^3)
steinhart += (steinconstB*(log(average))); //B*(lnR) + C*((lnR)^3)
steinhart += steinconstA; //Complete equation, 1/T=A+BlnR+C(lnR)^3
steinhart = 1.0/steinhart; //Inverse to isolate for T
steinhart -= 273.15; //Conversion from kelvin to celcius
peakECT = max(peakECT,steinhart);
Serial.print("Temperature = ");
Serial.print(steinhart, 1); //prints final temp in celcius
Serial.println(" *C");
delay(1000); //delay between readings
display1.clearDisplay();
display1.setTextColor(SSD1306_WHITE);
display1.setTextSize(1);
display1.setCursor(0, 0);
display1.print("COOLANT");
display1.setTextSize(4);
display1.setCursor(0, 12);
display1.print(steinhart,0);
display1.setCursor(84, 26);
display1.setTextSize(2);
display1.print(peakECT,0);
display1.fillRect(0, 47, steinhart, 16, WHITE);
display1.drawRect(0, 47, 100, 16, WHITE);
display1.drawRect(steinhart, 47, (120 - steinhart), 16, WHITE);
if (steinhart >= 100) {
display1.setTextSize(1);
display1.setCursor(72, 0);
display1.print("OVER HEAT");
}
display1.display();
}