//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>
#include <Keypad.h>
ezButton SDd(2);
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'U',},
{ '4', '5', '6', 'D',},
{ '7', '8', '9', 'P',},
{ 'C', '0', 'E', 'B'}
};
uint8_t colPins[COLS] = { 7, 8, 9, 10 }; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = { 3, 4, 5, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
byte squareChar[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte underscoreChar[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111
};
byte blankChar[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte arrowChar[] = {
B00000,
B00100,
B01000,
B11111,
B01000,
B00100,
B00000,
B00000
};
int headSet = 0;
int underscore = 1;
float currentMillis;
float underblinkMillis;
float spinMillis;
int blinkX = 0;
int headCodeCheck = 0;
int spincount = 0;
int headCount = 0;
int arrowCount = 0;
int maxStart = 0;
int selectedinput = 0;
int selectRouteCount;
int inputType;
void setup()
{
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(2, HIGH);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Railtech PIS");
lcd.setCursor(0,1);
lcd.print("V1.0 System Test");
lcd.createChar(0, squareChar);
lcd.setCursor(0,2);
lcd.write(0);
delay(1000);
lcd.setCursor(1,2);
lcd.write(0);
delay(1000);
lcd.setCursor(2,2);
lcd.write(0);
SDd.loop();
if(SDd.getState() == 1){
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(2, HIGH);
delay(1250);
lcd.setCursor(3,2);
lcd.write(0);
delay(1000);
lcd.setCursor(4,2);
lcd.write(0);
lcd.clear();
}
if(SDd.getState() == 0){
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(2, HIGH);
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print("Error!");
lcd.setCursor(0,3);
lcd.print("No SD Card Detected!");
}
}
void loop()
{
char key = keypad.getKey();
currentMillis = millis();
if(selectedinput == 0){
if(selectRouteCount == 0){
lcd.setCursor(0,1);
lcd.print("Route Code");
lcd.createChar(3, arrowChar);
lcd.setCursor(10,1);
lcd.write(3);
lcd.setCursor(0,2);
lcd.print("Headcode ");
}
if(selectRouteCount == 1){
lcd.setCursor(0,1);
lcd.print("Route Code ");
lcd.setCursor(0,2);
lcd.print("Headcode");
lcd.createChar(3, arrowChar);
lcd.setCursor(8,2);
lcd.write(3);
}
if(key == 'D'){
selectRouteCount += 1;
}
if(key == 'U'){
selectRouteCount -= 1;
}
if(selectRouteCount < 0){
selectRouteCount = 0;
}
if(selectRouteCount > 1){
selectRouteCount = 1;
}
if(key == 'E'){
if(selectRouteCount == 0){
inputType = 0;
selectedinput = 1;
lcd.clear();
}
if(selectRouteCount == 1){
inputType = 1;
selectedinput = 1;
lcd.clear();
}
}
}
if(headSet == 0 && selectedinput == 1 && inputType == 1){
if(blinkX == 4 && key == 'E'){
headSet = 1;
}
if (currentMillis - underblinkMillis >= 500 && blinkX < 4)
{
//restart this TIMER
underblinkMillis = currentMillis;
lcd.setCursor(0,0);
lcd.print("Train Headcode");
if(underscore == 1 && blinkX < 4){
lcd.createChar(1, underscoreChar);
lcd.setCursor(blinkX,2);
lcd.write(1);
}
if(underscore == 0 && blinkX < 4){
lcd.createChar(2, blankChar);
lcd.setCursor(blinkX,2);
lcd.write(2);
}
underscore += 1;
if(underscore > 1){
underscore = 0;
}
}
if(blinkX >= 4){
lcd.createChar(2, blankChar);
lcd.setCursor(blinkX,2);
lcd.write(2);
}
if (key != NO_KEY && key != 'C' && key != 'E' && key != 'U' && key != 'D' && key != 'P' && key != 'B' && blinkX < 4) {
Serial.println(key);
lcd.setCursor(blinkX,2);
lcd.print(key);
blinkX++;
}
if (key == 'C') {
lcd.createChar(2, blankChar);
lcd.setCursor(blinkX,2);
lcd.write(2);
blinkX -= 1;
lcd.createChar(2, blankChar);
lcd.setCursor(blinkX,2);
lcd.write(2);
}
if(blinkX < 0){
blinkX = 0;
}
}
if(headSet == 1 && headCodeCheck == 0){
lcd.setCursor(0,0);
lcd.print("Train Headcode");
if(currentMillis - spinMillis >= 750){
spinMillis = currentMillis;
if(spincount == 0){
lcd.setCursor(0,2);
lcd.print("Checking Headcode -");
}
if(spincount == 1){
lcd.setCursor(0,2);
lcd.print("Checking Headcode |");
}
spincount += 1;
if(spincount > 1){
spincount = 0;
}
headCount++;
if(headCount >= 10){
headCodeCheck = 1;
lcd.clear();
maxStart = 2;
}
}
}
if(headCodeCheck == 1){
lcd.setCursor(0,0);
lcd.print("Starting Point");
if(arrowCount == 0){
lcd.setCursor(0,1);
lcd.print("York");
lcd.createChar(3, arrowChar);
lcd.setCursor(4,1);
lcd.write(3);
lcd.setCursor(0,2);
lcd.print("Leeds ");
lcd.setCursor(0,3);
lcd.print("Huddersfield ");
}
if(arrowCount == 1){
lcd.setCursor(0,1);
lcd.print("York ");
lcd.setCursor(0,2);
lcd.print("Leeds");
lcd.createChar(3, arrowChar);
lcd.setCursor(5,2);
lcd.write(3);
lcd.setCursor(0,3);
lcd.print("Huddersfield ");
}
if(arrowCount == 2){
lcd.setCursor(0,1);
lcd.print("York ");
lcd.setCursor(0,2);
lcd.print("Leeds ");
lcd.setCursor(0,3);
lcd.print("Huddersfield");
lcd.createChar(3, arrowChar);
lcd.setCursor(12,3);
lcd.write(3);
}
if(key == 'D'){
arrowCount += 1;
}
if(key == 'U'){
arrowCount -= 1;
}
if(arrowCount < 0){
arrowCount = 0;
}
if(arrowCount > maxStart){
arrowCount = maxStart;
}
}
}