CDS1 Display Switch (Relais Schalten) Aurduino Mega

Code Teil 1

//=========================================================================//
// Arduino_RS232_UseCaseRotationMenu.ino                             c(1)  //
//-------------------------------------------------------------------------//                                              //
// TARGET HW      : Arduino Mega 2560                                      //
// DATE           : 11.04.2018                                             //
//=========================================================================//
//-------------------------------------------------------------------------//
// inclusion of header files                                         c(2)  //
//-------------------------------------------------------------------------//
// none                                                                    //
//-------------------------------------------------------------------------//
// declaration of ModuleScope macros                                 c(3)  //
//-------------------------------------------------------------------------//
// Enable Arduino Serial Monitor to use the debug printout
#define DEBUG

#ifdef DEBUG
 #define DEBUG_PRINT(x)         Serial.print (x)
 #define DEBUG_PRINTHEX(x)      Serial.print (x, HEX)
 #define DEBUG_PRINTLN(x)       Serial.println (x)
 #define DEBUG_PRINTLNHEX(x)    Serial.println (x, HEX)
#else
 #define DEBUG_PRINT(x)
 #define DEBUG_PRINTHEX(x)
 #define DEBUG_PRINTLN(x)
 #define DEBUG_PRINTLNHEX(x) 
#endif 

//-------------------------------------------------------------------------//
// declaration of ModuleScope data types                             c(4)  //
//-------------------------------------------------------------------------//
// none

//-------------------------------------------------------------------------//
// declaration of ModuleScope functions                              c(5)  //
//-------------------------------------------------------------------------//
// none

//-------------------------------------------------------------------------//
// definition of ProjectScope variables                              c(6)  //
//-------------------------------------------------------------------------//
unsigned long ledOffTime = 0;
unsigned long noInteractionTime = 0;
bool flagLed = false;
bool flagNoInteraction = false;
bool flagRestart = false;

//-------------------------------------------------------------------------//
// definition of ModuleScope variables                               c(7)  //
//-------------------------------------------------------------------------//
#define ID_TARGET               0x53
#define RS232_SPEED             115200
#define DELAY_TIME_TX_RX_MS     10
#define DELAY_TIME_TELEGRAM_MS  10

#define NO_TOUCH                0x00
#define SOFTKEY_CENTER_SHORT    0x01
#define SOFTKEY_CENTER_LONG     0x02
#define SOFTKEY_BOTTOM_SHORT    0x03
#define SOFTKEY_BOTTOM_LONG     0x04
#define SOFTKEY_LEFT_SHORT      0x05
#define SOFTKEY_LEFT_LONG       0x06
#define SOFTKEY_TOP_SHORT       0x07
#define SOFTKEY_TOP_LONG        0x08
#define SOFTKEY_RIGHT_SHORT     0x09
#define SOFTKEY_RIGHT_LONG      0x0A
#define SCROLL_INTERRUPT        0x0B
#define COMMUNICATION_ERROR     0xAA

// Time [ms] LED is active after touch input
#define LED_ON_TIME             2000

// Time [ms] Display shuts off after last touch input
#define NO_INTERACTION_TIME     10000

// CDS1 Interrupt: Pin 25 / PA2
int CDS1_INTERRUPT_PIN = 25;

// Relais mapping table
int RELAIS_13_PIN = 13;
int RELAIS_12_PIN = 12;
int RELAIS_11_PIN = 11;
int RELAIS_10_PIN = 10;
int RELAIS_9_PIN = 9;
int RELAIS_8_PIN = 8;
int RELAIS_7_PIN = 7;
int RELAIS_6_PIN = 6;

// Relais Status Signals
byte relaisStatus_13 = LOW;
byte relaisStatus_12 = LOW;
byte relaisStatus_11 = LOW;
byte relaisStatus_10 = LOW;
byte relaisStatus_9 = LOW;
byte relaisStatus_8 = LOW;
byte relaisStatus_7 = LOW;
byte relaisStatus_6 = LOW;

// CRC table
byte crcTable[256];
//-------------------------------------------------------------------------//
// definition of ProjectScope functions                              c(8)  //
//-------------------------------------------------------------------------//
byte CalculateCRC8(byte [], byte);
byte AppendChecksum(byte [], byte);

//-------------------------------------------------------------------------//
// definition of ModuleScope functions                               c(9)  //
//-------------------------------------------------------------------------//
void setup() 
{
  // Configure serial window
  Serial.begin(115200);  
  // Configure RS232 to CDS1
  Serial1.begin(115200); 
  // Configure MA_INT# Signal
  pinMode(CDS1_INTERRUPT_PIN, INPUT);
  // Initialise CRC Table
  CRC(0x07);  
  // Print interface specification
  Serial.println("*** Configuration  ***");
  Serial.println("Serial Interface: RS232");
  Serial.print("Bus Speed: ");
  Serial.print(RS232_SPEED);
  Serial.println(" Hz");
  Serial.print("Time between TX and RX: ");
  Serial.print(DELAY_TIME_TX_RX_MS);
  Serial.println("ms");    
  Serial.print("Time between Telegrams: ");
  Serial.println("not applicable"); 
  Serial.println("**********************");
  // Initialise Relais status
  digitalWrite (RELAIS_13_PIN, relaisStatus_13);
  digitalWrite (RELAIS_12_PIN, relaisStatus_12);
  digitalWrite (RELAIS_11_PIN, relaisStatus_11);
  digitalWrite (RELAIS_10_PIN, relaisStatus_10);
  digitalWrite (RELAIS_9_PIN, relaisStatus_9);
  digitalWrite (RELAIS_8_PIN, relaisStatus_8);
  digitalWrite (RELAIS_7_PIN, relaisStatus_7);
  digitalWrite (RELAIS_6_PIN, relaisStatus_6);   

}

void loop() 
{
  DEBUG_PRINTLN("Check CDS1 Status:");
  // *** Check if communication to CDS1 is possible ***
  if(readId() != ID_TARGET)
  {
    do
    {
      delay(1000);
    }
    while(readId() != ID_TARGET);
  }
  DEBUG_PRINTLN("CDS1 is ready to work");
  delay(DELAY_TIME_TELEGRAM_MS);
  enableAllFeatures();
  delay(DELAY_TIME_TELEGRAM_MS);
  readTouchRegisters();
  delay(DELAY_TIME_TELEGRAM_MS);
  Serial.println("*** Start Rotation Menu Function ***");
  byte fwVersion = readFwVersion();
  Serial.print("FW Version CDS1: ");
  Serial.print((fwVersion & 0xF0) >> 4);
  Serial.print(".");
  Serial.println(fwVersion & 0x0F);
  delay(DELAY_TIME_TELEGRAM_MS);
  setLedBlue();
  delay(DELAY_TIME_TELEGRAM_MS);
  setLedNoPattern();
  delay(DELAY_TIME_TELEGRAM_MS);
  showSchurterLogo();
  delay(DELAY_TIME_TELEGRAM_MS);
  setSwitchModeSinglePicture();
  delay(1000);