////////////////////////////////////////////////////////////////////////////////
// Name:         LCDtest                                                      //
// Platform:     Arduino Mega 2560 + 1Sheeld + Android phone                  //
// Created by:   HARB rboek2@gmail.com February 2018 GPL copyrights           //
// http://robotigs.nl/robotigs/includes/parts_header.php?idpart=269           //
// Created to test the magnetic waterswitch.                                  //
////////////////////////////////////////////////////////////////////////////////


// SET PRECOMPILER OPTIONS *****************************************************
// Initialse conditional compiling, uncomment to include, comment to exclude ---
// Do comment for runtime versions
#define RS232                   //Uncomment to include Serial Monitor sections
//  #ifdef RS232     //Only include these lines if the variable has been defined

// Define precompiler variables -----------------(Runs faster & doesn`t use RAM)
#define CUSTOM_SETTINGS                                     //Needed for 1Sheeld
#define INCLUDE_GAMEPAD_SHIELD                              //Needed for 1Sheeld

// Define PINS -----------------------------------------------------------------
#define ledRedPin  44          //3 Colour LED, to which PWM pin RED is connected
#define ledBluPin  45         //3 Colour LED, to which PWM pin BLUE is connected
#define ledGrePin  46        //3 Colour LED, to which PWM pin GREEN is connected
#define ledXmas     7              //White LED Christmas window chain, needs PWM

// Define the needed header files for the precompiler, no charge if not used ---
//#include <OneSheeld.h>                   //Include library provided by 1Sheeld
#include <LiquidCrystal.h>                         //Default in IDE, by Tom Igoe
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

//DEFINE VARIABLES -------------------------------------------------------------
bool ledOnBoardVal = LOW;     //You can chose HIGH-on or LOW-off for LED_BUILTIN
bool didWeCall = false;                          //1Sheeld Define a boolean flag
bool flagActive = false;     //Needed to be able to sent every command just once
bool flagSent   = false;            //Needed to sent halt just once after relief
char cmd1   = '0';                                    //Set halt command to send
unsigned int inByte = 0;                                        //Serial Monitor
unsigned int MaxPWM  = 32;                 //Christmas LEDs output max PWM value
unsigned int MilliSecs = 100;        //Christmas LEDs fader output counter timer
unsigned int BlinkTimer = 3;       //Christmas LEDs blinker output counter timer
unsigned int BlinkTimes = 6;   //Christmas LEDs blinker number of on on switches
unsigned int CntrTmp1  = 0;                       //To be used in any subroutine
unsigned int CntrTmp2  = 0;                       //To be used in any subroutine
byte cmdEngines = 'D';       //The startup colour and the command to the engines

#define anaVd1Pin    A8 //Define to which I/O pin the H=1/4 divider is connected
int     anaVd1Val =   0;                    //Contains last measurement (0-1023)

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs=24, rw=22, en=26, d0=30, d1=31, d2=32, d3=33, d4=34, d5=35, d6=36, d7=37;
LiquidCrystal lcd(rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7);
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

/*
 * LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7) 
  The circuit:
 * LCD RS Register select signal pin to digital pin 24 = Green
 * LCD Enable pin to digital pin 26 = Blue
 * 
 * LCD D0 pin to digital pin 30 = Orange
 * LCD D1 pin to digital pin 31 = Orange
 * LCD D2 pin to digital pin 32  = Orange
 * LCD D3 pin to digital pin 33 = Orange
 * LCD D4 pin to digital pin 37 = Orange
 * LCD D5 pin to digital pin 36 = Orange
 * LCD D6 pin to digital pin 35 = Orange
 * LCD D7 pin to digital pin 34 = Orange
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * 
 */
  
void setup() {
  disable_jtag();              //Disable jtag to free port C, enabled by default
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(ledRedPin, OUTPUT);        //Make the RED LED connection an output pin
  pinMode(ledBluPin, OUTPUT);       //Make the BLUE LED connection an output pin
  pinMode(ledXmas, OUTPUT);            //White LED Christmas window chain on PWM
  analogReference(DEFAULT); //Option can differ for other boards,voltage divider

  //OneSheeld.begin();
  
  test_LEDs();                //PWM fade in and fade out for all 4 LEDs on board
  #if defined(RS232)       //Only compiled in if Serial Monitor must be included
    Serial.begin(9600); //Nothing more needed for the Serial Monitor to function
    Serial.println("Hello world");  //Print a message to test the serial monitor
  #endif                                          //End of conditional compiling

  lcd.begin(16, 2);                //Set up the LCD's number of columns and rows
  lcd.print("hello, world!");                       //Print a message to the LCD
} //End of setup
//interrupts();


void loop() {                                //KEEP ON RUNNING THIS LOOP FOREVER
  anaVd1Val = analogRead(anaVd1Pin);                //Read voltage on pin anaPn1
  Serial.println(anaVd1Val);             //Print a message to the serial monitor  
  toggle_ledOnBoard();          //Toggles the LED_BUILTIN on-board LED on or off
  delay (100);                                         //Test your patience here
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898



void test_LEDs(void){ //PWM fade in and fade out for all 4 LEDs on board *******
  byte msWait = 2;                      //Test your patience during the LED test
  byte brillance = 0;           //Brightness of any color, just to test LEDs PWM
  
  while (brillance<255){
     analogWrite(LED_BUILTIN, brillance); //Set to desired PWM value LED_BUILTIN
     brillance++;
     delay (msWait);
  }
  while (brillance>0){
     analogWrite(LED_BUILTIN, brillance); //Set to desired PWM value LED_BUILTIN
     brillance--;
     delay (msWait);
  }
  analogWrite(LED_BUILTIN, 0);  //Set LED to desired PWM value = off LED_BUILTIN
  

  while (brillance<255){
     analogWrite(ledRedPin, brillance);       //Set LED to desired PWM value RED
     brillance++;
     delay (msWait);
  }
  while (brillance>0){
     analogWrite(ledRedPin, brillance);       //Set LED to desired PWM value RED
     brillance--;
     delay (msWait);
  }
  analogWrite(ledRedPin, 0);            //Set LED to desired PWM value = off RED

  
  while (brillance<255){
     analogWrite(ledGrePin, brillance);     //Set LED to desired PWM value GREEN
     brillance++;
     delay (msWait);
  }
  while (brillance>0){
     analogWrite(ledGrePin, brillance);     //Set LED to desired PWM value GREEN
     brillance--;
     delay (msWait);
  }
  analogWrite(ledGrePin, 0);          //Set LED to desired PWM value = off GREEN

  
  while (brillance<255){
     analogWrite(ledBluPin, brillance);      //Set LED to desired PWM value BLUE
     brillance++;
     delay (msWait);
  }
  while (brillance>0){
     analogWrite(ledBluPin, brillance);      //Set LED to desired PWM value BLUE
     brillance--;
     delay (msWait);
  }
  analogWrite(ledBluPin, 0);           //Set LED to desired PWM value = off BLUE
   
} //Exit test_LEDs -------------------------------------------------------------


void toggle_ledOnBoard(void){ //Toggles the LED_BUILTIN on-board LED on or off *
  ledOnBoardVal = !ledOnBoardVal;                                 //Toggle value
  digitalWrite(LED_BUILTIN, ledOnBoardVal);     //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------


void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------


////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO MEGA 2560                                    //
// Board  -Atmel- PIN - IDE - Function          - External Connection    FUNC //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
//? SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  -                         TWI //
//? SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                         TWI //
// AREF   -  31 - REF -     - AREF              -                         REF //
// GND    -  32 - GND -     - GND               -                         GND //
// 13 PWM -  26 - PB7 -  13 - OC0A/OC1C/PCINT17 - LED Arduino LED_BUILTIN PWM //
// 12 PWM -  18 - PB6 -  12 - OC1B/PCINT16      - Motormodule             PWM //
// 11 PWM -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - Motormodule             PWM //
// 10 PWM -  16 - PB2 -  10 - SS/OC1B/PCINT2    - Motormodule             PWM //
//  9 PWM -  15 - PB1 -   9 - OC1A/PCINT1       - Motormodule             PWM //
//  8 PWM -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                         DIO //
//                                                                            //
// CONNECTIONS RAILS TOP MIDDLE: DIGITAL PWM<~> ***************************** //
//  7 PWM -  13 - PD7 -   7 - PCINT23/AIN1      - Christmas LED chain     PWM //
//  6 PWM -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                         PWM //
//  5 PWM -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                         PWM //
//  4 PWM -   6 - PD4 -   4 - PCINT20/XCK/T0    -                         PWM //
//  3 PWM -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                         PWM //
//  2 PWM -   4 - PD2 -   2 - PCINT18/INT0      -                         INT //
//  1 TX0 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor          TX0 //
//  0 RX0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor          RC0 //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 14 TX3 -  13 - PD7 -   7 - PCINT23/AIN1      -                         DIO //
// 15 RX3 -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                         PWM //
// 16 TX2 -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                         TX2 //
// 17 RX2 -   6 - PD4 -   4 - PCINT20/XCK/T0    -                         RX2 //
// 18 TX1 -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                         INT //
// 19 RX1 -   4 - PD2 -   2 - PCINT18/INT0      -                         INT //
// 20 SDA -   3 - PD1 -   1 - PCINT17/TXD       -                         SDA //
// 21 SCL -   2 - PD0 -   0 - PCINT16/RCD       -                         SCL //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -     - VCC               -                          VCC //
// RES   -   1 - RES -     - PCINT14/RESET     -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM CENTER: ANALOG IN ******************************* //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A11   -  89 - PK0 -     - ADC1 4/PCINT?     -                          ADC //
// A12 = 88 = PK1 ADC15/PCINT?     =                                      ADC //
// A13 = 87 = PK2 ADC14/PCINT?     =                                      ADC //
// A15 = 86 = PK3 ADC15/PCINT?     =                                      ADC //
// A12 = 85 = PK4 ADC14/PCINT?     =                                      ADC //
// A13 = 84 = PK5 ADC15/PCINT?     =                                      ADC //
// A14 = 83 = PK6 ADC14/PCINT22    = Battery monitor H=1/2 6Vdc pack      ADC //
// A15 = 82 = PK7 ADC15/PCINT23    = Battery monitor H=1/3 9Vdc battery   ADC //
//                                                                            //

// 20  = 44 = PD1 SDA/INT1         =                                      TWI //
// 21  = 43 = PD0 SCL/INT0         =                                      TWI //


  
// CONNECTIONS RAILS QUER RIGHT ********************************************* //
// Board  -Atmel- PIN - IDE - Function          - External Connection    FUNC //
// 32    -   58 - PC5 - A13 - DIO               - LCD RS, green           DIO //

// 44  = 40 = PL5 OC5C             = 3 Color led Red                      PWM //
// 45  = 39 = PL4 OC5B             = 3 Color led Blue                     PWM //
// 46  = 38 = PL3 OC5A             = 3 Color led Green                    PWM //
// 50  = 22 = PB3 MISO/PCINT3      =                                      SPI //
// 51  = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// 52  = 20 = PB1 SCK/PCINT1       =                                      SPI //
// 53  = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898
////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////

/*
LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

*/