Last active
January 1, 2021 19:00
-
-
Save wmontg5988/262b073c15a4d473bbb2e0e1dab09391 to your computer and use it in GitHub Desktop.
Voltage Reading and Load Center control from any Arduino MCU to the Stone10.1 HMI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Credit Electronic Clinic for inspiring this code. Basic code written by Electronic Clinic. Expanded and appended by me. | |
//This is a combination of 2 codes for use with the Stone 10.1 HMI | |
#include <SoftwareSerial.h> | |
SoftwareSerial max232(2,3); | |
#define Sensor1_H 0x00 | |
#define Sensor1_L 0x08 | |
#define Sensor2_H 0x00 | |
#define Sensor2_L 0x09 | |
int volt = A0; | |
int volt2 = A1; | |
unsigned char sensor1_send[8]= {0xA5, 0x5A, 0x05, 0x82, Sensor1_H, Sensor1_L, 0x00, 0x00}; | |
unsigned char sensor2_send[8]= {0xA5, 0x5A, 0x05, 0x82, Sensor2_H, Sensor2_L, 0x00, 0x00}; | |
char data; | |
String mystring; | |
int load1 = 13; | |
int load1f = 0; | |
int load2 = 12; | |
int load2f = 0; | |
int load3 = 11; | |
int load3f = 0; | |
int load4 = 10; | |
int load4f = 0; | |
void setup() | |
{ | |
Serial.begin(115200); | |
max232.begin(115200); | |
pinMode(volt,INPUT); | |
pinMode(volt2,INPUT); | |
pinMode(load1, OUTPUT); | |
pinMode(load2, OUTPUT); | |
pinMode(load3, OUTPUT); | |
pinMode(load4, OUTPUT); | |
digitalWrite(load1, LOW); | |
digitalWrite(load2, LOW); | |
digitalWrite(load3, LOW); | |
digitalWrite(load4, LOW); | |
} | |
void loop() | |
{ | |
if (max232.available()>0) | |
{ | |
data = max232.read(); | |
mystring = mystring + byte(data) ; | |
delay(10); | |
} | |
if (max232.available() == 0) | |
{ | |
//Serial.println(mystring); | |
int sensor1 = analogRead(volt); | |
int sensor2 = analogRead(volt2); | |
sensor1_send[6] = highByte(sensor1); | |
sensor1_send[7] = lowByte(sensor1); | |
max232.write(sensor1_send,8); | |
delay(100); | |
sensor2_send[6] = highByte(sensor2); | |
sensor2_send[7] = lowByte(sensor2); | |
max232.write(sensor2_send,8); | |
//Serial.println(sensor1); | |
delay(100); | |
load1control(); | |
load2control(); | |
load3control(); | |
load4control(); | |
delay(100); | |
mystring = ""; | |
} | |
} | |
void load1control() | |
{ | |
if (mystring.endsWith("101")&& (load1f == 0)) | |
{ | |
mystring = ""; | |
if(digitalRead(load1) == LOW) | |
{ | |
digitalWrite(load1,HIGH); | |
Serial.println("load1 High"); | |
} | |
load1f = 1; | |
} | |
if (mystring.endsWith("105") && (load1f == 1)) | |
{ | |
mystring = ""; | |
if(digitalRead(load1) == HIGH) | |
{ | |
digitalWrite(load1,LOW); | |
Serial.println("load1 LOW"); | |
} | |
load1f = 0; | |
} | |
} | |
void load2control() | |
{ | |
if (mystring.endsWith("102")&& (load2f == 0)) | |
{ | |
mystring = ""; | |
if(digitalRead(load2) == LOW) | |
{ | |
digitalWrite(load2,HIGH); | |
Serial.println("load2 High"); | |
} | |
load2f = 1; | |
} | |
if (mystring.endsWith("106") && (load2f == 1)) | |
{ | |
mystring = ""; | |
if(digitalRead(load2) == HIGH) | |
{ | |
digitalWrite(load2,LOW); | |
Serial.println("load2 LOW"); | |
} | |
load2f = 0; | |
} | |
} | |
void load3control() | |
{ | |
if (mystring.endsWith("103")&& (load3f == 0)) | |
{ | |
mystring = ""; | |
if(digitalRead(load3) == LOW) | |
{ | |
digitalWrite(load3,HIGH); | |
Serial.println("load3 High"); | |
} | |
load3f = 1; | |
} | |
if (mystring.endsWith("107") && (load3f == 1)) | |
{ | |
mystring = ""; | |
if(digitalRead(load3) == HIGH) | |
{ | |
digitalWrite(load3,LOW); | |
Serial.println("load3 LOW"); | |
} | |
load3f = 0; | |
} | |
} | |
void load4control() | |
{ | |
if (mystring.endsWith("104")&& (load4f == 0)) | |
{ | |
mystring = ""; | |
if(digitalRead(load4) == LOW) | |
{ | |
digitalWrite(load4,HIGH); | |
Serial.println("load4 High"); | |
} | |
load4f = 1; | |
} | |
if (mystring.endsWith("108") && (load4f == 1)) | |
{ | |
mystring = ""; | |
if(digitalRead(load4) == HIGH) | |
{ | |
digitalWrite(load4,LOW); | |
Serial.println("load4 LOW"); | |
} | |
load4f = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment