Arduino_Header ARDUINO "C" CODE Arduino_Header

 

// ARDUINO HOME AUTOMATION DRIVER Ver 7.0 - Murray Purdon - 29/03/2022 - www.murraypurdon.co.za

// This Driver is compatible with the Arduino Uno, Nano and Every
// Its function is to convert the Arduino to an Input/Output Device for the EmmaJane Home Automation Program built in MS VB2017

// Funtionality Description:

// Arduino transmits ">Q0<" to VB via the Serial Monitor - where 0 is the Station Address
// On receiving ">Q0<" from the Arduino, VB sends the ConfigData String
// This String configures the Arduino Digital Pins as either Inputs or Outputs
// This String takes the Format - >020112222022<
// The first Digit is the Station Address
// The remaining 11 Digits refer to the 11 Digital Pins of the Arduino starting from Pin2
// A 0 Configures the Arduino Digital Pin as a Normally Low Input
// A 1 Configures the Arduino Digital Pin as a Normally High Input
// A 2 Configures the Arduino Digital Pin as an Output
// The ConfigString is returned to VB as a confirmation of receipt

// The "to and fro" transmitting of the DataString now begins
// The Arduino accepts the DataString from VB is the specified parameters are met
// This String takes the Format - <0001010000000>
// The first Digit is the Station Address
// If a Digital Pin is configured as an Output, the Arduino sets the value of the Physical Output accoring to the value of the DataString Digit - 0 = Off - 1 = On
// If a Digital Pin is configured as an Input, the Arduino Reads the Physical Input and updates the Digit in the DataString accordingly - 0 = Off - 1 = On
// The DataString is Re-Built on every cycle and checked for any changes
// If a change is detected, the new DataString is sent to VB via the Serial Monitor

// If the Arduino receives a ">R<" String from VB then it resets itself and begins to request a new ConfigString by sending ">Q0<" to VB

// ********************************************************************************************************************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// STATION ADDRESS
int ArduinoAddress = 0; // Set Station Address here - Only Item that changes between Stations

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DIN PIN ASSIGNMENTS AND VALUES
int Din02 = 2; int Din03 = 3; int Din04 = 4; int Din05 = 5; int Din06 = 6; int Din07 = 7; // Physical Digital Inputs
int Din08 = 8; int Din09 = 9; int Din10 = 10; int Din11 = 11; int Din12 = 12; int Din13 = 13;
int Din02Value = 0; int Din03Value = 0; int Din04Value = 0; int Din05Value = 0; int Din06Value = 0; int Din07Value = 0; // Place Holders for States of Physical Inputs
int Din08Value = 0; int Din09Value = 0; int Din10Value = 0; int Din11Value = 0; int Din12Value = 0; int Din13Value = 0;
int ConfigSentMarker = 0; int NewConfigString =0; int DataStringIsActive = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DATA STRINGS
String SerialString = ""; // Actual Serial Port String received from VB - >020112222022< or <0001010000000> or >R< or >Q0<
String ConfigString = ""; // Configuration Information - eg : >020112222022<
String DataString = ""; // IO Data Between Arduino and VB - eg : <0001010000000>
String DataStringPrev = ""; // IO Data from previous cycle - to detect changes
int NewConfiguration = 0; // 1 = New Configuration sent
int DataStringReady = 0; // 1 = Ready to Operate once Config is in

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// RESET FUNCTION - Function to allow for Soft resetting of Arduino
void(* resetFunc) (void) = 0;

void setup() {
Serial.setTimeout(100); Serial.begin(9600);} // Set Serial Timeout and Initiate Serial Monitor

void loop() { // Begin Void Loop

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// READ FROM THE SERIAL MONITOR
if (Serial.available()>0){SerialString = Serial.readString(); // Read Serial Information from Serial Monitor if informtion is available
do { SerialString.remove (15, 1);} while (SerialString.length() > 15);} // Remove any additional characters from the Incoming String
if (SerialString[0] == '>' && (SerialString[1] - 48) == ArduinoAddress && SerialString[14] == '<' && SerialString.length() == 15) { // If Serial String matches ConfigString Format then Assign to Config String
ConfigString = SerialString; NewConfigString = 1; SerialString = "";}
if (SerialString[0] == '<' && (SerialString[1] - 48) == ArduinoAddress && SerialString[14] == '>' && SerialString.length() == 15) { // If Serial String matches DataString Format then Assign to DataString
DataString = SerialString; SerialString = "";}
if (SerialString[0] == '>' && SerialString[1] == 'R' && SerialString[2] == '<') { SerialString = ""; resetFunc();} // If Serial String matches Soft Reset then Run Soft Reset Function to Reset Arduino

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CONFIGURATION STRING PROCESSING
if (NewConfigString == 1) {Serial.println(ConfigString); NewConfigString = 0; // If New Configuration String is detected - Send Config String back to VB as Confirmation - Reset New Config Marker
if (ConfigString[2] == '0') {pinMode(Din02, INPUT); digitalWrite(Din02, LOW);} //If Config String Digit is 0 then that input is configured as a Normally Low Input
if (ConfigString[2] == '1') {pinMode(Din02, INPUT); digitalWrite(Din02, HIGH);} //If Config String Digit is 1 then that input is configured as a Normally High Input
if (ConfigString[2] == '2') {pinMode(Din02, OUTPUT);} //If Config String Digit is 2 then that input is configured as an Output
if (ConfigString[3] == '0') {pinMode(Din03, INPUT); digitalWrite(Din03, LOW);}
if (ConfigString[3] == '1') {pinMode(Din03, INPUT); digitalWrite(Din03, HIGH);}
if (ConfigString[3] == '2') {pinMode(Din03, OUTPUT);}
if (ConfigString[4] == '0') {pinMode(Din04, INPUT); digitalWrite(Din04, LOW);}
if (ConfigString[4] == '1') {pinMode(Din04, INPUT); digitalWrite(Din04, HIGH);}
if (ConfigString[4] == '2') {pinMode(Din04, OUTPUT);}
if (ConfigString[5] == '0') {pinMode(Din05, INPUT); digitalWrite(Din05, LOW);}
if (ConfigString[5] == '1') {pinMode(Din05, INPUT); digitalWrite(Din05, HIGH);}
if (ConfigString[5] == '2') {pinMode(Din05, OUTPUT);}
if (ConfigString[6] == '0') {pinMode(Din06, INPUT); digitalWrite(Din06, LOW);}
if (ConfigString[6] == '1') {pinMode(Din06, INPUT); digitalWrite(Din06, HIGH);}
if (ConfigString[6] == '2') {pinMode(Din06, OUTPUT);}
if (ConfigString[7] == '0') {pinMode(Din07, INPUT); digitalWrite(Din07, LOW);}
if (ConfigString[7] == '1') {pinMode(Din07, INPUT); digitalWrite(Din07, HIGH);}
if (ConfigString[7] == '2') {pinMode(Din07, OUTPUT);}
if (ConfigString[8] == '0') {pinMode(Din08, INPUT); digitalWrite(Din08, LOW);}
if (ConfigString[8] == '1') {pinMode(Din08, INPUT); digitalWrite(Din08, HIGH);}
if (ConfigString[8] == '2') {pinMode(Din08, OUTPUT);}
if (ConfigString[9] == '0') {pinMode(Din09, INPUT); digitalWrite(Din09, LOW);}
if (ConfigString[9] == '1') {pinMode(Din09, INPUT); digitalWrite(Din09, HIGH);}
if (ConfigString[9] == '2') {pinMode(Din09, OUTPUT);}
if (ConfigString[10] == '0') {pinMode(Din10, INPUT); digitalWrite(Din10, LOW);}
if (ConfigString[10] == '1') {pinMode(Din10, INPUT); digitalWrite(Din10, HIGH);}
if (ConfigString[10] == '2') {pinMode(Din10, OUTPUT);}
if (ConfigString[11] == '0') {pinMode(Din11, INPUT); digitalWrite(Din11, LOW);}
if (ConfigString[11] == '1') {pinMode(Din11, INPUT); digitalWrite(Din11, HIGH);}
if (ConfigString[11] == '2') {pinMode(Din11, OUTPUT);}
if (ConfigString[12] == '0') {pinMode(Din12, INPUT); digitalWrite(Din12, LOW);}
if (ConfigString[12] == '1') {pinMode(Din12, INPUT); digitalWrite(Din12, HIGH);}
if (ConfigString[12] == '2') {pinMode(Din12, OUTPUT);}
if (ConfigString[13] == '2') {pinMode(Din13, OUTPUT);}
DataString = '<'; DataString = DataString + ConfigString[1]; DataString = DataString + "000000000002>"; //On Setting up a new Config, the DataString is reset to Zeros - Din13 is always an Output - Therefore set as a 2
DataStringReady = 1;} // DataStringReady Marker is set to 1 to indicate that the Arduino is now ready to Run
else {if (ConfigString.length() != 15){ // If Config String is not valid...
ConfigString = ">Q"; ConfigString = ConfigString + ArduinoAddress; ConfigString = ConfigString + "<"; Serial.println(ConfigString); DataStringReady = 0; delay(1000);}} // Send >Q0< to VB to request a new Config - Mark DataStringReady as 0

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DATA STRING PROCESSING
if (DataStringReady == 1) { // If Arduino is Configured and ready to Run...
if (ConfigString[2] == '2') {Din02Value = (DataString[2] - 48); if (Din02Value == 0) {digitalWrite(Din02, LOW);} else {digitalWrite(Din02, HIGH);}} // If Din is configured as an Output, Set DinValue to the value in the DataString - 0 or 1 - Then set the Physical Output accordingly
else {Din02Value = digitalRead(Din02);} //_DIN02 // If Din is configured as an Input, Set DinValue to the value read from the Physical Input
if (ConfigString[3] == '2') {Din03Value = (DataString[3] - 48); if (Din03Value == 0) {digitalWrite(Din03, LOW);} else {digitalWrite(Din03, HIGH);}}
else {Din03Value = digitalRead(Din03);} //_DIN03
if (ConfigString[4] == '2') {Din04Value = (DataString[4] - 48); if (Din04Value == 0) {digitalWrite(Din04, LOW);} else {digitalWrite(Din04, HIGH);}}
else {Din04Value = digitalRead(Din04);} //_DIN04
if (ConfigString[5] == '2') {Din05Value = (DataString[5] - 48); if (Din05Value == 0) {digitalWrite(Din05, LOW);} else {digitalWrite(Din05, HIGH);}}
else {Din05Value = digitalRead(Din05);} //_DIN05
if (ConfigString[6] == '2') {Din06Value = (DataString[6] - 48); if (Din06Value == 0) {digitalWrite(Din06, LOW);} else {digitalWrite(Din06, HIGH);}}
else {Din06Value = digitalRead(Din06);} //_DIN06
if (ConfigString[7] == '2') {Din07Value = (DataString[7] - 48); if (Din07Value == 0) {digitalWrite(Din07, LOW);} else {digitalWrite(Din07, HIGH);}}
else {Din07Value = digitalRead(Din07);} //_DIN07
if (ConfigString[8] == '2') {Din08Value = (DataString[8] - 48); if (Din08Value == 0) {digitalWrite(Din08, LOW);} else {digitalWrite(Din08, HIGH);}}
else {Din08Value = digitalRead(Din08);} //_DIN08
if (ConfigString[9] == '2') {Din09Value = (DataString[9] - 48); if (Din09Value == 0) {digitalWrite(Din09, LOW);} else {digitalWrite(Din09, HIGH);}}
else {Din09Value = digitalRead(Din09);} //_DIN09
if (ConfigString[10] == '2') {Din10Value = (DataString[10] - 48); if (Din10Value == 0) {digitalWrite(Din10, LOW);} else {digitalWrite(Din10, HIGH);}}
else {Din10Value = digitalRead(Din10);} //_DIN10
if (ConfigString[11] == '2') {Din11Value = (DataString[11] - 48); if (Din11Value == 0) {digitalWrite(Din11, LOW);} else {digitalWrite(Din11, HIGH);}}
else {Din11Value = digitalRead(Din11);} //_DIN11
if (ConfigString[12] == '2') {Din12Value = (DataString[12] - 48); if (Din12Value == 0) {digitalWrite(Din12, LOW);} else {digitalWrite(Din12, HIGH);}}
else {Din12Value = digitalRead(Din12);} //_DIN12
if (ConfigString[13] == '2') {Din13Value = (DataString[13] - 48); if (Din13Value == 0) {digitalWrite(Din13, LOW);} else {digitalWrite(Din13, HIGH);}} //_DIN13 // Din13 is always set as an Output
DataString = "<";
DataString = DataString + ArduinoAddress + Din02Value + Din03Value + Din04Value + Din05Value + Din06Value + Din07Value + Din08Value + Din09Value + Din10Value + Din11Value + Din12Value + Din13Value;
DataString = DataString + ">"; // Re-Build the according to the new information formulated above

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TRANSMIT DATASTRING TO VB ON CHANGE
if (DataString != DataStringPrev) {Serial.println(DataString); DataStringPrev = DataString;}} // If DataString has changed from previous cycle, Send DataString to VB

delay(500);} // Cycle Delay od 500 mS

 

 

 

 

 

 

HomeButton