I have a project that I'm building, to provide LCD readings of engine speed, oil pressure, OAT, coollant temperature etc. I have most of it working with a set of functions that setup the display for the required items. Currently, I have been commenting out 4 of the five (lcdWrite) options just for test.
The next stage is to use a push button to cycle through the options to be displayed. I have looked into FSM today and I think it might be the way ahead, so would appreciate comments. You can see my code so far here, I don't profess to be any sort of expert!
/*
Last modified 24th January 2014 13:20
Added DHT code
Working with PCF8574A on breadboard at address 0x38
2 of I2C 16x2 display 0x27 and 0x38
Edited to create functions
Alert between 2200 and 2400 RPM. on a 4 cylinder, frequencies are 73 and 80Hz
RPM = frequency x 30
Pin designation:
A0 Battery voltage via divider
A1 (Con 9) Oil pressure sensor
A2 (Con 8) Water temperature
A3 (Con 7) Fuel level
A4 LCD
A5 LCD
A6 Courtesy switch - Acive low(Con 6)
A7 DHT11 sensor data
D4 Push button (Con 5)
D5 (Con 4)
D6 Backlight
D7 (Con 2) W terminal from alternator
D8 (Con 1) Sidelight circuit
D9 LED Yellow
D10 LED Blue
D11 LED Green
D12 LED Red
D13 Courtesy light relay (Con 3)
-----( Import needed libraries )-----*/
#include <FreqPeriodCounter.h>
#include <Albert.h>
#include <Streaming.h>
#include <TimerOne.h>
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
// - - - - - - - - - - - - - - - - - - - - - -
#include <dht.h>
#define dht_dpin A2 //no ; here. Set equal to channel sensor is on
dht DHT;
// - - - - - - - - - - - - - - - - - - - - - -
#define NUM_SAMPLES 10
int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
// - - - - - - - - - - - - - - - - - - - - - -
const byte counterPin = 3; // Frequency count input from points
const byte counterInterrupt = 1; // = physical pin 3
const byte led = 13; // Set pin for alarm output
const byte RPM = 30;
int sensorPin = A0; // select the input pin for Temp sensor
int sensorPin2 = A1; // select the input pin for Oil pressure
int inputPin = 5; // select the pin for the push button
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the DHT11
int ledValue = LOW;
int backLight=6;
Bounce bouncer = Bounce();
FreqPeriodCounter counter(counterPin, micros);
// Set the LCD I2C addresses for 0x38 & 0x27 16x2 displays
// LiquidCrystal_I2C lcd2(0x38, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE); // Green LCD
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Blue LCD
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(backLight, OUTPUT);
// Setup the button
pinMode( inputPin ,INPUT);
attachInterrupt(counterInterrupt, counterISR, CHANGE);
// After setting up the button, setup the object
bouncer .attach( inputPin );
bouncer .interval(5);
Serial.begin(9600);
lcd.begin(16,2); // initialize the lcd
// lcd2.begin(16,2);
lcdWrite5();
delay(1000);
backlightpin();
delay(1000);
testLeds();
;
}
void loop() {
analogWrite(backLight, 255);
volTage();
DHT.read11(dht_dpin);
// deBounce();
alarmLed();
lcdWrite1();
// lcdWrite2();
// lcdWrite3();
// lcdWrite4();
delay(500);
}
// End of main program. Functions below
void volTage() {
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
sum += analogRead(A0);
sample_count++;
delay(10);
}
// calculate the voltage
// use 5.0 for a 5.0V ADC reference voltage
// 5.015V is the calibrated reference voltage
voltage = ((float)sum / (float)NUM_SAMPLES * 5.015) / 2750.0;
// send voltage for display on Serial Monitor was / 1024
// voltage multiplied by 11 when using voltage divider that
// divides by 11. 11.132 is the calibrated voltage divide
// value
// Serial.print(voltage * 11.132);
// Serial.println (" V");
sample_count = 0;
sum = 0;
}
void backlightpin() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(backLight, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(backLight, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
//
}
}
void lcdWrite5() {
lcd.setCursor(0,0);
lcd.print("Greetings Driver");
lcd.setCursor(0,1);
lcd.print("Start self test ");
}
void deBounce() {
if (bouncer.update() && bouncer.read() == LOW)
{
ledValue = ! ledValue;
digitalWrite(ledPin, ledValue);
} }
void lcdWrite4() {
int speed = (RPM * (counter.hertz()));
lcd.setCursor(0,0);
lcd.print("Air temp: C");
lcd.setCursor(9,0);
lcd.print(DHT.temperature);
lcd.setCursor(0,1);
lcd.print("Speed: RPM"); // Write the text to the LCD
lcd.setCursor(8,1);
lcd.print(speed); // Send the actual value to the LCD
}
void lcdWrite3() {
int speed = (RPM * (counter.hertz()));
lcd.setCursor(0,0);
lcd.print("Outside C");
lcd.setCursor(9,0);
lcd.print(DHT.temperature);
lcd.setCursor(0,1);
lcd.print("Humidity %");
lcd.setCursor(9,1);
lcd.print(DHT.humidity); }
void lcdWrite2() {
int speed = (RPM * (counter.hertz()));
lcd.setCursor(0,0);
lcd.print("Water temp C");
lcd.setCursor(11,0);
lcd.print(analogRead(sensorPin));
lcd.setCursor(0,1);
lcd.print("Oil press PSI");
lcd.setCursor(10,1);
lcd.print(analogRead(sensorPin2)); }
void lcdWrite1() {
int speed = (RPM * (counter.hertz()));
lcd.setCursor(0,0);
lcd.print("Voltage V");
lcd.setCursor(9,0);
lcd.print(voltage * 11.132);
// lcd.print(counter.hertz());
lcd.setCursor(0,1);
lcd.print("Speed: RPM"); // Write the text to the LCD
lcd.setCursor(8,1);
lcd.print(speed); // Send the actual value to the LCD
}
void alarmLed() {
int speed = (RPM * (counter.hertz()));
if (speed > 2200 && speed <2400){
digitalWrite(led,HIGH); }
else
{ digitalWrite(led,LOW);
delay(1);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void testLeds() {
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
digitalWrite(9, LOW);
delay(1000); }
void counterISR() {
counter.poll();