I have a VDO Oil Pressure Sender that I'm going to use together with OLED to show an oil pressure in my car.
However I'm struggling with a code. I've copy-pasted a part of the code, responsible for sensor calculations from GitHub - MurdockGT/Classic-Car-Digital-Dash: Full digital monitoring of classic car with coolant fan control with Arduino Mega
I've tried combining it together with OLED libraries, the problem is - code is compiling fine, display is initializing, shows Adafruit logo and after that - it's completely blank. I've tried to put data through Serial monitor instead of an oled - same result, Serial shows completely nothing. What's the issue?
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//dla SPI:
#define OLED_MOSI 9 //SDA
#define OLED_CLK 10 //SCL
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const byte oilPressPin = A0;
const float aRef = 5.0;
void setup() {
Serial.begin(9600);
//display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(3000);
display.clearDisplay();
delay(3000);
display.display();
}
void loop() {
int getOilpressure();
// using VDO Oil Pressure guage, 10BAR
float array[12] = {
// Voltage From Sensor on left // Corresponding Temprature on right ,
//You find this by using voltage divider forumla. Current set is for a 330 ohm R1 and R2 is your VDO Oil Pressure sensor
2.105263 , 0.1,
1.944444 , 14.5,
1.764706 , 29,
1.5625 , 43.5,
1.071429 , 72.5,
0.4141189 , 100,
};
int val = 0;
for (int i = 1; i <= 10; i++)
{
val += analogRead(oilPressPin);
}
val += 5; // allows proper rounding due to using integer math
val /= 10; // get average value
float voltage = val / 1023.0 * aRef;
float oilp;
if (voltage >= array[0])
{
oilp = array[1];
}
else if (voltage <= array[10])
{
oilp = array[11];
}
else
{
for (int i = 1; i < 12; i++)
{
int ii = 2 * i;
if (voltage <= array[ii] && voltage >= array[ii + 2])
{
oilp = array[ii + 1] + ((array[ii + 3] - array[ii + 1]) * (voltage - array[ii]) / (array[ii + 2] - array[ii]));
break;
}
}
}
int oilpres = oilp;
return oilpres;
//added a delay, thought it can help - it didn't
delay(200);
Serial.print("Pressure: ");
Serial.print(oilpres);
/*
* display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Pressure:");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(5,25);
display.println(oilpres);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 54);
display.println("BAR");
display.setCursor(70, 0);
display.println("Temp:");
display.setTextSize(2);
display.setCursor(65, 25);
display.println("test");
display.setTextSize(1);
display.setCursor(90, 54);
display.println("*C");
display.display();
delay(500);
*/
}
Also i think you were either going to create a function or thought you did.int getOilpressure();And personally i would set the pinMode() for all pins used even if i do set them to INPUT which is default.
Deva_Rishi:
Also i think you were either going to create a function or thought you did.int getOilpressure();And personally i would set the pinMode() for all pins used even if i do set them to INPUT which is default.
Thanks for the advice!
Now,
I want to convert the original code (which is not mine) to show readout as a float. I've managed to do that, however, when the pressure is 0 BAR on OLED - it shows it as "inf", probably it tries to divide by zero somewhere in the code? How to fix that?
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//dla SPI:
#define OLED_MOSI 9 //SDA
#define OLED_CLK 10 //SCL
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const byte oilPressPin = A0;
const byte oilWarninglight = 7;
const float aRef = 5.0;
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
//display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(3000);
display.clearDisplay();
delay(3000);
display.display();
}
void loop() {
// using VDO Oil Pressure gauge, 10BAR
float array[12] = {
// Voltage From Sensor on left // Corresponding Temperature on right ,
//You find this by using voltage divider formula. Current set is for a 330 ohm R1 and R2 is your VDO Oil Pressure sensor
2.118263 , 0.1,
1.944444 , 1.0,
1.764706 , 2.0,
1.5625 , 3.0,
1.071429 , 5.0,
0.4141189 , 6.89,
};
int val = 0;
for (float i = 1; i <= 10; i++)
{
val += analogRead(oilPressPin);
}
val += 5.0; // allows proper rounding due to using integer math
val /= 10.0; // get average value
float voltage = val / 1023.0 * aRef;
float oilp;
if (voltage >= array[0])
{
oilp = array[1];
}
else if (voltage <= array[10])
{
oilp = array[11];
}
else
{
for (float i = 1; i < 12; i++)
{
int ii = 2 * i;
if (voltage <= array[ii] && voltage >= array[ii + 2])
{
oilp = array[ii + 1] + ((array[ii + 3] - array[ii + 1]) * (voltage - array[ii]) / (array[ii + 2] - array[ii]));
break;
}
}
}
float oilpres = oilp;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(5,0);
display.println("Pressure:");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,25);
display.println(oilpres);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(22, 42);
display.println("BAR");
display.setCursor(70, 0);
display.println("Oil Temp:");
display.setTextSize(2);
display.setCursor(75, 25);
display.println("24");
display.setTextSize(1);
display.setCursor(100, 25);
display.println("*C");
display.setTextSize(1);
display.setCursor(22, 52);
display.println("RIP");
display.setTextSize(1);
display.setCursor(80, 52);
display.println("COLD!");
display.display();
delay(200);
return oilpres;
}
it shows it as "inf", probably it tries to divide by zero somewhere in the code?
on an ESP that will just crash.
just as a note for (float i = 1; i <= 10; i++) this should not be a float.
also i am wondering if this actually prints the float value ?display.println(oilpres);