Here the first part of the code:
/*
VERSION WITH 3x4 KEYPAD, not capacitive touch unit
Modified to use * and # in place of two buttons
d. bodnar revised 5-23-2016
To Do:
1. LCD to show Loco #, speed, direction L=5721 S=88 D=> 16 Characters
2. LCD line two Function display 0000 0000 0000 ?????
3. Left button calls up Menu
4. Right button used to change loco in focus
5. POT center = stop, CCW = reverse to max, CW = forward to max
*/
#include "Arduino.h"
byte Key;
#include<EEPROM.h>
char key ;
int LED = 13; // LED to blink when DCC packets are sent in loop
byte Fx = 0;
// Array set for 4 Loco2 - change the 7 numbers in the next 7 declarations
int maxLocos = 4;// number of loco addresses
int LocoAddress[4] = {1830, 3, 999, 4444};
int LocoDirection[4] = {1, 1, 1, 1};
int LocoSpeed[4] = {0, 0, 0, 0};
byte LocoFN0to4[4];
byte LocoFN5to8[4];
byte Locofn9to12[4];// 9-12 not yet implemented
int xxxxx = 0;
int pot1Pin = A3;
int potValue = 0;
int NewPotValue = 0;
int potValueOld = 0;
int counter = 0;
int Trigger1 = 3;
int Trigger2 = 4;
int TrigVal1 = 0;
int TrigVal2 = 0;
int old_speed = 0;
int ZeroSpeedFlag = 0;
int ActiveAddress = 0; // make address1 active
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
int z = 0;
int powerTemp = 0;
int i = 0;
char VersionNum[] = "1.7F "; ///////////////////////// //////////////////////VERSION HERE///////
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8 }; //{8,7,6,5 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11}; // {11,10,9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;
boolean blink = false;
boolean ledPin_state;
int debug = 0; // set to 1 to show debug info on serial port - may cause issues with DCC++ depending on what is sent
void setup() {
pinMode(Trigger1, INPUT);
pinMode(Trigger2, INPUT);
digitalWrite(Trigger1, HIGH);// turn on pullup resistors
digitalWrite(Trigger2, HIGH);// turn on pullup resistors
// randomSeed(analogRead(0));
pinMode(LED, OUTPUT);
lcd.begin (16, 2); // LCD is 16 characters x 2 lines
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH); // Switch on the backlight
lcd.home (); // go home
Serial.begin (115200);
lcd.setCursor(0, 0);
lcd.print("DCC++ Throttle");
lcd.setCursor(0, 1);
lcd.print("5-26-16 - v");
for (int i = 0; i < 4; i++) {
lcd.print(VersionNum[i]);
//delay(500);
}
getAddresses(); // read eeprom
Serial.print("5-22-2016 version ");
for (int i = 0; i < 4; i++) {
Serial.print(VersionNum[i]);
//delay(500);
}
if (debug == 1) Serial.println("");
Serial.print("<0>");// power off to DCC++ unit
delay(1500);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
// showFirstLine();
lcd.clear();
} // END SETUP
void loop() {
TrigVal1 = digitalRead(Trigger1); // read the input pin
TrigVal2 = digitalRead(Trigger2); // read the input pin
key = keypad.getKey();
// if (key) {
if (key == 42) { // *
all2ZeroSpeeed();
getLocoAddress();
key = 0;
}
// }
if (TrigVal1 == 0) {
all2ZeroSpeeed();
getLocoAddress();
// showFirstLine();
}
potValue = analogRead(pot1Pin); // read the value from the sensor
if (potValue != potValueOld) {
NewPotValue = 1;
//doMainLCD();
}
else NewPotValue = 0;
potValueOld = potValue;
potValue = (potValue / 4) - 128;
if (potValue <= 0) {
LocoDirection[ActiveAddress] = 0; // backward
}
else LocoDirection[ActiveAddress] = 1; // forward
potValue = abs(potValue);
if (potValue >= 126) potValue = 126; // max is 127
if (potValue <= 3) potValue = 0; // set to zero if close to zero
LocoSpeed[ActiveAddress] = potValue;
if (NewPotValue == 1) {
doDCC();
doMainLCD();
NewPotValue = 0;
delay(50);
}
if (key == 35) { // #
ActiveAddress++;
if (ActiveAddress >= 4) ActiveAddress = 0;
//showFirstLine();
doMainLCD();
delay(200);
key = 0;
}
if (TrigVal2 == 0) { // change loco # on right button press
ActiveAddress++;
if (ActiveAddress >= 4) ActiveAddress = 0;
//showFirstLine();
doMainLCD();
delay(200);
}
// key = keypad.getKey();
// Serial.print("KKKKKKKKKKKKKKKKKEY =");
// Serial.println(key);
if (key != 42 && key != 35 && key >= 40) {
// if (key == 42) Serial.println("****************************");
// Serial.print("KEY =");
// Serial.println(key);
doFunction();
}
// doMainLCD();
} //END LOOP
//START DO FUNCTION BUTTONS
int doFunction() {
key = key - 48 - 1; // convert from ascii value
lcd.setCursor (14, 1); // go to end of 2nd line
/// lcd.print("FN code ");
lcd.print(key, DEC);
if (debug == 1) Serial.print("got a keypad button ");
if (debug == 1) Serial.println(key, DEC);
if (key <= 4) {
if (bitRead(LocoFN0to4[ActiveAddress], key) == 0 ) {
bitWrite(LocoFN0to4[ActiveAddress], key, 1);
}
else {
if (bitRead(LocoFN0to4[ActiveAddress], key) == 1 ) {
bitWrite(LocoFN0to4[ActiveAddress], key, 0);
}
}
doDCCfunction04();
Serial.print(LocoFN0to4[ActiveAddress], BIN);
if (debug == 1) Serial.println(" LocoFN0to4[ActiveAddress] d ");
Serial.print(LocoFN0to4[ActiveAddress], DEC);
if (debug == 1) Serial.println(" LocoFN0to4[ActiveAddress]");
}
if (key >= 5 && key <= 8) {
key = key - 5;
if (bitRead(LocoFN5to8[ActiveAddress], key) == 0 ) {
bitWrite(LocoFN5to8[ActiveAddress], key, 1);
}
else {
if (bitRead(LocoFN5to8[ActiveAddress], key) == 1 ) {
bitWrite(LocoFN5to8[ActiveAddress], key, 0);
}
}
doDCCfunction58();
Serial.print(LocoFN5to8[ActiveAddress], BIN);
if (debug == 1) Serial.println(" LocoFN5to8[ActiveAddress] d ");
Serial.print(LocoFN5to8[ActiveAddress], DEC);
if (debug == 1) Serial.println(" LocoFN5to8[ActiveAddress]");
}
if (key == -1)
{
lcd.setCursor (14, 1); // go to end of 2nd line
/// lcd.print("FN code ");
lcd.print(key, DEC);
key = 0;
LocoFN0to4[ActiveAddress] = B00000000; //clear variables for which functions are set
LocoFN5to8[ActiveAddress] = B00000000;
doDCCfunction04();
doDCCfunction58();
delay(500);
key = 0;
}
key = 0;
// delay(500);
doMainLCD();
}
//END DO FUNCTION BUTTONS
The rest will be posted later due to restrictions on my account?!.