#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ 1, 2, 3, '>' },
{ 4, 5, 6, '<' },
{ 7, 8, 9, 'E' },
{ '0', 'D', 'S', 'M' }
};
uint8_t colPins[COLS] = { A3, A2, A1, A0 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 5, 4,3, 2 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int count;
bool run=false;
void setup() { // set up everything
pinMode(6, OUTPUT); // Set the pin to be OUTPUT
pinMode(13,OUTPUT);
count=0;
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
TIMSK1 = B00000110; //Set OCIE1A to 1 so we enable compare match A
OCR1A = 15466 -60; // set compare register A to 15466 - interrupt latency correction
OCR1B = 7733; // set compare register B to 500 to give vpulse width 2000us
digitalWrite(6,false); // make thw v pulse potput false (D6)
digitalWrite(13,false);
lcd.begin(16,2); // initialise the lcd to 20 char wide and 4 rows (for simulation but ok for 16*2 display also)
lcd.setCursor(0, 0); // set lcd cursor to start of display lin 0
lcd.print(" Brake Tester"); // print first line of welcome message on display
lcd.setCursor(0,1); // set lcd cursor to start of display line 1
lcd.print(" Pulse Generator"); // print second line of welcome message on display
//run = true;
delay (2000); // wait 4 seconds displaying welcome message
lcd.clear(); // clear the display
}
void loop()
{
char key = keypad.getKey();
if (key!= 0)
{
if (key == '>')
{
run=true;
TCCR1B = B00000001;
}
if (key == '<')
{
run=false;
TCCR1B = B00000000;
}
}
if (run==true)
{
lcd.setCursor(0,0);
lcd.print("Rolling Road Sim");
lcd.setCursor(0,1);
lcd.print(" Press < to Stop");
}
else
{
lcd.setCursor(0,0);
lcd.print("Rolling Road Sim");
lcd.setCursor(0,1);
lcd.print(" Press > to Run ");
}
}
ISR(TIMER1_COMPA_vect) // timer 1 comparator A interrupt vector
{ // come here every time counter = comparison register A
TCNT1 = 0; // set the timer back to 0 so it resets for next interrupt
digitalWrite(6,true); // then write new state to the pulse output on D6 (high to start v pulse)
if (count>=400) digitalWrite(13,false);
}
ISR(TIMER1_COMPB_vect) // timer 1 comparator B interrupt vector
{ // come here every time counter = comparison register B
digitalWrite(6,false); // write new state to the pulse output on D6 (low to end vpulse)
count ++;
if (count>=4297)
{
count=0;
digitalWrite(13,true);
}
}