Hi all,
This is my first attempt at coding and first time attempt doing arduino stuff. I have my code below. Basically my plan is to display 4 sensor data sets on the screen (4 row) with the name/bar graph/numerical value.
I have a few questions:
-
Is there any obvious clean up someone can help me with? I know the timer can mostly be deleted and changed to a single line now that i know the 122hz modification of timer 2 works best. Maybe i can clean up the print commands to single ones for each line instead of so much cursor commands.
-
Regarding the numerical values, if i wanted to make them come up as 1 decimal place, or 1 decimal place at 0.5 resolution, what would i need to do to change it? float or double functions?
-
With the sensors, the pressure sensors are 3 wire and output a voltage which is easy to translate to ADC values, but the temperature sensors are 2 different ones. 1 sensor i plan to run 5v to and use a 200ohm resistor to read the voltage drop across. this gives a good voltage range due to the sensors low resistance range (18-323ohm).
The water temp sensor is part of the oem vehicle system, so the oem ecu i believe sends a 5v signal and earths at the ECU also. Then I gather there must be a pullup inside the ECU itself (must be a divider made somehow)? Im not sure, ill have to research this more to see if i can easily tap into it somehow without affecting the ECU readings.
-
For these sensor readings, is it best to use the map function as done in my code below? The pressure sensors are linear so i think its ok, the temp sensors are mostly linear in the important areas (above 50deg c).
-
Im thinking to add a second menu which i could switch to with a push button switch, maybe display the data in a different way. Not sure how to do this, any links to some standard code or libraries for push button commands and making a 2nd screen instance?
//included functions
#include <LiquidCrystal.h>
#include <LcdBarGraph.h>
//pwm pin for temp driver must change pwm out to pin 11 or 3!!!
const int tempPin = 5;
const int pwmPin = 6;
const int divisor = 256;
int rawtemp = 0;
int stackpwm = 0;
//lcd setup
int lcdNumCols = 16;
int lcdNumRow = 4;
//sensor inputs
const int otempPin = 1;
const int fpressPin = 2;
const int opressPin = 3;
int wtemp = 0;
int rawotemp = 0;
int otemp = 0;
int rawfpress = 0;
int fpress = 0;
int rawopress = 0;
int opress = 0;
//lcd bar graph instances, must change 11 and 3 to any other pins!!!
LiquidCrystal lcd(13, 12, 7, 6, 5, 4);
// LCD driving pins, LiquidCrystal(rs, enable, d4, d5, d6, d7) 4bit LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)8bit
LcdBarGraph lbg0(&lcd, 10, 4, 0); // (&lcd, lcdNumCols used, start X, start Y)
LcdBarGraph lbg1(&lcd, 10, 4, 1);
LcdBarGraph lbg2(&lcd, 10, 4, 2);
LcdBarGraph lbg3(&lcd, 10, 4, 3);
void setup(){
// -- initializing the LCD
lcd.begin(lcdNumCols, lcdNumRow);
lcd.clear();
// -- do some delay: some time I've got broken visualization
delay(100);
pinMode(otempPin, INPUT);
pinMode(fpressPin, INPUT);
pinMode(opressPin, INPUT);
//setup pin modes for PWM temp driving
pinMode(tempPin, INPUT);
pinMode(pwmPin, OUTPUT);
Serial.begin(9600);
}
//setup pwm frequency change to close to 100hz, must use pin 11 or 3 for this!!!
void setPwmFrequency(int pwmPin, int divisor) {
byte mode;
switch(divisor) {
case 128: mode = 0x05; break; //result 245.098 hz
case 256: mode = 0x06; break; //result 122.549 hz which is good, dash wants 100hz
case 1024: mode = 0x7; break; //result 30.6373 hz
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
//setup loop
void loop()
{
//set map for temp driver
rawtemp = analogRead(tempPin); //set pin for temp input reading
//settings for pwm output with map (input, min in, max in, min out, max out), Stack 70deg=236PWM 110deg=98PWM
stackpwm = map(rawtemp, 323, 596, 236, 98);
analogWrite(pwmPin, stackpwm); //setting for pwm out (output pin, output function)
//setup temperature and pressure readings
rawotemp = analogRead(otempPin);
otemp = map(rawotemp, 391, 939, 50, 150);
lcd.setCursor(16, 1);
lcd.print(otemp);
rawfpress = analogRead(fpressPin);
fpress = map(rawfpress, 102, 921, 0, 100);
lcd.setCursor(14, 2);
lcd.print(fpress);
rawopress = analogRead(opressPin);
opress = map(rawopress, 102, 921, 0, 100);
lcd.setCursor(14, 3);
lcd.print(opress);
wtemp = map(rawtemp, 110, 596, 0, 110); //setting output temperature for lcd
lcd.setCursor(16, 0);
lcd.print(wtemp);
// -- draw bar graph from the analog value readed
lbg0.drawValue(analogRead(tempPin), 600);
lbg1.drawValue(analogRead(otempPin), 939);
lbg2.drawValue(analogRead(fpressPin), 921);
lbg3.drawValue(analogRead(opressPin), 921);
lcd.setCursor(0, 0);
lcd.print("Wat");
lcd.setCursor(19, 0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Oil");
lcd.setCursor(19, 1);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("Fuel");
lcd.setCursor(17, 2);
lcd.print("PSI");
lcd.setCursor(0, 3);
lcd.print("Oil");
lcd.setCursor(17, 3);
lcd.print("PSI");
// -- do some delay: frequent draw may cause broken visualization
delay(1000);
}