Hello there, where do I begin?
I had an idea long ago to customise my motorbike a little bit and do an electronics project at the same time. I'm not afraid of electronics, but programming is completely new to me. My motorbike had only a speedo, and I thought that I would love to have RPM, oil temperature, oil pressure, air temperature, and a stopwatch + clock! Oh my kingdom for a clock!
It was then that I stumbled upon the Arduino and I was most interested. So I bought one :D.
I have now got the Arduino + 16x2 LCD sort of doing what I want them to do (although minus the stopwatch so far). Only problem is, I'm not a programmer, and I cobbled the program together only out of what I learnt from tutorials on the net (so a bit of praise, at least? Pleeeze?).
The sensors are all simulated for now by a pot, and I have them mapped pretty much how I want them to be.
My problems are as follows:
- The code is EXTREMELY clumsy. Of this I am sure. Can anyone give me some pointers for tidying it up?
- The stopwatch is missing from the time and date display. I want it shown to the right of the time, with a start/stop button and a reset button. Any hints? I'm going a bit "loop"y if you know what I mean.
- The button for changing the page is a bit temperamental. I suspect it's down to timing and my crap coding
. You need to give it a quick tap to get to the clock page, but to get back to engine stats page you have to hold it down for longer than a second.
Here is a video of it in action so you know what I'm going on about:
Here is my, er, code... . And yes, there are no remarks in the code. Bad form
. Sorry!
Thanks in advance.
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
#include <DateTime.h>
#include <DateTimeStrings.h>
#define dt_SHORT_DAY_STRINGS
#define dt_SHORT_MONTH_STRINGS
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
DateTime.sync(DateTime.makeTime(0, 3, 11, 6, 9, 2011)); // sec, min, hour, date, month, year // Replace this with the most current time
pinMode (inPin, INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("A");
delay(100);
lcd.setCursor(1, 0);
lcd.print("r");
delay(100);
lcd.setCursor(2, 0);
lcd.print("m");
delay(100);
lcd.setCursor(3, 0);
lcd.print("s");
delay(100);
lcd.setCursor(4, 0);
lcd.print("t");
delay(100);
lcd.setCursor(5, 0);
lcd.print("r");
delay(100);
lcd.setCursor(6, 0);
lcd.print("o");
delay(100);
lcd.setCursor(7, 0);
lcd.print("n");
delay(100);
lcd.setCursor(8, 0);
lcd.print("g");
delay(100);
lcd.setCursor(11, 0);
lcd.print("M");
delay(100);
lcd.setCursor(12, 0);
lcd.print("T");
delay(100);
lcd.setCursor(13, 0);
lcd.print("5");
delay(100);
lcd.setCursor(14, 0);
lcd.print("0");
delay(100);
lcd.setCursor(15, 0);
lcd.print("0");
delay(100);
lcd.setCursor(2, 1);
lcd.print("version 0.03");
delay(3000);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
delay(50);
lcd.scrollDisplayRight();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
lcd.scrollDisplayLeft();
}
void loop()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RPM: Air: C");
lcd.setCursor(0, 1);
lcd.print("Oil: C bar");
do {
val = digitalRead(inPin);
int oilTempReading = analogRead(A0);
int oilTemp = map(oilTempReading, 0, 1023, 0, 150);
lcd.setCursor(4, 1);
lcd.print(oilTemp);
int rpmReading = analogRead(A1);
int rpm = map(rpmReading, 0, 1023, 0, 8500);
lcd.setCursor(4, 0);
lcd.print(rpm);
int airReading = analogRead(A2);
int air = map(airReading, 0, 1023, 0, 60);
lcd.setCursor(13, 0);
lcd.print(air);
int oilPressReading = analogRead(A3);
int oilPress = map(oilPressReading, 0, 1023, 0, 5);
lcd.setCursor(11, 1);
lcd.print(oilPress);
delay(160);
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(" ");
}
while (val == LOW);
do {
val = digitalRead(inPin);
if(DateTime.available()) {
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
}
while (val == LOW);
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits,DEC);
}
void digitalClockDisplay(){
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,1);
//lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
if(DateTime.Day <10)
lcd.print('0');
lcd.print(DateTime.Day,DEC);
lcd.print("/");
//lcd.print(DateTimeStrings.monthStr(DateTime.Month));
if(DateTime.Month <10)
lcd.print('0');
lcd.print(DateTime.Month,DEC);
lcd.print("/");
lcd.print((DateTime.Year,DEC)+2000);
//lcd.print(" ");
if(DateTime.Hour <10)
lcd.setCursor(1,0);
lcd.setCursor(0,0);
// digital clock display of current time
lcd.setCursor(0,0);
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
}