#include <SoftwareSerial.h>
//// Pin definitions
#define LED 13
#define DIS 7
#define VENT 6
#define VAC 5
#define BRAKE 4 // Move this to pin 2 to use interrupt 0
#define SPEED 3
#define CNTRL A0
#define S_POS A1
//// Global vars
byte ledState;
volatile unsigned long deltaT, lastTime;
SoftwareSerial LCD(13,12); // RX, TX pins
//-------------------------------------------------------------------------------------------
void setup()
{
LCD.begin(9600); // All SerLCDs are 9600 Baud by default
if (0) toggleSplash(); // This is only necessary once?
// delay(1200); // Required if splash is displayed
pinMode(BRAKE, INPUT); // A "1" when brake is applied
pinMode(LED, OUTPUT); // The on board LED on pin 13
pinMode(SPEED, INPUT); // Speed is relative to space between pulses
attachInterrupt(1, speedInterupt, RISING);
pinMode(VENT, OUTPUT); // Vent
pinMode(VAC, OUTPUT); // Vac
pinMode(DIS, OUTPUT); // Disable = 1
digitalWrite(DIS, HIGH); // Start with cruise disabled
}
//-------------------------------------------------------------------------------------------
void loop()
{
// clearScreen();
// selectLineOne();
// showBrake();
showSpeed();
// delay(100);
// digitalWrite(LED, ledState ^= 1); // Toggle the LED to indicate looping
}
// Prints a double value with number of decimal places determined by precision.
// example: double2ASCII(buf, 3.1415, 2); // prints 3.14 (two decimal places).
// The allowed range of precision is 1 to 6 inclusive. Otherwise the value will
// be displayed as an integer (no decimal point and all to the right of it will
// not be displayed).
char * double2ASCII(char *buffer, double val, char precision)
{
double frac, frac1, mult = 1;
char i, padding[7];
if (precision > 0 && precision < 7) {
frac = frac1 = (abs(val) - (int)abs(val)); // Get the fractional portion (2 copies)
while (precision--) mult *= 10; // Multiplier to move fraction to left
frac *= mult; // of decimal point
// Fill a buffer with leading zeros for the value to the right of decimal point
for (i = 0; frac1 && (frac1 *= 10) < 1; i++) padding = '0';
_ padding = 0;_
* sprintf(buffer, "%d.%s%u", (int)val, padding, (unsigned)frac); //*
* }*
* else sprintf(buffer, "%d", (int)val); // No precision; treat as an integer*
* return buffer;*
}
void showBrake()
{
* LCD.print(digitalRead(BRAKE) ? "Br=1 " : "Br=0 ");*
}
void showSpeed()
{
char buffer[17];
* clearScreen();*
* selectLineOne();*
* LCD.print(double2ASCII(buffer, (double)deltaT, 0));*
}
// Interrupt service routine for speedometer pulses.
// Calculates the time since the last interrupt. The
// result is in global var deltaT.
void speedInterupt()
{
* unsigned long diff, now = micros();*
// noInterrupts(); // Using this causes loop to stop
* if (now < lastTime) now += lastTime; // Accomodate rollover*
* diff = now - lastTime;*
* if (diff > 100) // Can also use to gauge min speed for cruise*
* {*
* deltaT = diff;*
* lastTime = now;*
* }*
// interrupts();
}
////////////////////////////////////// LCD SUPPORT BELOW \\\\\\\\\\\\\\\\\\
//-------------------------------------------------------------------------------------------
void clearScreen()
{
* //clears the screen, you will use this a lot!*
* LCD.write(0xFE);*
* LCD.write(0x01);*
}
//-------------------------------------------------------------------------------------------
void selectLineOne()
{
* //puts the cursor at line 0 char 0.*
* LCD.write(0xFE); //command flag*
* LCD.write(128); //position*
}
//-------------------------------------------------------------------------------------------
void selectLineTwo()
{
* //puts the cursor at line 0 char 0.*
* LCD.write(0xFE); //command flag*
* LCD.write(192); //position*
}
//-------------------------------------------------------------------------------------------
void toggleSplash()
{
* //this toggles the spalsh screenif off send this to turn onif on send this to turn off*
* LCD.write(0x7C); //command flag = 124 dec*
* LCD.write(9); // 0x09*
}
//--------------------------------------------------------------------------------------------
*// Sets the cursor position of the SerLCD. *
void setCursorPosition(int row, int col)
{
int pos;
if (row == 1) pos = col;
else pos = col + 64;
pos = pos + 128; // Cursor move command
LCD.write(0xFE);
LCD.write(pos);
}
[/quote]