#include "EasyNextionLibrary.h"
#include "neotimer.h"
EasyNex myNex(Serial1); // Create an object of EasyNex class with the name < myNex >
// Set as parameter the Hardware Serial you are going to use
// PHYSICAL PINOUTS
const int MCU1 = 10; //OK but needs relay block - Fan IO
const int MCU2 = 11; //OK - Light IO
const int MCU3 = 12; //OK - Socket IO
const int MCU4 = 13; //OK - UV
const int MCU5 = 25; //OK
const int MCU6 = 26; //OK
const int MCU7 = 24; //Clutch
const int MCU8 = 22; //OK but needs capacitor - Sash Up
const int MCU9 = 23; //OK but needs capacitor - Sash Down
const int DIN1 = 78; //This is FLOW-1
const int DIN2 = 79;
const int DIN3 = 2; //MOTS
const int DIN4 = 81;
const int DIN5 = 62; //SashHigh
const int DIN6 = 63; //SashLow
const int DIN7 = 64; //SashMid
const int DIN8 = A11; //ESTP
const int AIN1 = A0;
const int AIN2 = A1;
const int AIN3 = A2;
const int AIN4 = A3;
// SOFT VARIABLES
int autoSashEN = 1; // 0 - Autosash disabled :: 1 - Autosash enabled
volatile bool emrStop = 0; //This needs chaning to tie to DIN8
bool sashUpFlag = 0;
bool sashDownFlag = 0;
// TIMERS
Neotimer noMotion = Neotimer(3000); //Change this to HMI sash time
void setup() {
// put your setup code here, to run once:
myNex.begin(115200); // Begin the object with a baud rate of 9600
// If no parameter was given in the begin(), the default baud rate of 9600 will be used
pinMode(MCU1, OUTPUT);
pinMode(MCU2, OUTPUT);
pinMode(MCU3, OUTPUT);
pinMode(MCU4, OUTPUT);
pinMode(MCU5, OUTPUT);
pinMode(MCU6, OUTPUT);
pinMode(MCU7, OUTPUT);
pinMode(MCU8, OUTPUT);
pinMode(MCU9, OUTPUT);
pinMode(DIN1, INPUT);
pinMode(DIN2, INPUT);
pinMode(DIN3, INPUT);
pinMode(DIN4, INPUT);
pinMode(DIN5, INPUT);
pinMode(DIN6, INPUT);
pinMode(DIN7, INPUT);
pinMode(DIN8, INPUT);
pinMode(AIN1, INPUT);
pinMode(AIN2, INPUT);
pinMode(AIN3, INPUT);
pinMode(AIN4, INPUT);
digitalWrite(MCU8,LOW);
digitalWrite(MCU9, LOW);
// Needs a section to check for emergency stop alarms on startup and check sash position
noMotion.start();
attachInterrupt(digitalPinToInterrupt(DIN3), timerReset, RISING); // killSash also resets the timer Not sure we still need this.
attachInterrupt(digitalPinToInterrupt(DIN5), setSHFL, FALLING); //set Sash-High-Level-Flag-Low
attachInterrupt(digitalPinToInterrupt(DIN6), setSLFL, FALLING); //set Sash-Low-Level-Flag-Low
}
void loop() {
// put your main code here, to run repeatedly:
myNex.NextionListen(); // This function must be called repeatedly to response touch events
// from Nextion touch panel. Actually, you should place it in your loop function.
setFlags();
// if(DIN3 == HIGH){
// noMotion.reset();
// noMotion.restart();
// }
runSash();
}
void setFlags() {
if((digitalRead(DIN6) == HIGH) && (autoSashEN == 1) && (noMotion.done() == 1)) {
sashDownFlag = 1;
sashUpFlag = 0;
} //DIN6 is sash low :: 23 is MCU9 SashDown
else {
sashDownFlag = 0;
}
if((digitalRead(DIN5) == HIGH) && (autoSashEN == 1) && (noMotion.done() == 0)) {
sashUpFlag = 1;
sashDownFlag = 0;
}
else {
sashUpFlag = 0;
}
}
void timerReset() {
if(digitalRead(DIN3) == HIGH) {
noMotion.reset();
noMotion.start();
sashUpFlag = 1;
}
}
void runSash() {
if (sashDownFlag == 1) {
rSashDown();
}
else {
sSashDown();
}
if (sashUpFlag == 1) {
rSashUp();
}
else {
sSashUp();
}
}
void trigger0() { // This is called when the fan switch is toggled
digitalWrite(MCU1, !digitalRead(MCU1));
if (digitalRead(MCU1) == HIGH) {
//myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
//myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
digitalWrite(MCU1, HIGH);
} else if (digitalRead(MCU1) == LOW) {
//myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
//myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
digitalWrite(MCU1, LOW);
} // I have left this in as an example of how to run logic alongside and generate new screen elements from arduino.
}
void trigger1() { // This is called when the light switch is toggled
digitalWrite(MCU2, !digitalRead(MCU2));
if (digitalRead(MCU2) == HIGH) {
digitalWrite(MCU2, HIGH);
} else if (digitalRead(MCU2) == LOW) {
digitalWrite(MCU2, LOW);
}
}
void trigger2() { // This is called when the socket switch is toggled
digitalWrite(MCU3, !digitalRead(MCU3));
if (digitalRead(MCU3) == HIGH) {
digitalWrite(MCU3, HIGH);
} else if (digitalRead(MCU3) == LOW) {
digitalWrite(MCU3, LOW);
}
}
void trigger3() { // This is called when the auto-sash switch is toggled
// Needs logic writing to determine sash position using
// sash level switches and input from the motion sensor
if(autoSashEN==0){
autoSashEN = 1;
noMotion.start(); //Enables autosash and starts timer
}
else {
autoSashEN = 0;
digitalWrite(MCU8, LOW); //Stop the sash on the offchance it is still moving
digitalWrite(MCU9, LOW);
digitalWrite(MCU7, LOW);
noMotion.reset(); //Disables the autosash and resets the timer for next use
}
}
void trigger5() { // This is called when the sash up button is pressed
digitalWrite(MCU8, !digitalRead(MCU8));
if (digitalRead(MCU8) == HIGH) {
digitalWrite(MCU8, HIGH);
digitalWrite(MCU7, HIGH);
} else if (digitalRead(MCU8) == LOW) {
digitalWrite(MCU8, LOW);
digitalWrite(MCU7, LOW);
}
}
void trigger6() { // This is called when the sash down button is pressed
digitalWrite(MCU9, !digitalRead(MCU9));
if (digitalRead(MCU9) == HIGH) {
digitalWrite(MCU9, HIGH);
digitalWrite(MCU7, HIGH);
} else if (digitalRead(MCU9) == LOW) {
digitalWrite(MCU9, LOW);
digitalWrite(MCU7, LOW);
}
}
void killSash() { //Use this to stop sash motion any time.
digitalWrite(MCU8, LOW);
digitalWrite(MCU9, LOW);
digitalWrite(MCU7, LOW);
noMotion.reset();
}
void rSashUp() {
digitalWrite(MCU9, LOW); //Just in case it has been running
digitalWrite(MCU7, HIGH);
digitalWrite(MCU8, HIGH);
}
void rSashDown() {
digitalWrite(MCU8, LOW); //Just in case it has been running
digitalWrite(MCU7, HIGH);
digitalWrite(MCU9, HIGH);
}
void setSHFL() {
sashUpFlag = 0; //High level switch has been triggered set sashup flag to 0 to stop movement
killSash();
}
void setSLFL() {
sashDownFlag = 0; //Low level switch has been triggered set sashdown flag to 0 to stop movement
killSash();
}
void sSashUp() {
digitalWrite(MCU7, LOW);
digitalWrite(MCU8, LOW);
}
void sSashDown() {
digitalWrite(MCU7, LOW);
digitalWrite(MCU9, LOW);
}