Industrial programming guidance

Hello Arduino Forum.
So I am looking for some programming guidance.
I have an robodyn Mega 2560 w/ Ethernet.
I am communicating with several Acromags.

4 Acromags are for thermocouples (six thermocouples per).
1 Acromag has analog Input(4 pressure transducers)
1 Acromag has analog output(controlling VFDs)
1 Acromag has digital output.(Various Digital Inputs)

I am communicating with the Acromags using the Arduino MODBUS library
building off the TCP/IP example.

My main concern is: what is the best way to monitor the 24 thermocoples and
4 pressure transducers in the program.
Im not sure what is the best way in regards to conserving memory.
Should i have an array?
Should I have an array of pointers?

I need to monitor these values by pinging them through MODBUS and alarm if out of range.
What would be the most efficient way of doing this.

Help Me Arduino Forum.
Your our only hope.

Do you have a sketch working (or not) that simply pings all your sensors and prints their current readings?

Please post that here after you “copy for forum” from the IDE or use the </> button in the message composition window.

Yes, arrays. You may not need an array to store the values you read, but arrays could be used to store your minim and maxmum values allowed before alarms get raised up.

a7

Why do you believe that you have to conserve memory?

Make C++ classes for the various sensor and control types – thermocouples, transducers, digital I/O, etc.

Include in the classes information needed to communicate with each type … bus address, channel number, etc. And create class functions for communicating with each type.

Then make arrays of objects instantiated from the classes.

You could perhaps even derive all classes from the same base class and then access them polymorphically. That's how Adafruit's sensor library works.

Hi Yall.

@alto777
Heres some test code just pinging the Acromags. Its choppy but a start

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(128, 1, 1, 110); //needs to be a similar mask b/c will not connect
//IPAddress ip(192 , 168, 1, 110);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress server(128,1,1,100); // update with the IP Address of your Modbus server
//IPAddress server(192,168,1,100);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  
  pinMode(7, OUTPUT );
  digitalWrite(7, HIGH);//reseting ethernet wiznet5500
  delay(3000);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.********************");
  }

}

void loop() {
  if(!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Attempting to connect to Modbus TCP server");
    if (!modbusTCPClient.begin(server, 502)) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
      Serial.println("Modbus TCP Client connected");
    }
  } 
  
  else {

   
    /*
    //Acromag 963EN-4012 Analog input
    //Notes: Need to mess with the scaling
      Serial.println(modbusTCPClient.holdingRegisterRead(3016));//ref:43016 read Channel 0
      Serial.println(modbusTCPClient.holdingRegisterRead(3017));//ref:43017 read Channel 1
      Serial.println(modbucTCPClient.holdingRegister(3028));//ref:43028 read Channel 11
      */
 /*-------------Needs Attention----------------------
    //Acromag 973EN-4006 Analog Outputs six channel
    modbusTCPClient.holdingRegisterWrite(6,0x0000);//WD timeout
    modbusTCPClient.holdingRegisterWrite(11,10000);// write slave id 2, channel 0, 5volts
    Serial.println(modbusTCPClient.lastError());
    delay(5000);
    modbusTCPClient.holdingRegisterWrite(11,0);//write slave id 2, channel 0, 0volts
    Serial.println(modbusTCPClient.lastError());
    delay(5000);
    
   // -------------needs Attention-------------------------
*/
  /*
    //Acromag 989EN-4016 DIO with event counters
    //Note - Have to do binary addition to for 16 bits ie 0000001 is CH0 on
    modbusTCPClient.holdingRegisterWrite(39,0x01); //ch0 on
    //modbusTCPClient.holdingRegisterWrite(39,0x02); //ch2 on
    Serial.println(modbusTCPClient.lastError());
    delay(3000);
    modbusTCPClient.holdingRegisterWrite(39,0x00); //ch0 off
    Serial.println(modbusTCPClient.lastError());
    delay(3000);
    */

  /*
    //Acromag965EN-4006 six T/C or mv Inputs 1V peak
      Serial.println(modbusTCPClient.holdingRegisterRead(3012));//T/C analog input 0 Room Temp ~270 units
      Serial.println(modbusTCPClient.lastError());//for troubleshooting
      Serial.println(modbusTCPClient.holdingRegisterRead(3013));//
     // Serial.println(modbucTCPClient.holdingRegisterRead(3028));//
  */

   //Acromag952EN-4012DIO and AIO
      Serial.println(modbusTCPClient.holdingRegisterRead(103));//Read Analog Input A0
      modbusTCPClient.holdingRegisterWrite(100,20000);//reads ~14vdc so close(24/2=12) but does not turn off???
      modbusTCPClient.holdingRegisterWrite(102,63);//Digital Outputs On 11111
      Serial.println(modbusTCPClient.lastError());
      delay(3000);
      modbusTCPClient.holdingRegisterWrite(100,0);
      modbusTCPClient.holdingRegisterWrite(102,0);//All digitals outputs off 00000
      delay(3000);
    
  
 
  }
}

@CedarLakeInstruments.
I am worried about the stack becoming to full in the future because the project is going to add MQTT and GSM for remote monitoring.

@gfvalvo
Thanks for the insight. I will begin working on some classes.

It makes no difference, a byte takes up a byte however you specify it. If you are having to worry about shortage of memory at this stage then either your estimate is way off or you need a board with more memory.

If this is a commercial project consider whether it is appropriate to take advice on a forum for hobbyists and learners, what are the consequences if we give you bad advice and your project does not perform as it should as a result?

1 Like

Or if it's appropriate to use hobbyist-quality hardware? Or what level of experience should the programmer have?

Or if it fails catastrophically and hurts someone?

2 Likes

What is an 'Acromag'? I have never heard of one and, although I don't speak for anyone else, I suspect that most of the people here who might have useful advice don't know what one is either. More information means better advice.

What am I to do if it pays the bills and its what i like to do and its been in the design and Ive just been hired on. There are failsafes in place.

Draw a flowchart or any other diagram describing the desired functionality.
Write a software specification.
Let the customer confirm that you got everything right.

If you can provide all prerequisits some might be able if "arrays" or "pointers" are a good choice.

You could consider asking in Jobs and Paid Consultancy - Arduino Forum, getting an agreement with someone and paying them, then they will be contractually obliged to deliver whatever you agree.

If you want to do that please ask me to move this topic, please don't start another one.

Thanks.

Go ahead Perry

Moved as requested.

My suggestion would be to not waste time trying to optimize for available RAM and instead use something more suited to the purpose. Can you use a Raspberry Pi or some other small embedded computer for this? Especially if someone is paying you to do this, the $50 or so that you save in parts will vanish in the time you spend.

1 Like

Unfortunately we are stuck with a current design for this revision. I am going to push for significant changes on next revision.
However, if you are curious to see where Im at.

//this is for pcb revision3 with acromags
//Had to change some of the digital outputs for the new PCB
//

//need to figure out digital inputs for the
//green amber and estop buttons.
//As far as the BMM.ALARM and BMM.POF,
//These can be read on the OCI417.10

#include<SPI.h>
#include<Ethernet.h>
#include<ArduinoRS485.h> //ArduinoModbus depends on the ArduinoRS485 lib
#include<ArduinoModbus.h>
#include<ModbusMaster.h>
#include<EEPROM.h>
#include<pid.h>

//For OCI417 module
#define MAX485_DE 12
#define MAX485_RE_NEG 13

//ECU jibber jabber
#define ESTOP_BREAK 40
#define LED_PWR 22
#define TRACO_24VDC 23

//TIMING PARAMETERS
unsigned long LOOP_TIME;
unsigned long CURRENT_MILLIS=0;//used to keep track of time
unsigned long PREVIOUS_MILLIS=0;//used to keep track of time
unsigned long PREVIOUS_MILLIS_1=0;//used in case 8
unsigned long PREVIOUS_MILLIS_2=0;//used in case 7
unsigned long PREVIOUS_MILLIS_3=0;//used in readTCs function
unsigned long LOOP_MILLIS=0;//used to caluclate loop function time.
//timing constants
const long SUPERHEAT_TIMER=50000;
const int BMM_OFF_TIMER=30000;
const int BMM_START_TIMER=5000;
const int BMM_PURGE_TIMER=32000;
const long BMM_IGNITION_TIMER=30000;
const long BURNER_RAMP_TIMER=240000;//4mins
const long BURNER_REACH_815C=2700000;//SECONDS//45mins
const long STEAM_GENERATION_TIMER=1800000;//30 mins
const int STEAM_AT_170PSI_TIMER=10000;//10 seconds
const long OPEN_SR_FUEL_TIMER=180000;//3 mins

//superheat delta constant PARAMETERS
const int SUPERHEAT_DELTA_CONSTANT=0.3;// ~3degreesC per 10ms
uint16_t PREVIOUS_TT303=0;

//counter
uint8_t TC_CHECK_COUNTER=1;

//BLOWER VARIABLES
const int BLOWER_PURGE_SPEED=4, BLOWER_IGNITION_SPEED=3407,BLOWER_RAMP_END=7150;//2 to keep the dps triggered .17 * 20043counts
uint16_t BLOWER_SPEED_AT_170PSI,BLOWER_SPEED_FEEDBACK=0;//feedback in counts
int BLOWER_CALCULATE_SPEED;
bool BLOWER_ENABLE=false;

//RO PUMP VARIABLES
const int MAX_RO_PRESSURE=250;//psi
const int RO_PUMP_AT_10_GRAMS_PER_SEC=3000;//counts
uint8_t RO_PUMP_FEEDBACK=0;
int RO_CALCULATE_SPEED;
bool RO_PUMP_ENABLE=false;

//FLOW CONTROL VALVES
uint8_t FCV205_HX406_INLET_FLOW=0;//g/sec
uint16_t FCV134_BURNER_FUEL_FLOW_FB;//counts
const int FCV134_BURNER_FUEL_FLOW_IGNITION=10079,FCV134_BURNER_FUEL_FLOW_RAMP_END=15320;//5 volts ~40 percent open
uint8_t FCV141_SR_FEED_FLOW=0;//PERCENTAGE
uint8_t FT132_NG_FEED_FLOW=0; // UNSURE OF UNITS??
bool TWV308_STEAM_REFORMER_FEED=false;//NEED TO DETERMINE DIRECTIONS
bool TWV901_LOW_PRESSURE_REFORMATE=false;//NEED TO DETERMINE SIRECTIONS

uint8_t ATMOSPHERIC_PRESSURE=5;//psi

//INITIALIZE PRESSURE TRANSDUCER VARIABLES
uint16_t PT213_RO_PRESSURE,PT318_HX406_OUTPUT_PRESSURE;//psi
uint16_t PT420_STEAM_EJECTOR_PRESSURE,PT304_TWV308_INPUT_PRESSURE;//psi

//INITIALIZE THERMOCOUPLE VARIABLES
uint16_t TT142_SR_FUEL,TT301_HX406_STEAM_OUT,TT303_HX504_STEAM_OUT,TT306_EJECTOR_STEAM_IN;
uint16_t TT313_HX402_STEAM_OUT,TT319_HX402_STEAM_SYSTEM,TT407_STEAM_REFORMER_OUT_LREF;
uint16_t TT408_HTS_IN_LREF,TT410_HTS_OUT_LREF,TT411_FPZ_OUT_LREF,TT430_SMR_TUBES_INLET;
uint16_t TT511_SILICON_CARBIDE_OUT,TT512_SILICON_CARBIDE_OUT,TT513_HX504_IN,TT514_HX504_OUT;
uint16_t TT441_SMR_TUBE1_OUT,TT442_SMR_TUBE2_OUT,TT443_SMR_TUBE3_OUT,TT444_SMR_TUBE4_OUT;
uint16_t TT445_SMR_TUBE5_OUT,TT446_SMR_TUBE6_OUT,TT447_SMR_TUBE7_OUT,TT448_SMR_TUBE8_OUT;
uint16_t TT449_SMR_TUBE9_OUT;
uint16_t HOLD_BURNER_TEMP;

//OCI417 MODBUS PARAMETERS
const int OCI_INPUT_STATUS_REGISTER=25;
const int OCI_OUTPUT_STATUS_REGISTER=26;
const int OCI_UNIT_ID=1;
uint16_t OCI_INPUT_STATUS_WORD=0;
uint16_t OCI_OUTPUT_STATUS_WORD=0;
uint8_t OCI_RESULT;
bool COMBUSTION_PRESSURE_SWITCH,BMM_ALARM_STATUS;
bool BMM_PROOF_OF_FLAME,OCI_TO_BMM_COM;
bool DUN_PSH,DUN_PSL,DUN_ZSL;
ModbusMaster NODE;

//FLAGS
bool GRN_BTN_FLAG=false,AMB_BTN_FLAG=false,ESTOP_FLAG=false,SENSOR_INTEGRITY_CHECK=false;
bool WATCHDOG_TIMER_FLAG=false;
bool PSI_INIT_TIMER=false;

uint8_t FSM_STATE=0;

uint8_t ERROR=0;//ERROR 0 means no ERROR.

//Mac Address of controller
byte mac[] = {0xdE,0xAD,0xBE,0xEF,0xED};
//needs to be similar to Acromag because will not connect
IPAddress ip(128,1,1,110);

//PARAMETERS FOR ACROMAG SERVERS
EthernetClient ethClient1;
EthernetClient ethClient2;
EthernetClient ethClient3;
EthernetClient ethClient4;
EthernetClient ethClient5;
EthernetClient ethClient6;
EthernetClient ethClient7;

ModbusTCPClient modbusTCPClient1(ethClient1);
ModbusTCPClient modbusTCPClient2(ethClient2);
ModbusTCPClient modbusTCPClient3(ethClient3);
ModbusTCPClient modbusTCPClient4(ethClient4);
ModbusTCPClient modbusTCPClient5(ethClient5);
ModbusTCPClient modbusTCPClient6(ethClient6);
ModbusTCPClient modbusTCPClient7(ethClient7);

IPAddress serverA1(128,1,1,101); // IP of Acromag1 973EN-4006 analog output
IPAddress serverA2(128,1,1,102); // IP of Acromag2 963EN-4012 analog input
IPAddress serverA3(128,1,1,103); // IP of Acromag3 989EN-4016 digital output
IPAddress serverA4(128,1,1,104); // IP of Acromag4 965EN-4006 tc
IPAddress serverA5(128,1,1,105); // IP of Acromag5 965EN-4006 tc
IPAddress serverA6(128,1,1,106); // IP of Acromag6 965EN-4006 tc
IPAddress serverA7(128,1,1,107); // IP of Acromag7 965EN-4006 tc

//PIDs
//steam gen pid
PID SUPER_HEAT_TT301 = PID(0.1,1000,-1000,0.5,0.01,0.5);//dt,max,min,kp,kd,ki
PID SUPER_HEAT_TT303 = PID(0.1,1000,-1000,0.5,0.01,0.5);//dt,max,min,kp,kd,ki
PID SUPER_HEAT_TT306 = PID(0.1,1000,-1000,0.5,0.01,0.5);//dt,max,min,kp,kd,ki
PID Steam_Gen_PID = PID(0.1, 100, -100, 0.5, 0.01, 0.5);//dt,max,min,kp,kd,ki;//
PID OPEN_SR_FUEL_PID = PID(0.1, 500, -500, 0.5, 0.01, 0.5);//dt,max,min,kp,kd,ki;//
int8_t BLOWER_SPEED_OFFSET, RO_SPEED_OFFSET;
uint16_t BURNER_FUEL_CUT_OFFSET, BURNER_FUEL_CUT;//for pid in case 9 open sr fuel also case 10


void setup() {

  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  // Init in receive mode rs485
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600); // for dedbugging

  Serial1.begin(9600);//FOR OCI MODULE rs485
  NODE.begin(1, Serial1); //unit id1 on serial1
  NODE.preTransmission(preTransmission);
  NODE.postTransmission(postTransmission);

  delay(500);//wait for system to boot up

  pinMode(LED_PWR,OUTPUT);
  digitalWrite(LED_PWR,HIGH);
  pinMode(TRACO_24VDC,OUTPUT);
  digitalWrite(TRACO_24VDC,HIGH);
  pinMode(ESTOP_BREAK,OUTPUT);
  digitalWrite(ESTOP_BREAK,HIGH);

  //reseting wifichip
  pinMode(7,OUTPUT);
  digitalWrite(7,HIGH);
  delay(50);

  //start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  //CONNECT TO ACROMAGS
  while(!modbusTCPClient1.connected()){modbusTCPClient1.begin(serverA1,502);}
  while(!modbusTCPClient2.connected()){modbusTCPClient2.begin(serverA2,502);}
  while(!modbusTCPClient3.connected()){modbusTCPClient3.begin(serverA3,502);}
  while(!modbusTCPClient4.connected()){modbusTCPClient4.begin(serverA4,502);}
  while(!modbusTCPClient5.connected()){modbusTCPClient5.begin(serverA5,502);}
  while(!modbusTCPClient6.connected()){modbusTCPClient6.begin(serverA6,502);}
  while(!modbusTCPClient7.connected()){modbusTCPClient7.begin(serverA7,502);}

  readBtn();
  readOCI();
  integrityCheck();

  modbusTCPClient1.holdingRegisterWrite(0x12,0);//write channel 0 (BLWRSpeed) off

  //get that dang dynamic pressure switch off!!
  BLOWER_SPEED_FEEDBACK=modbusTCPClient2.holdingRegisterRead(3016)*(.0005);//10volts/20000counts
  while(BLOWER_SPEED_FEEDBACK>0){
    BLOWER_SPEED_FEEDBACK=modbusTCPClient2.holdingRegisterRead(3016)*(.0005);//10volts/20000counts
    delay(1);
    }

    CURRENT_MILLIS=millis();//So as to not divide by zero on first looptime.
    LOOP_MILLIS=millis();
}

void loop() {

  LOOP_TIME=LOOP_MILLIS-CURRENT_MILLIS;
  connect_IO_Expanders();
  superheatTest();
  CURRENT_MILLIS=millis();

  Serial.println(FSM_STATE);//debugging
  Serial.print("Error:");Serial.println(ERROR);//debugging

  readTCs();
  readBtn();
  readOCI();
  readPTs();

  if(!SENSOR_INTEGRITY_CHECK){integrityCheck();}
  if(DUN_PSL){readOCI();}
connect_IO_Expanders();
switch(FSM_STATE){

 case 0: //initialize
  Serial.println("In case 0");
  if(!SENSOR_INTEGRITY_CHECK){break;}
  modbusTCPClient3.coilWrite(4,255);//CH4 digital output xv909

 if(!COMBUSTION_PRESSURE_SWITCH){
  modbusTCPClient1.holdingRegisterWrite(0x12,(BLOWER_PURGE_SPEED*2000));//write channel 0 (BLWRSpeed)
  modbusTCPClient3.coilWrite(13,0);//BLWR_EN..The logic is opposite
  break;}

 if(!DUN_PSL){Serial.print("Break due to DUN_PSL Switch: ");Serial.println(DUN_PSL);break;}

 //check for RO water???
 modbusTCPClient1.holdingRegisterWrite(0x12,OFF);//write channel 0 (BLWRSpeed)
 modbusTCPClient3.coilWrite(13,255);//BLWR_EN..The logic is opposite
 FSM_STATE=1;break;

 case 1: //depressurise reformer
 Serial.println("In case 1");
  modbusTCPClient3.coilWrite(8,OFF);//TWV308

  if(PT420_STEAM_EJECTOR_PRESSURE >= ATMOSPHERIC_PRESSURE){

    PT420_STEAM_EJECTOR_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3022)*(.0125))-47;//ref:43016 read Channel 6 PT420
    // PT420_STEAM_EJECTOR_PRESSURE = modbusTCPClient2.holdingRegisterRead(3022);
    modbusTCPClient3.coilWrite(4,255);//CH4 digital output xv909

    break;
  }

  modbusTCPClient3.coilWrite(4,OFF);//CH4 digital output xv909

  //250 psi
  if(PT213_RO_PRESSURE<250){

   //just guessing on the fcv205 flow, as long as we stay beneath 250psi
   modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_PURGE_SPEED*2000);//write channel 0 (BLWRSpeed)
   modbusTCPClient1.holdingRegisterWrite(0x15,7000);//write channel 3 (FCV205)//made steam at 1000//last 3000//~about 35 percent according to Shawn
   modbusTCPClient1.holdingRegisterWrite(0x13,RO_PUMP_AT_10_GRAMS_PER_SEC);//write channel 1 (WP_Speed) made steam at 1*2000//last 3*2000
   modbusTCPClient3.coilWrite(14,OFF);//WP_EN..Control is opposite
   modbusTCPClient3.coilWrite(13,OFF);//BLWR_EN..Control is opposite
   FSM_STATE=2;
   PREVIOUS_MILLIS=millis();
   break;
  }

 case 2://Superheat Test
 Serial.println("case 2");
  //make sure we are closed//xv909
  modbusTCPClient3.coilWrite(4,OFF);//CH4 digital output xv909
  //monitor tc 407 & 408
  //need a timer

  if(CURRENT_MILLIS - PREVIOUS_MILLIS <= SUPERHEAT_TIMER){

    TT303_HX504_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3014)*(18.4/265);
    //guessing on 300 degrees centigrade
    if(TT303_HX504_STEAM_OUT >300){
      ERROR=7;FSM_STATE=0;break;
      }
    break;
   }
   FSM_STATE=3;
   PREVIOUS_MILLIS = millis();
   break;

  case 3://bmm off //30 secondsconnect_IO_Expanders();
  Serial.println("case 3");

  if(CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_OFF_TIMER){

    modbusTCPClient1.holdingRegisterWrite(0x12,OFF);//write channel 0 (BLWRSpeed)
    modbusTCPClient3.coilWrite(12,OFF);//BMM_CR2 bmm off
    break;
   }
  FSM_STATE=4;
  PREVIOUS_MILLIS = millis();
  modbusTCPClient3.coilWrite(12,255);//BMM_CR2 turn on
  break;

  case 4://bmm on //5 seconds
  Serial.println("Case 4");

  if(CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_START_TIMER){
   break;
   }
  PREVIOUS_MILLIS = millis();
  FSM_STATE=5;
  break;

  case 5://bmm purge
  Serial.println("Case 5");

  if(CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_PURGE_TIMER){

   modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_PURGE_SPEED*2000);//write channel 0 (BLWRSpeed)
   break;
   }
  PREVIOUS_MILLIS = millis();
  FSM_STATE=6;
  break;

  case 6://BMM ignition
  Serial.println("Case 6");


  modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_IGNITION_SPEED);//write channel 0 (BLWRSpeed)
  modbusTCPClient1.holdingRegisterWrite(0x14,FCV134_BURNER_FUEL_FLOW_IGNITION);//write channel 2 (FCV134)

  if(CURRENT_MILLIS - PREVIOUS_MILLIS >= BMM_IGNITION_TIMER){

  if(!BMM_PROOF_OF_FLAME){ERROR=8;FSM_STATE=0;break;}
  else{FSM_STATE=7;PREVIOUS_MILLIS=millis();PREVIOUS_MILLIS_2=millis();break;}

  }
  break;

  case 7://start burner ramp//15 minutes//900 seconds
  Serial.println("Case 7");
  //burner ramp is 4mins
  //timing to reach 800c is 15 mins

  TT511_SILICON_CARBIDE_OUT=(modbusTCPClient5.holdingRegisterRead(3017))*(0.07);
  TT513_HX504_IN=(modbusTCPClient6.holdingRegisterRead(3013))*(0.07);

  Serial.print("TT511: ");Serial.println(TT511_SILICON_CARBIDE_OUT);
  Serial.print("TT513: ");Serial.println(TT513_HX504_IN);
  Serial.print("Burner Ramp Timer 15 mins: ");Serial.println(CURRENT_MILLIS - PREVIOUS_MILLIS);

  if(CURRENT_MILLIS - PREVIOUS_MILLIS >= BURNER_REACH_815C){

   if(TT511_SILICON_CARBIDE_OUT >= 815){
    FSM_STATE=8;
    PREVIOUS_MILLIS = millis();
    break;
    }

   if( TT513_HX504_IN >= 815){
    FSM_STATE=8;
    PREVIOUS_MILLIS = millis();
    break;
    }

   ERROR=11;FSM_STATE=0;break;
   }

   if(TT511_SILICON_CARBIDE_OUT >= 815){
    FSM_STATE=8;
    PREVIOUS_MILLIS = millis();
    break;
    }

   if( TT513_HX504_IN >= 815){
    FSM_STATE=8;
    PREVIOUS_MILLIS = millis();
    break;
    }

   BLOWER_SPEED_FEEDBACK = modbusTCPClient2.holdingRegisterRead(3016);//10volts/20000counts //~3400
   FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient2.holdingRegisterRead(3026);//ref:43028 read Channel 10 FCV134//~10079

   Serial.print("Blower FB: ");Serial.println(BLOWER_SPEED_FEEDBACK);
   Serial.print("FCV134 FB: ");Serial.println(FCV134_BURNER_FUEL_FLOW_FB);

   if(CURRENT_MILLIS - PREVIOUS_MILLIS <= BURNER_RAMP_TIMER){

    if(CURRENT_MILLIS - PREVIOUS_MILLIS_2 >= 5000){

    //if(BLOWER_SPEED_FEEDBACK<=14030){
    //modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_SPEED_FEEDBACK+220);}//write channel 0 blwr speed//44counts/sec
    //if(FCV134_BURNER_FUEL_FLOW_FB<=16121){
    //modbusTCPClient1.holdingRegisterWrite(0x14,FCV134_BURNER_FUEL_FLOW_FB+125);}//25counts/sec

     modbusTCPClient1.holdingRegisterWrite(0x14,FCV134_BURNER_FUEL_FLOW_RAMP_END);//16121 best thus far, fuel
     modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_RAMP_END);//7100 best thus far, blwr
     PREVIOUS_MILLIS_2 = millis();
     break;
     }
   }
   else{
  //modbusTCPClient1.holdingRegisterWrite(0x14,16121);//25counts/sec
  //modbusTCPClient1.holdingRegisterWrite(0x12,13000);//write channel 0 blwr speed//44counts/sec
    break;
    }
   break;

  case 8://Steam generation
  Serial.println("Case 8");

  //steam gen timeout 30 mins
  if(CURRENT_MILLIS - PREVIOUS_MILLIS >= STEAM_GENERATION_TIMER){ERROR=9;FSM_STATE=0;break;}

  Serial.print("Elapsed Time(time 1800ms): ");Serial.println(CURRENT_MILLIS - PREVIOUS_MILLIS);
  //PID control blower to acheive 170PSI pt304
  BLOWER_SPEED_FEEDBACK=modbusTCPClient2.holdingRegisterRead(3016);//10volts/20000counts

  BLOWER_SPEED_OFFSET = Steam_Gen_PID.calculate(170 , PT304_TWV308_INPUT_PRESSURE);
  BLOWER_CALCULATE_SPEED = BLOWER_SPEED_FEEDBACK + BLOWER_SPEED_OFFSET;
  Serial.print("Bower Calculate Speed: ");Serial.println(BLOWER_CALCULATE_SPEED);

  if(BLOWER_SPEED_FEEDBACK<12000){modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_CALCULATE_SPEED);}//write channel 0 (BLWRSpeed)
  else{modbusTCPClient1.holdingRegisterWrite(0x12,(BLOWER_CALCULATE_SPEED-1000));}
  if(PT304_TWV308_INPUT_PRESSURE <= 180 && PT304_TWV308_INPUT_PRESSURE >= 160){
  if(PSI_INIT_TIMER == false){ PREVIOUS_MILLIS_1 = millis(); PSI_INIT_TIMER = true;break; }
  if(CURRENT_MILLIS - PREVIOUS_MILLIS_1 >= STEAM_AT_170PSI_TIMER){
   BLOWER_SPEED_AT_170PSI=BLOWER_SPEED_FEEDBACK;
   modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_SPEED_AT_170PSI);
   modbusTCPClient3.coilWrite(5,255);//XV501 ON
   FSM_STATE=9; PREVIOUS_MILLIS = millis(); PSI_INIT_TIMER=false; HOLD_BURNER_TEMP=TT513_HX504_IN; break;
  }
 }
 else{PSI_INIT_TIMER=false;modbusTCPClient3.coilWrite(5,0);}//If not in bounds of 170-psi, reset timer.
 break;

 case 9://OPEN SR FUEL
 Serial.println("Case 9:");

 modbusTCPClient3.coilWrite(9,255);//TWV308 ON//channeling right when in front of refromer r050
 modbusTCPClient1.holdingRegisterWrite(0x15,10000);//write channel 3 (FCV205)//50 percent

 FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient2.holdingRegisterRead(3026);//ref:43028 read Channel 10 FCV134

 if(CURRENT_MILLIS-PREVIOUS_MILLIS<=180000){

 BURNER_FUEL_CUT_OFFSET = OPEN_SR_FUEL_PID.calculate(HOLD_BURNER_TEMP , TT513_HX504_IN);
 BURNER_FUEL_CUT= FCV134_BURNER_FUEL_FLOW_FB + BURNER_FUEL_CUT_OFFSET;
 modbusTCPClient1.holdingRegisterWrite(0x14, BURNER_FUEL_CUT);//write channel 2 (FCV134)

//reset timing if temp falls out of threshold
 if((HOLD_BURNER_TEMP>(TT513_HX504_IN+5)) || (HOLD_BURNER_TEMP<(TT513_HX504_IN-5))){PREVIOUS_MILLIS=millis();break;}

 }

 else{
   if(ERROR==0){FSM_STATE=10;PREVIOUS_MILLIS=millis();break;}
   FSM_STATE=9; PREVIOUS_MILLIS=millis();break;
 }

 break;

 case 10://idle hold mode blower/burner pid
 Serial.println("Case 10:");
 BLOWER_SPEED_FEEDBACK = modbusTCPClient2.holdingRegisterRead(3016);//10volts/20000counts
 FCV134_BURNER_FUEL_FLOW_FB = (modbusTCPClient2.holdingRegisterRead(3026)*(.005))-20;//ref:43028 read Channel 10 FCV134

 //3 mins idle
 if(CURRENT_MILLIS-PREVIOUS_MILLIS<=180000){

//burner fuel PID
 BURNER_FUEL_CUT_OFFSET = OPEN_SR_FUEL_PID.calculate(HOLD_BURNER_TEMP , TT513_HX504_IN);
 BURNER_FUEL_CUT= FCV134_BURNER_FUEL_FLOW_FB + BURNER_FUEL_CUT_OFFSET;
 modbusTCPClient1.holdingRegisterWrite(0x14, BURNER_FUEL_CUT);//write channel 2 (FCV134)

 //blower pid
 BLOWER_SPEED_OFFSET = Steam_Gen_PID.calculate(170 , PT304_TWV308_INPUT_PRESSURE);
 BLOWER_CALCULATE_SPEED = BLOWER_SPEED_FEEDBACK + BLOWER_SPEED_OFFSET;
 modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_CALCULATE_SPEED);//write channel 0 (BLWRSpeed)

 //check if out of threshold
 if((HOLD_BURNER_TEMP>(TT513_HX504_IN+5)) || (HOLD_BURNER_TEMP<(TT513_HX504_IN-5))){PREVIOUS_MILLIS=millis();break;}
 if(PT304_TWV308_INPUT_PRESSURE <= 171 && PT304_TWV308_INPUT_PRESSURE >= 169){PREVIOUS_MILLIS=millis();break;}

 }
 else{FSM_STATE=11;break;}
 break;

 case 11://reformer stabilize
 Serial.println("Case 11:");


 default:
 Serial.println("Default.");
 break;

}//end switch

LOOP_MILLIS = millis();
}//end loop

void superheatTest(){

  int DELTA_TT303;

  //These dont seem to be changing enough
  PREVIOUS_TT303=TT303_HX504_STEAM_OUT;

  RO_PUMP_FEEDBACK = modbusTCPClient2.holdingRegisterRead(3017)*(.005);

  RO_SPEED_OFFSET = Steam_Gen_PID.calculate(170 , PT304_TWV308_INPUT_PRESSURE);
  BLOWER_CALCULATE_SPEED = BLOWER_SPEED_FEEDBACK + BLOWER_SPEED_OFFSET;

  if(!modbusTCPClient4.connected()){modbusTCPClient4.begin(serverA4,502);Serial.println("Connecting to A5 for superheat test.");}
   TT303_HX504_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3014)*(18.4/265);

  DELTA_TT303 = (TT303_HX504_STEAM_OUT - PREVIOUS_TT303)/(LOOP_TIME);


  if(DELTA_TT303 > SUPERHEAT_DELTA_CONSTANT){
    modbusTCPClient1.holdingRegisterWrite(0x13,10000);//write channel 1 (WP_Speed) full speed ahead sire
    modbusTCPClient3.coilWrite(14,OFF);//WP_EN..Control is opposite
  }

  else{
    modbusTCPClient1.holdingRegisterWrite(0x13,RO_PUMP_AT_10_GRAMS_PER_SEC);//write channel 1 (WP_Speed) full speed ahead sire
    modbusTCPClient3.coilWrite(14,OFF);//WP_EN..Control is opposite
  }

}

void connect_IO_Expanders(){
  //CONNECT TO ACROMAGS
  if(!modbusTCPClient1.connected()){modbusTCPClient1.begin(serverA1,502);}
  if(!modbusTCPClient2.connected()){modbusTCPClient2.begin(serverA2,502);}
  if(!modbusTCPClient3.connected()){modbusTCPClient3.begin(serverA3,502);}
  if(!modbusTCPClient4.connected()){modbusTCPClient4.begin(serverA4,502);}
  if(!modbusTCPClient5.connected()){modbusTCPClient5.begin(serverA5,502);}
  if(!modbusTCPClient6.connected()){modbusTCPClient6.begin(serverA6,502);}
  if(!modbusTCPClient7.connected()){modbusTCPClient7.begin(serverA7,502);}
}

void readPTs() {
  Serial.println("Reading PTs");
 if(!modbusTCPClient2.connected()){modbusTCPClient2.begin(serverA2,502);}
 //checking PTs
PT318_HX406_OUTPUT_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3020)*(.0125))-50;//ref:43017 read Channel 4 PressureTransducer_318.. 1psi/80counts maximux
PT213_RO_PRESSURE=(modbusTCPClient2.holdingRegisterRead(3021)*(.0125))-50;//ref:43028 read Channel 5 PT213
PT420_STEAM_EJECTOR_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3022)*(.0125))-47;//ref:43016 read Channel 6 PT420
PT304_TWV308_INPUT_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3023)*(.0125))-50;//ref:43017 read Channel 7 PT304

//if(PT213_RO_PRESSURE>20){modbusTCPClient1.holdingRegisterWrite(0x13,11000);}//write channel 1 (WP_Speed) made steam at 1*2000//last 3*2000}
//else{modbusTCPClient1.holdingRegisterWrite(0x13,RO_PUMP_AT_10_GRAMS_PER_SEC);}//write channel 1 (WP_Speed) made steam at 1*2000//last 3*2000}}


//raw data
//PT318_HX406_OUTPUT_PRESSURE = modbusTCPClient2.holdingRegisterRead(3020);
//PT213_RO_PRESSURE = modbusTCPClient2.holdingRegisterRead(3021);
//PT420_STEAM_EJECTOR_PRESSURE = modbusTCPClient2.holdingRegisterRead(3022);
//PT304_TWV308_INPUT_PRESSURE = modbusTCPClient2.holdingRegisterRead(3023);

Serial.print("PT318: ");Serial.println(PT318_HX406_OUTPUT_PRESSURE);
Serial.print("PT213: ");Serial.println(PT213_RO_PRESSURE);
Serial.print("PT420: ");Serial.println(PT420_STEAM_EJECTOR_PRESSURE);
Serial.print("PT304: ");Serial.println(PT304_TWV308_INPUT_PRESSURE);

if(PT318_HX406_OUTPUT_PRESSURE>=240){FSM_STATE=0;ERROR=3;Serial.print("PT318 >240PSI");Serial.println(PT318_HX406_OUTPUT_PRESSURE);}
else{ERROR=0;}
if( PT213_RO_PRESSURE>=240){FSM_STATE=0;ERROR=4;Serial.print("PT213 >240PSI");Serial.println(PT213_RO_PRESSURE);}
else{ERROR=0;}
if(PT420_STEAM_EJECTOR_PRESSURE>=240){FSM_STATE=0;ERROR=5;Serial.print("PT420 >240PSI");Serial.println(PT420_STEAM_EJECTOR_PRESSURE);}
else{ERROR=0;}
if(PT304_TWV308_INPUT_PRESSURE>=240){FSM_STATE=0;ERROR=6;Serial.print("PT305 >240PSI");Serial.println(PT304_TWV308_INPUT_PRESSURE);}
else{ERROR=0;}

}

void readTCs(){

 if(!modbusTCPClient4.connected()){modbusTCPClient4.begin(serverA4,502);}
 if(!modbusTCPClient5.connected()){modbusTCPClient5.begin(serverA5,502);}
 if(!modbusTCPClient6.connected()){modbusTCPClient6.begin(serverA6,502);}
 if(!modbusTCPClient7.connected()){modbusTCPClient7.begin(serverA7,502);}
 /*
 TT142_SR_FUEL=modbusTCPClient4.holdingRegisterRead(3012)*(18.4/265);
 TT301_HX406_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3013)*(18.4/265);
 TT303_HX504_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3014)*(18.4/265);
 TT306_EJECTOR_STEAM_IN=modbusTCPClient4.holdingRegisterRead(3015)*(18.4/265);
 TT313_HX402_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3016)*(18.4/265);
 TT319_HX402_STEAM_SYSTEM=modbusTCPClient4.holdingRegisterRead(3017)*(18.4/265);
 TT407_STEAM_REFORMER_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3012)*(18.4/265);
 TT408_HTS_IN_LREF=modbusTCPClient5.holdingRegisterRead(3013)*(18.4/265);
 TT410_HTS_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3014)*(18.4/265);
 TT411_FPZ_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3015)*(18.4/265);
 TT430_SMR_TUBES_INLET=modbusTCPClient5.holdingRegisterRead(3016)*(18.4/265);
 TT511_SILICON_CARBIDE_OUT=modbusTCPClient5.holdingRegisterRead(3017)*(18.4/265);
 TT512_SILICON_CARBIDE_OUT=modbusTCPClient6.holdingRegisterRead(3012)*(18.4/265);
 TT513_HX504_IN=modbusTCPClient6.holdingRegisterRead(3013)*(18.4/265);
 TT514_HX504_OUT=modbusTCPClient6.holdingRegisterRead(3014)*(18.4/265);
 TT441_SMR_TUBE1_OUT=modbusTCPClient6.holdingRegisterRead(3015)*(18.4/265);
 TT442_SMR_TUBE2_OUT=modbusTCPClient6.holdingRegisterRead(3016)*(18.4/265);
 TT443_SMR_TUBE3_OUT=modbusTCPClient6.holdingRegisterRead(3017)*(18.4/265);
 TT444_SMR_TUBE4_OUT=modbusTCPClient7.holdingRegisterRead(3012)*(18.4/265);
 TT445_SMR_TUBE5_OUT=modbusTCPClient7.holdingRegisterRead(3013)*(18.4/265);
 TT446_SMR_TUBE6_OUT=modbusTCPClient7.holdingRegisterRead(3014)*(18.4/265);
 TT447_SMR_TUBE7_OUT=modbusTCPClient7.holdingRegisterRead(3015)*(18.4/265);
 TT448_SMR_TUBE8_OUT=modbusTCPClient7.holdingRegisterRead(3016)*(18.4/265);
 TT449_SMR_TUBE9_OUT=modbusTCPClient7.holdingRegisterRead(3017)*(18.4/265);

 Serial.print("TT142_SR_FUEL: ");Serial.println(TT142_SR_FUEL);
 Serial.print("TT301_HX406_STEAM_OUT : ");Serial.println(TT301_HX406_STEAM_OUT);
 Serial.print("TT303_HX504_STEAM_OUT : ");Serial.println(TT303_HX504_STEAM_OUT);
 Serial.print("TT306_EJECTOR_STEAM_IN : ");Serial.println(TT306_EJECTOR_STEAM_IN);
 Serial.print("TT313_HX402_STEAM_OUT : ");Serial.println(TT313_HX402_STEAM_OUT);
 Serial.print("TT319_HX402_STEAM_SYSTEM : ");Serial.println(TT319_HX402_STEAM_SYSTEM);
 Serial.print("TT407_STEAM_REFORMER_OUT_LREF : ");Serial.println(TT407_STEAM_REFORMER_OUT_LREF);
 Serial.print("TT408_HTS_IN_LREF : ");Serial.println(TT408_HTS_IN_LREF);
 Serial.print("TT410_HTS_OUT_LREF : ");Serial.println(TT410_HTS_OUT_LREF);
 Serial.print("TT411_FPZ_OUT_LREF : ");Serial.println(TT411_FPZ_OUT_LREF);
 Serial.print("TT430_SMR_TUBES_INLET : ");Serial.println(TT430_SMR_TUBES_INLET);
 Serial.print("TT511_SILICON_CARBIDE_OUT : ");Serial.println(TT511_SILICON_CARBIDE_OUT);
 Serial.print("TT512_SILICON_CARBIDE_OUT : ");Serial.println(TT512_SILICON_CARBIDE_OUT);
 Serial.print("TT513_HX504_IN : ");Serial.println(TT513_HX504_IN);
 Serial.print("TT514_HX504_OUT : ");Serial.println(TT514_HX504_OUT);
 Serial.print("TT441_SMR_TUBE1_OUT : ");Serial.println(TT441_SMR_TUBE1_OUT);
 Serial.print("TT442_SMR_TUBE2_OUT : ");Serial.println(TT442_SMR_TUBE2_OUT);
 Serial.print("TT442_SMR_TUBE2_OUT : ");Serial.println(TT442_SMR_TUBE2_OUT);
 Serial.print("TT443_SMR_TUBE3_OUT : ");Serial.println(TT443_SMR_TUBE3_OUT);
 Serial.print("TT444_SMR_TUBE4_OUT : ");Serial.println(TT444_SMR_TUBE4_OUT);
 Serial.print("TT445_SMR_TUBE5_OUT : ");Serial.println(TT445_SMR_TUBE5_OUT);
 Serial.print("TT446_SMR_TUBE6_OUT : ");Serial.println(TT446_SMR_TUBE6_OUT);
 Serial.print("TT447_SMR_TUBE7_OUT : ");Serial.println(TT447_SMR_TUBE7_OUT);
 Serial.print("TT448_SMR_TUBE8_OUT : ");Serial.println(TT448_SMR_TUBE8_OUT);
 Serial.print("TT449_SMR_TUBE9_OUT : ");Serial.println(TT449_SMR_TUBE9_OUT);*/

//read acromag 4

switch(TC_CHECK_COUNTER){
case 1:
 if(CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000){

 TT142_SR_FUEL=modbusTCPClient4.holdingRegisterRead(3012)*(18.4/265);
 TT301_HX406_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3013)*(.07);
 TT303_HX504_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3014)*(18.4/265);
 TT306_EJECTOR_STEAM_IN=modbusTCPClient4.holdingRegisterRead(3015)*(18.4/265);
 TT313_HX402_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3016)*(18.4/265);
 TT319_HX402_STEAM_SYSTEM=modbusTCPClient4.holdingRegisterRead(3017)*(18.4/265);

 if(TT301_HX406_STEAM_OUT>1000){Serial.println("TT301_HX406_STEAM_OUT > 1000C");FSM_STATE=0;ERROR=13;}
 if(TT303_HX504_STEAM_OUT>1000){Serial.println("TT303_HX504_STEAM_OUT > 1000C");FSM_STATE=0;ERROR=14;}
 //if(TT306_EJECTOR_STEAM_IN>1000){Serial.println("TT306_EJECTOR_STEAM_IN > 1000C");FSM_STATE=0;ERROR=15;}
 if(TT313_HX402_STEAM_OUT>1000){Serial.println("TT313_HX402_STEAM_OUT > 1000C");FSM_STATE=0;ERROR=16;}
 if(TT319_HX402_STEAM_SYSTEM>1000){Serial.println("TT319_HX402_STEAM_SYSTEM > 1000C");FSM_STATE=0;ERROR=17;}

 Serial.print("TT142_SR_FUEL: ");Serial.println(TT142_SR_FUEL);
 Serial.print("TT301_HX406_STEAM_OUT : ");Serial.println(TT301_HX406_STEAM_OUT);
 Serial.print("TT303_HX504_STEAM_OUT : ");Serial.println(TT303_HX504_STEAM_OUT);
 Serial.print("TT306_EJECTOR_STEAM_IN : ");Serial.println(TT306_EJECTOR_STEAM_IN);
 Serial.print("TT313_HX402_STEAM_OUT : ");Serial.println(TT313_HX402_STEAM_OUT);
 Serial.print("TT319_HX402_STEAM_SYSTEM : ");Serial.println(TT319_HX402_STEAM_SYSTEM);

 PREVIOUS_MILLIS_3=millis();
 TC_CHECK_COUNTER=2;
 break;
 }

 case 2:
//read acromag5
 if(CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000){

 TT407_STEAM_REFORMER_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3012)*(18.4/265);
 TT408_HTS_IN_LREF=modbusTCPClient5.holdingRegisterRead(3013)*(18.4/265);
 TT410_HTS_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3014)*(18.4/265);
 TT411_FPZ_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3015)*(18.4/265);
 TT430_SMR_TUBES_INLET=modbusTCPClient5.holdingRegisterRead(3016)*(18.4/265);
 TT511_SILICON_CARBIDE_OUT=modbusTCPClient5.holdingRegisterRead(3017)*(18.4/265);

 if(TT407_STEAM_REFORMER_OUT_LREF>1000){Serial.println("TT407_STEAM_REFORMER_OUT_LREF > 1000C");FSM_STATE=0;ERROR=18;}
 if(TT408_HTS_IN_LREF>1000){Serial.println("TT408_HTS_IN_LREF > 1000C");FSM_STATE=0;ERROR=19;}
 //if(TT410_HTS_OUT_LREF>1000){Serial.println("TT410_HTS_OUT_LREF > 1000C");FSM_STATE=0;ERROR=20;}
 if(TT411_FPZ_OUT_LREF>1000){Serial.println("TT411_FPZ_OUT_LREF > 1000C");FSM_STATE=0;ERROR=21;} //current issue with reading
 //if(TT430_SMR_TUBES_INLET>1000){Serial.println("TT430_SMR_TUBES_INLET > 1000C");FSM_STATE=0;ERROR=22;} current issue with reading
 if(TT511_SILICON_CARBIDE_OUT>1000){Serial.println("TT511_SILICON_CARBIDE_OUT > 1000C");FSM_STATE=0;ERROR=23;}

 Serial.print("TT407_STEAM_REFORMER_OUT_LREF : ");Serial.println(TT407_STEAM_REFORMER_OUT_LREF);
 Serial.print("TT408_HTS_IN_LREF : ");Serial.println(TT408_HTS_IN_LREF);
 Serial.print("TT410_HTS_OUT_LREF : ");Serial.println(TT410_HTS_OUT_LREF);
 Serial.print("TT411_FPZ_OUT_LREF : ");Serial.println(TT411_FPZ_OUT_LREF);
 Serial.print("TT430_SMR_TUBES_INLET : ");Serial.println(TT430_SMR_TUBES_INLET);
 Serial.print("TT511_SILICON_CARBIDE_OUT : ");Serial.println(TT511_SILICON_CARBIDE_OUT);

 TC_CHECK_COUNTER=3;
 PREVIOUS_MILLIS_3=millis();
 break;
 }

 case 3:
 if(CURRENT_MILLIS - PREVIOUS_MILLIS_3>=3000){

 TT512_SILICON_CARBIDE_OUT=modbusTCPClient6.holdingRegisterRead(3012)*(18.4/265);
 TT513_HX504_IN=modbusTCPClient6.holdingRegisterRead(3013)*(18.4/265);
 TT514_HX504_OUT=modbusTCPClient6.holdingRegisterRead(3014)*(18.4/265);
 TT441_SMR_TUBE1_OUT=modbusTCPClient6.holdingRegisterRead(3015)*(18.4/265);
 TT442_SMR_TUBE2_OUT=modbusTCPClient6.holdingRegisterRead(3016)*(18.4/265);
 TT443_SMR_TUBE3_OUT=modbusTCPClient6.holdingRegisterRead(3017)*(18.4/265);

 if(TT512_SILICON_CARBIDE_OUT>1000){Serial.println("TT512_SILICON_CARBIDE_OUT > 1000C");FSM_STATE=0;ERROR=24;}
 if(TT513_HX504_IN>1000){Serial.println("TT513_HX504_IN > 1000C");FSM_STATE=0;ERROR=25;}
 if(TT514_HX504_OUT>1000){Serial.println("TT514_HX504_OUT > 1000C");FSM_STATE=0;ERROR=26;}
 if(TT441_SMR_TUBE1_OUT>700){Serial.println("TT441_SMR_TUBE1_OUT > 1000C");FSM_STATE=0;ERROR=27;}
 if(TT442_SMR_TUBE2_OUT>700){Serial.println("TT442_SMR_TUBE2_OUT > 1000C");FSM_STATE=0;ERROR=28;}
 if(TT443_SMR_TUBE3_OUT>700){Serial.println("TT443_SMR_TUBE3_OUT > 1000C");FSM_STATE=0;ERROR=29;}

 Serial.print("TT512_SILICON_CARBIDE_OUT : ");Serial.println(TT512_SILICON_CARBIDE_OUT);
 Serial.print("TT513_HX504_IN : ");Serial.println(TT513_HX504_IN);
 Serial.print("TT514_HX504_OUT : ");Serial.println(TT514_HX504_OUT);
 Serial.print("TT441_SMR_TUBE1_OUT : ");Serial.println(TT441_SMR_TUBE1_OUT);
 Serial.print("TT442_SMR_TUBE2_OUT : ");Serial.println(TT442_SMR_TUBE2_OUT);
 Serial.print("TT443_SMR_TUBE3_OUT : ");Serial.println(TT443_SMR_TUBE3_OUT);

 TC_CHECK_COUNTER=4;
 PREVIOUS_MILLIS_3=millis();
 break;
 }

 case 4:
 if(CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000){

 TT444_SMR_TUBE4_OUT=modbusTCPClient7.holdingRegisterRead(3012)*(18.4/265);
 TT445_SMR_TUBE5_OUT=modbusTCPClient7.holdingRegisterRead(3013)*(18.4/265);
 TT446_SMR_TUBE6_OUT=modbusTCPClient7.holdingRegisterRead(3014)*(18.4/265);
 TT447_SMR_TUBE7_OUT=modbusTCPClient7.holdingRegisterRead(3015)*(18.4/265);
 TT448_SMR_TUBE8_OUT=modbusTCPClient7.holdingRegisterRead(3016)*(18.4/265);
 TT449_SMR_TUBE9_OUT=modbusTCPClient7.holdingRegisterRead(3017)*(18.4/265);

 if(TT444_SMR_TUBE4_OUT>700){Serial.println("TT444_SMR_TUBE4_OUT > 700C");FSM_STATE=0;ERROR=30;}
 if(TT445_SMR_TUBE5_OUT>700){Serial.println("TT445_SMR_TUBE5_OUT > 700C");FSM_STATE=0;ERROR=31;}
 if(TT446_SMR_TUBE6_OUT>700){Serial.println("TT446_SMR_TUBE6_OUT > 700C");FSM_STATE=0;ERROR=32;}
 if(TT447_SMR_TUBE7_OUT>700){Serial.println("TT447_SMR_TUBE7_OUT > 700C");FSM_STATE=0;ERROR=33;}
 if(TT448_SMR_TUBE8_OUT>700){Serial.println("TT448_SMR_TUBE8_OUT > 700C");FSM_STATE=0;ERROR=34;}
 if(TT449_SMR_TUBE9_OUT>700){Serial.println("TT449_SMR_TUBE9_OUT > 700C");FSM_STATE=0;ERROR=35;}

 Serial.print("TT444_SMR_TUBE4_OUT : ");Serial.println(TT444_SMR_TUBE4_OUT);
 Serial.print("TT445_SMR_TUBE5_OUT : ");Serial.println(TT445_SMR_TUBE5_OUT);
 Serial.print("TT446_SMR_TUBE6_OUT : ");Serial.println(TT446_SMR_TUBE6_OUT);
 Serial.print("TT447_SMR_TUBE7_OUT : ");Serial.println(TT447_SMR_TUBE7_OUT);
 Serial.print("TT448_SMR_TUBE8_OUT : ");Serial.println(TT448_SMR_TUBE8_OUT);
 Serial.print("TT449_SMR_TUBE9_OUT : ");Serial.println(TT449_SMR_TUBE9_OUT);

 PREVIOUS_MILLIS_3=millis();
 TC_CHECK_COUNTER=1;
 break;

  }
 }
}

void integrityCheck(){
  Serial.println("Checking Signal Integrity.");
 if(!modbusTCPClient1.connected()){modbusTCPClient1.begin(serverA1,502);}
 if(!modbusTCPClient2.connected()){modbusTCPClient2.begin(serverA2,502);}
 if(!modbusTCPClient3.connected()){modbusTCPClient3.begin(serverA3,502);}
 if(!modbusTCPClient4.connected()){modbusTCPClient4.begin(serverA4,502);}
 if(!modbusTCPClient5.connected()){modbusTCPClient5.begin(serverA5,502);}
 if(!modbusTCPClient6.connected()){modbusTCPClient6.begin(serverA6,502);}
 if(!modbusTCPClient7.connected()){modbusTCPClient7.begin(serverA7,502);}

 PT318_HX406_OUTPUT_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3020)*(.0125))-50;//ref:43017 read Channel 4 PressureTransducer_318.. 1psi/80counts maximux
 PT213_RO_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3021)*(.0125))-50;//ref:43028 read Channel 5 PT213
 PT420_STEAM_EJECTOR_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3022)*(.0125))-47;//ref:43016 read Channel 6 PT420
 PT304_TWV308_INPUT_PRESSURE = (modbusTCPClient2.holdingRegisterRead(3023)*(.0125))-50;//ref:43017 read Channel 7 PT304
// if(PT318_HX406_OUTPUT_PRESSURE>=240){FSM_STATE=0;ERROR=3;}
// else{ERROR=0;}
// if(PT213_RO_PRESSURE>=240){FSM_STATE=0;ERROR=4;}
// else{ERROR=0;}
 if(PT420_STEAM_EJECTOR_PRESSURE>=240){FSM_STATE=0;ERROR=5;}
 else{ERROR=0;}
 if(PT304_TWV308_INPUT_PRESSURE>=240){FSM_STATE=0;ERROR=6;}
 else{ERROR=0;}

 TT142_SR_FUEL=modbusTCPClient4.holdingRegisterRead(3012)*(18.4/265);
 TT301_HX406_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3013)*(18.4/265);
 TT303_HX504_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3014)*(18.4/265);
 TT306_EJECTOR_STEAM_IN=modbusTCPClient4.holdingRegisterRead(3015)*(18.4/265);
 TT313_HX402_STEAM_OUT=modbusTCPClient4.holdingRegisterRead(3016)*(18.4/265);
 TT319_HX402_STEAM_SYSTEM=modbusTCPClient4.holdingRegisterRead(3017)*(18.4/265);
 TT407_STEAM_REFORMER_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3012)*(18.4/265);
 TT408_HTS_IN_LREF=modbusTCPClient5.holdingRegisterRead(3013)*(18.4/265);
 TT410_HTS_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3014)*(18.4/265);
 TT411_FPZ_OUT_LREF=modbusTCPClient5.holdingRegisterRead(3015)*(18.4/265);
 TT430_SMR_TUBES_INLET=modbusTCPClient5.holdingRegisterRead(3016)*(18.4/265);
 TT511_SILICON_CARBIDE_OUT=modbusTCPClient5.holdingRegisterRead(3017)*(18.4/265);
 TT512_SILICON_CARBIDE_OUT=modbusTCPClient6.holdingRegisterRead(3012)*(18.4/265);
 TT513_HX504_IN=modbusTCPClient6.holdingRegisterRead(3013)*(18.4/265);
 TT514_HX504_OUT=modbusTCPClient6.holdingRegisterRead(3014)*(18.4/265);
 TT441_SMR_TUBE1_OUT=modbusTCPClient6.holdingRegisterRead(3015)*(18.4/265);
 TT442_SMR_TUBE2_OUT=modbusTCPClient6.holdingRegisterRead(3016)*(18.4/265);
 TT443_SMR_TUBE3_OUT=modbusTCPClient6.holdingRegisterRead(3017)*(18.4/265);
 TT444_SMR_TUBE4_OUT=modbusTCPClient7.holdingRegisterRead(3012)*(18.4/265);
 TT445_SMR_TUBE5_OUT=modbusTCPClient7.holdingRegisterRead(3013)*(18.4/265);
 TT446_SMR_TUBE6_OUT=modbusTCPClient7.holdingRegisterRead(3014)*(18.4/265);
 TT447_SMR_TUBE7_OUT=modbusTCPClient7.holdingRegisterRead(3015)*(18.4/265);
 TT448_SMR_TUBE8_OUT=modbusTCPClient7.holdingRegisterRead(3016)*(18.4/265);
 TT449_SMR_TUBE9_OUT=modbusTCPClient7.holdingRegisterRead(3017)*(18.4/265);

  //analog outputs
 modbusTCPClient1.holdingRegisterWrite(0x12,OFF);//write channel 0 (BLWRSpeed) to 10 volts
 modbusTCPClient1.holdingRegisterWrite(0x13,OFF);//write channel 1 (WP_Speed) to 10 volts
 modbusTCPClient1.holdingRegisterWrite(0x14,OFF);//write channel 2 (FCV134) to 10 volts
 modbusTCPClient1.holdingRegisterWrite(0x15,OFF);//write channel 3 (FCV205) to 10 volts
 modbusTCPClient1.holdingRegisterWrite(0x16,OFF);//write channel 4 (FCV141) to 10 volts

 //digital outputs
 modbusTCPClient3.coilWrite(7,OFF);//XV801
 modbusTCPClient3.coilWrite(13,255);//BLWR_EN ON bc opposite
 modbusTCPClient3.coilWrite(14,255);//WP_EN ON bc opposite
 modbusTCPClient3.coilWrite(9,OFF);//TWV308
 modbusTCPClient3.coilWrite(6,OFF);//XV1100
 modbusTCPClient3.coilWrite(5,OFF);//XV501
 modbusTCPClient3.coilWrite(12,OFF);//BMM_CR2
 modbusTCPClient3.coilWrite(8,OFF);//TWV901
 modbusTCPClient3.coilWrite(4,OFF);//xv909

 FCV134_BURNER_FUEL_FLOW_FB = (modbusTCPClient2.holdingRegisterRead(3026)*(.005))-20;//ref:43028 read Channel 10 FCV134
 if(!(FCV134_BURNER_FUEL_FLOW_FB<0.1)){ERROR=2;FSM_STATE=0;SENSOR_INTEGRITY_CHECK=false;}
 else{SENSOR_INTEGRITY_CHECK=true;}

}

void readOCI(){
  Serial.println("Reading OCI417.");
  OCI_RESULT=NODE.readInputRegisters(OCI_INPUT_STATUS_REGISTER,2);//address,qty
  //do something with data if read is successful
  if (OCI_RESULT==NODE.ku8MBSuccess)
  {
    OCI_INPUT_STATUS_WORD = NODE.getResponseBuffer(0);
    Serial.print("OCI INPUT WORD: ");Serial.println(OCI_INPUT_STATUS_WORD,BIN);
    DUN_ZSL = bitRead(OCI_INPUT_STATUS_WORD,1);//PROOF OF CLOSURE
    DUN_PSH = bitRead(OCI_INPUT_STATUS_WORD,4);//PRESS SW VALVE PROVING
    DUN_PSL = bitRead(OCI_INPUT_STATUS_WORD,5);//LOW GAS PRESSURE SWITCH
    COMBUSTION_PRESSURE_SWITCH = bitRead(OCI_INPUT_STATUS_WORD,7);//COMBUSTION AIR SW

    OCI_OUTPUT_STATUS_WORD=NODE.getResponseBuffer(1);
    Serial.print("OCI OUTPUT WORD: ");Serial.println(OCI_OUTPUT_STATUS_WORD,BIN);
    BMM_PROOF_OF_FLAME=bitRead(OCI_OUTPUT_STATUS_WORD,0);
    if(FSM_STATE>6 && BMM_PROOF_OF_FLAME==false){FSM_STATE=0;ERROR=10;}
    BMM_ALARM_STATUS=bitRead(OCI_OUTPUT_STATUS_WORD,1);
    OCI_TO_BMM_COM=bitRead(OCI_OUTPUT_STATUS_WORD,2);
  }
}

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void readBtn(){
  Serial.println("Reading Buttons.");
  //read if there has been a change for the inputs
  //function for reading buttons and signaling indicators

  if(!modbusTCPClient3.connected()){modbusTCPClient3.begin(serverA2,502);}

  if(modbusTCPClient3.inputRegisterRead(3002)){

    if(bitRead(modbusTCPClient3.holdingRegisterRead(26),15)){
      ESTOP_FLAG=true;ERROR=1;
      modbusTCPClient3.coilWrite(1,0);
      modbusTCPClient3.coilWrite(3,0);
    }
  if(ESTOP_FLAG==false){

    if(bitRead(modbusTCPClient3.holdingRegisterRead(26),2)){
      AMB_BTN_FLAG=true;GRN_BTN_FLAG=false;
      modbusTCPClient3.coilWrite(1,0);
      modbusTCPClient3.coilWrite(3,1);
    }

    if(bitRead(modbusTCPClient3.holdingRegisterRead(26),0)){
      GRN_BTN_FLAG=true;AMB_BTN_FLAG=false;
      modbusTCPClient3.coilWrite(1,1);
      modbusTCPClient3.coilWrite(3,0);
   }
  }
 }
}

/*

error map
1-estop
2-fcv134 timeout integrity check
3-pt318 over 250psi
4-pt213 over 250psi
5-pt420 over 250psi
6-pt304 over 250psi
7-super heat error
8-No Flame detected
9-Steam gen timeout
10-Loss of Flame
11-Burner reach 800 c timeout

12-Temperature > 1000C
13-temp error
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-

36-

*/

Suggestions always welcome

Off the top of my head.. using a struct to collect the data and a state machine instead of millis would make the code more readable and neater if not faster.

no F-Makro for fix text on an AVR?

no enumeration for the states of a state machine but magic numbers?

```
case 4://bmm on //5 seconds
```

wasting two pins for RE/DE?

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

@noiasca
The Serial print() are for debugging right now and will be removed but I do hear ya.
The void preTransmission(){} function stuff is part of the ModbusMaster Library. Those pins are for activating an RS485 Chip
The enums is a great idea.

@mugambi
Hey im not sure how i would use a struct for a state machine.
a pseudocode would be very desirable.
I was told to use a Switch case to make a State machine.
Is there another way? How would I use a Struct?
anyways, heres the code.

//Revision of JBOX with moxa and stride ioexpanders

#include <Wire.h> //For I2C comm
#include <Adafruit_GFX.h>
#include <LedHelper.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_LiquidCrystal.h"
#include<SPI.h>//serial peripheral interface
#include<Ethernet3.h>//for ethernet
#include<ArduinoRS485.h> //ArduinoModbus depends on the ArduinoRS485 lib
#include<ArduinoModbus.h>//for modbus tcp/ip
#include<ModbusMaster.h>//for oci417.10 rs485 interface with BMM

#include<pid.h>
#include "DEFINES.h"
#include "GLOBALS.h"

// OCI417.10 rs485 interface with BMM
ModbusMaster NODE;

//modbus tcp/ip communication declarations
byte mac[] = { 0xdE, 0xAD, 0xBE, 0xEF, 0xED};

IPAddress ip(128, 1, 1, 110); //needs to be similar to Acromag because will not connect

EthernetClient ethClient1;
EthernetClient ethClient2;
EthernetClient ethClient3;
EthernetClient ethClient4;
EthernetClient ethClient5;
EthernetClient ethClient6;
EthernetClient ethClient7;
EthernetClient ethClient8;

ModbusTCPClient modbusTCPClient1(ethClient1);
ModbusTCPClient modbusTCPClient2(ethClient2);
ModbusTCPClient modbusTCPClient3(ethClient3);
ModbusTCPClient modbusTCPClient4(ethClient4);
ModbusTCPClient modbusTCPClient5(ethClient5);
ModbusTCPClient modbusTCPClient6(ethClient6);
ModbusTCPClient modbusTCPClient7(ethClient7);
ModbusTCPClient modbusTCPClient8(ethClient8);

IPAddress serverIOEX1(128, 1, 1, 101); // IP of ioex1 STRIDE SIO-MB04DAS 4CH Analog Output
IPAddress serverIOEX2(128, 1, 1, 102); // IP of ioex2 STRIDE SIO-MB04DAS 4CH Analog Output
IPAddress serverIOEX3(128, 1, 1, 103); // IP of ioex3 STRIDE SIO-MB04DAS 8CH TC/mV Input
IPAddress serverIOEX4(128, 1, 1, 104); // IP of ioex4 STRIDE SIO-MB04DAS 8CH TC/mV Input
IPAddress serverIOEX5(128, 1, 1, 105); // IP of ioex5 STRIDE SIO-MB04DAS 8CH TC/mV Input
IPAddress serverIOEX6(128, 1, 1, 106); // IP of ioex6 MOXA IOLOGIK E1240 8CH Analog Input
IPAddress serverIOEX7(128, 1, 1, 107); // IP of ioex7 MOXA IOLOGIK E1211 16CH Digital Output
IPAddress serverIOEX8(128, 1, 1, 108); // IP of ioex8 MOXA IOLOGIK E1210 16CH Digital Input

//PID controllers
PID SUPER_HEAT_TT303 = PID(0.1, RO_PUMP_TOP_SPEED, RO_PUMP_AT_10_GRAMS_PER_SEC, 1000, 0, 0); //dt,max,min,kp,kd,ki
PID SUPER_HEAT_TT306 = PID(0.1, RO_PUMP_TOP_SPEED, RO_PUMP_AT_10_GRAMS_PER_SEC, 0.9, 0.01, 0); //dt,max,min,kp,kd,ki
PID STEAM_GEN_PID = PID(0.5, BLOWER_TOP_SPEED, BLOWER_RAMP_END , 5000, 0, 0);//dt,max,min,kp,kd,ki;//0.1,100,-100,0.8,0.01,0.5
PID BURNER_TEMP_CROSSOVER_PID = PID(0.9, 10000, 6500, 1000, 0, 0); //dt,max,min,kp,kd,ki;//
PID OPEN_SR_FUEL_PID = PID(0.9, 10000, 0, 100, 0.01, 0);//dt,max,min,kp,kd,ki;//

//Daughter Board periferals
SmallMatrix smallMatrix[3] = {SmallMatrix(0x70), SmallMatrix(0x71), SmallMatrix(0x72) };
LargeMatrix bigMatrix[3] = {LargeMatrix(0x73), LargeMatrix(0x74), LargeMatrix(0x75) };
Adafruit_LiquidCrystal lcd(0);

//telemetry
union floatToBytes  {
  char  asBytes[4] = {0};
  float asFloat;
};

void setup() {

  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  // Init in receive mode rs485
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600); // for dedbugging

  Serial1.begin(9600);//FOR OCI MODULE rs485
  NODE.begin(1, Serial1); //unit id1 on serial1
  NODE.preTransmission(preTransmission);
  NODE.postTransmission(postTransmission);

  delay(500);//wait for system to boot up

  pinMode(LED_PWR, OUTPUT);
  digitalWrite(LED_PWR, HIGH);
  pinMode(TRACO_24VDC, OUTPUT);
  digitalWrite(TRACO_24VDC, HIGH);
  pinMode(ESTOP_BREAK, OUTPUT);
  digitalWrite(ESTOP_BREAK, HIGH);

  //reseting wifichip
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  delay(50);

  //start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  integrityCheck();
  connect_IO_Expanders();
  readBtn();
  readOCI();

  CURRENT_MILLIS = millis(); //So as to not divide by zero on first looptime.
  LOOP_MILLIS = millis();

  lcd.begin(16, 2);

  DB_INIT();
  SENSOR_INTEGRITY_CHECK = true;

}

void loop() {

  Serial.print("Error:"); Serial.println(ERROR); //debugging
  Serial.print("GRN_BTN_FLAG: "); Serial.println(GRN_BTN_FLAG);
  Serial.print("AMB_BTN_FLAG: "); Serial.println(AMB_BTN_FLAG);
  Serial.print("ESTOP_FLAG: "); Serial.println(ESTOP_FLAG);
  Serial.println(FSM_STATE);

  LOOP_TIME = LOOP_MILLIS - CURRENT_MILLIS;
  CURRENT_MILLIS = millis();

  DB_RX();
  error_Checker();
  connect_IO_Expanders();
  integrityCheck();
  superheatTest();
  readTCs();
  readBtn();
  readOCI();
  readPTs();
  blinkGRN();
  blinkAMB();
  readOut();


  if (GRN_BTN_FLAG && !ESTOP_FLAG) {

    switch (FSM_STATE) {

      case INITIALIZE: //initialize

        if (!SENSOR_INTEGRITY_CHECK) {

          break;
        }
        modbusTCPClient3.coilWrite(2, 1); //CH2 digital output xv909//ON


        if (!COMBUSTION_PRESSURE_SWITCH) {

          modbusTCPClient1.holdingRegisterWrite(40, BLOWER_PURGE_SPEED); //write channel 0 (BLWRSpeed)
          modbusTCPClient7.coilWrite(9, 1); //BLWR_EN..The logic is opposite
          break;
        }

        if (DUN_PSL) {
          Serial.print("Break due to DUN_PSL Switch: ");
          Serial.println(DUN_PSL);
          break;
        }

        //check for RO water???//need another sensor
        modbusTCPClient1.holdingRegisterWrite(40, OFF); //write channel 0 (BLWRSpeed)
        modbusTCPClient7.coilWrite(9, 0); //BLWR_EN..

        if (BMM_PROOF_OF_FLAME) { //get that flame out before proceed

          if (!modbusTCPClient1.connected()) {
            modbusTCPClient1.begin(serverIOEX1, 502);
          }
          if (!modbusTCPClient2.connected()) {
            modbusTCPClient2.begin(serverIOEX2, 502);
          }

          modbusTCPClient1.holdingRegisterWrite(42, OFF); //write channel 2 ioex1 (FCV134)
          modbusTCPClient2.holdingRegisterWrite(40, OFF); //write channel 0 ioex2 (FCV141)

          if (!modbusTCPClient7.connected()) {
            modbusTCPClient7.begin(serverIOEX7, 502);
          }
          modbusTCPClient7.coilWrite(6, OFF); //TWV308
          modbusTCPClient7.coilWrite(4, OFF); //XV1100
          modbusTCPClient7.coilWrite(3, OFF); //XV501
          modbusTCPClient7.coilWrite(8, OFF); //BMM_CR2
          modbusTCPClient7.coilWrite(7, OFF); //TWV901
          modbusTCPClient7.coilWrite(2, ON); //CH4 digital output xv909
          break;

        }
        FSM_STATE = DEPRESSURIZE;
        break;

      case DEPRESSURIZE: //depressurise reformer

        modbusTCPClient7.coilWrite(6, OFF); //TWV308

        if (PT420_STEAM_EJECTOR_PRESSURE >= ATMOSPHERIC_PRESSURE) {

          PT420_STEAM_EJECTOR_PRESSURE = (modbusTCPClient6.holdingRegisterRead(5) * (.0125)) - 47; //ref:43016 read Channel 6 PT420
          // PT420_STEAM_EJECTOR_PRESSURE = modbusTCPClient2.holdingRegisterRead(3022);
          modbusTCPClient7.coilWrite(2, ON); //CH2 digital output xv909

          break;
        }

        modbusTCPClient7.coilWrite(2, OFF); //CH2 digital output xv909

        //250 psi
        if (PT213_RO_PRESSURE < 250) {

          //just guessing on the fcv205 flow, as long as we stay beneath 250psi
          modbusTCPClient1.holdingRegisterWrite(40, BLOWER_PURGE_SPEED); //write channel 0 (BLWRSpeed)
          modbusTCPClient1.holdingRegisterWrite(42, FCV205_AT_35_PERCENT); //write channel 2 (FCV205)//made steam at 1000//last 3000//~about 35 percent
          modbusTCPClient1.holdingRegisterWrite(41, RO_PUMP_AT_10_GRAMS_PER_SEC); //write channel 1 (WP_Speed) made steam at 1*2000//last 3*2000
          modbusTCPClient7.coilWrite(10, ON); //WP_EN..
          modbusTCPClient7.coilWrite(9, ON); //BLWR_EN..
          FSM_STATE = SUPERHEAT_TEST;
          PREVIOUS_MILLIS = millis();
          break;
        }
        break;

      case SUPERHEAT_TEST://Superheat Test

        //make sure we are closed//xv909
        modbusTCPClient7.coilWrite(2, OFF); //CH4 digital output xv909
        //monitor tc 407 & 408
        //need a timer

        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= SUPERHEAT_TIMER) {

          TT303_HX504_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(42) * (0.095);
          //guessing on 300 degrees centigrade
          if (TT303_HX504_STEAM_OUT > 300) {
            ERROR = 7; FSM_STATE = INITIALIZE; break;
          }
          break;
        }
        FSM_STATE = BMM_OFF;
        PREVIOUS_MILLIS = millis();
        break;

      case BMM_OFF://bmm off //30 seconds;

        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_OFF_TIMER) {

          modbusTCPClient1.holdingRegisterWrite(40, OFF); //write channel 0 (BLWRSpeed)
          modbusTCPClient7.coilWrite(8, OFF); //BMM_CR2 bmm off
          break;
        }
        FSM_STATE = BMM_ON;
        PREVIOUS_MILLIS = millis();
        break;

      case BMM_ON:
        //bmm on //5 seconds
        modbusTCPClient7.coilWrite(8, ON); //BMM_CR2 turn on
        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_START_TIMER) {
          break;
        }
        PREVIOUS_MILLIS = millis();
        FSM_STATE = BMM_PURGE;
        break;

      case BMM_PURGE:
        //bmm purge

        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= BMM_PURGE_TIMER) {
          modbusTCPClient1.holdingRegisterWrite(40, BLOWER_PURGE_SPEED); //write channel 0 (BLWRSpeed)
          break;
        }
        PREVIOUS_MILLIS = millis();
        FSM_STATE = BMM_IGNITION;
        break;

      case BMM_IGNITION:
        //BMM ignition

        modbusTCPClient1.holdingRegisterWrite(40, BLOWER_IGNITION_SPEED); //write channel 0 (BLWRSpeed)
        modbusTCPClient1.holdingRegisterWrite(42, FCV134_BURNER_FUEL_FLOW_IGNITION); //write channel 2 (FCV134)

        if (CURRENT_MILLIS - PREVIOUS_MILLIS >= BMM_IGNITION_TIMER) {

          if (!BMM_PROOF_OF_FLAME) {
            ERROR = 8;
            FSM_STATE = INITIALIZE;
            break;
          }
          else {
            FSM_STATE = BURNER_RAMP;
            PREVIOUS_MILLIS = millis();
            PREVIOUS_MILLIS_2 = millis();
            break;
          }
        }
        break;

      case BURNER_RAMP:
        //start burner ramp//15 minutes//900 seconds
        //burner ramp is 4mins
        //timing to reach 800c is 15 mins

        TT511_SILICON_CARBIDE_OUT = (modbusTCPClient4.holdingRegisterRead(43)) * (0.095);
        TT513_HX504_IN = (modbusTCPClient4.holdingRegisterRead(45)) * (0.095);

        Serial.print("TT511: "); Serial.println(TT511_SILICON_CARBIDE_OUT);
        Serial.print("TT513: "); Serial.println(TT513_HX504_IN);
        Serial.print("Burner Ramp Timer 15 mins: "); Serial.println(CURRENT_MILLIS - PREVIOUS_MILLIS);

        if (CURRENT_MILLIS - PREVIOUS_MILLIS >= BURNER_REACH_END_TIMER) {

          if (TT511_SILICON_CARBIDE_OUT >= BURNER_TEMP_RAMP_END) {
            FSM_STATE = STEAM_GEN;
            PREVIOUS_MILLIS = millis();
            break;
          }

          if ( TT513_HX504_IN >= BURNER_TEMP_RAMP_END) {
            FSM_STATE = STEAM_GEN;
            PREVIOUS_MILLIS = millis();
            break;
          }

          ERROR = 11; FSM_STATE = INITIALIZE; break;
        }

        if (TT511_SILICON_CARBIDE_OUT >= BURNER_TEMP_RAMP_END) {
          FSM_STATE = STEAM_GEN;
          PREVIOUS_MILLIS = millis();
          break;
        }

        if ( TT513_HX504_IN >= BURNER_TEMP_RAMP_END) {
          FSM_STATE = STEAM_GEN;
          PREVIOUS_MILLIS = millis();
          break;
        }

        BLOWER_SPEED_FEEDBACK = modbusTCPClient6.inputRegisterRead(0);//10volts/10000counts
        FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient6.inputRegisterRead(7);//ref:43028 read Channel 7 FCV134//~10079

        Serial.print("Blower FB: "); Serial.println(BLOWER_SPEED_FEEDBACK);
        Serial.print("FCV134 FB: "); Serial.println(FCV134_BURNER_FUEL_FLOW_FB);

        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= BURNER_RAMP_TIMER) {

          if (CURRENT_MILLIS - PREVIOUS_MILLIS_2 >= 5000) {

            //if(BLOWER_SPEED_FEEDBACK<=14030){
            //modbusTCPClient1.holdingRegisterWrite(0x12,BLOWER_SPEED_FEEDBACK+220);}//write channel 0 blwr speed//44counts/sec
            //if(FCV134_BURNER_FUEL_FLOW_FB<=16121){
            //modbusTCPClient1.holdingRegisterWrite(0x14,FCV134_BURNER_FUEL_FLOW_FB+125);}//25counts/sec

            modbusTCPClient1.holdingRegisterWrite(42, FCV134_BURNER_FUEL_FLOW_RAMP_END); //16121 best thus far, fuel
            modbusTCPClient1.holdingRegisterWrite(40, BLOWER_RAMP_END); //7100 best thus far, blwr
            PREVIOUS_MILLIS_2 = millis();
            break;
          }
        }
        else {
          //modbusTCPClient1.holdingRegisterWrite(0x14,16121);//25counts/sec
          //modbusTCPClient1.holdingRegisterWrite(0x12,13000);//write channel 0 blwr speed//44counts/sec
          break;
        }
        break;

      case STEAM_GEN://Steam generation


        //steam gen timeout 30 mins
        if (CURRENT_MILLIS - PREVIOUS_MILLIS >= STEAM_GENERATION_TIMER) {
          ERROR = 9;
          FSM_STATE = INITIALIZE;
          break;
        }

        //PID control blower to acheive 170PSI pt304
        BLOWER_SPEED_FEEDBACK = modbusTCPClient6.inputRegisterRead(0); //10volts/20000counts

        steamGenPID();

        if (PT304_TWV308_INPUT_PRESSURE <= 172 && PT304_TWV308_INPUT_PRESSURE >= 168) {
          if (PSI_INIT_TIMER == false) {
            PREVIOUS_MILLIS_1 = millis();
            PSI_INIT_TIMER = true;
            break;
          }

          if (CURRENT_MILLIS - PREVIOUS_MILLIS_1 >= STEAM_AT_170PSI_TIMER && PSI_INIT_TIMER == true) {
            BLOWER_SPEED_AT_170PSI = BLOWER_SPEED_FEEDBACK;
            modbusTCPClient1.holdingRegisterWrite(40, BLOWER_SPEED_AT_170PSI);
            modbusTCPClient7.coilWrite(3, ON); //XV501 ON
            FSM_STATE = OPEN_SR_FUEL; PREVIOUS_MILLIS = millis(); PSI_INIT_TIMER = false; BURNER_TEMP_CROSSOVER = TT513_HX504_IN; break;
          }
        }
        else {
          PSI_INIT_TIMER = false;  //If not in bounds of 170-psi, reset timer. Turn on xv501
          modbusTCPClient6.coilWrite(3, OFF);
        }
        break;

      case OPEN_SR_FUEL:
        //OPEN SR FUEL

        //Point twv308 to reformer
        if (!modbusTCPClient7.connected()) {
          modbusTCPClient7.begin(serverIOEX7, 502);
        }
        modbusTCPClient7.coilWrite(6, ON); //TWV308//TWV308 ON//direct steam to reformer
        modbusTCPClient7.coilWrite(5, ON); //XV801 On to induce gas feed to reformer

        //assign percentage to fcv141 sr fuel flow control //fcv205 kicked to 50%
        if (!modbusTCPClient2.connected()) {
          modbusTCPClient2.begin(serverIOEX2, 502);
        }
        modbusTCPClient2.holdingRegisterWrite(40, FCV141_SR_FUEL_START_PERCENT); //write channel 4 (FCV141)
        if (!modbusTCPClient1.connected()) {
          modbusTCPClient1.begin(serverIOEX1, 502);
        }
        modbusTCPClient1.holdingRegisterWrite(43, FCV205_AT_50_PERCENT); //write channel 3 (FCV205)

        //read fcv134 for burner temp control
        if (!modbusTCPClient6.connected()) {
          modbusTCPClient6.begin(serverIOEX6, 502);
        }
        FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient6.holdingRegisterRead(7);//read Channel 7 FCV134

        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= OPEN_SR_FUEL_TIMER) {

          //pid burner control for 880C
          BURNER_FUEL_CUT_OFFSET = BURNER_TEMP_CROSSOVER_PID.calc_reverse(BURNER_TEMP_CROSSOVER , TT513_HX504_IN);
          Serial.print("BURNER_FUEL_CUT_OFFSET: "); Serial.println(BURNER_FUEL_CUT_OFFSET);
          Serial.print("TT513_HX504_IN: "); Serial.println(TT513_HX504_IN);
          if (!modbusTCPClient1.connected()) {
            modbusTCPClient1.begin(serverIOEX1, 502);
          }
          modbusTCPClient1.holdingRegisterWrite(42, BURNER_FUEL_CUT_OFFSET);//write channel 2 (FCV134)

          //pid srfuel control for 0.3g/second
          if (!modbusTCPClient6.connected()) {
            modbusTCPClient6.begin(serverIOEX6, 502);
          }
          FT132_NG_FEED_FLOW = modbusTCPClient6.inputRegisterRead(2);
          FT132_ADJUSTED_MEASURE = (FT132_NG_FEED_FLOW * FT132_PIPE_DIA_CONV * FT132_COUNTS_TO_G_PER_SEC) - FT132_4MA_OFFSET;
          SR_FUEL_OFFSET = OPEN_SR_FUEL_PID.calculate(SR_FUEL_CUT, FT132_ADJUSTED_MEASURE); //calc offset for FCV141
          Serial.print("SR_FUEL_OFFSET: "); Serial.println(SR_FUEL_OFFSET);
          Serial.print("FT132_ADJUSTED_MEASURE: "); Serial.println(FT132_ADJUSTED_MEASURE);
          if (!modbusTCPClient2.connected()) {
            modbusTCPClient2.begin(serverIOEX2, 502);
          }
          modbusTCPClient2.holdingRegisterWrite(40, SR_FUEL_OFFSET); //write channel 4 (FCV141)

          //check conformance and restart time if out of bounds
          if ((FT132_ADJUSTED_MEASURE > (SR_FUEL_CUT + 0.2)) || (BURNER_TEMP_CROSSOVER < (SR_FUEL_CUT - 0.2))) {
            PREVIOUS_MILLIS = millis();
            break;
          }

          //reset timing if temp falls out of threshold
          if ((BURNER_TEMP_CROSSOVER > (TT513_HX504_IN + 5)) || (BURNER_TEMP_CROSSOVER < (TT513_HX504_IN - 5))) {
            PREVIOUS_MILLIS = millis();
            break;
          }

        }

        else {
          if (ERROR == 0) {
            FSM_STATE = IDLE_MODE;
            PREVIOUS_MILLIS = millis();
            break;
          }
          FSM_STATE = OPEN_SR_FUEL; PREVIOUS_MILLIS = millis(); break;
        }

        break;

      case IDLE_MODE:
        //idle hold mode blower/burner pid
        BLOWER_SPEED_FEEDBACK = modbusTCPClient6.inputRegisterRead(0);//10volts/10000counts
        FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient6.inputRegisterRead(7);//ref:43028 read Channel 7 FCV134

        //3 mins idle ????
        if (CURRENT_MILLIS - PREVIOUS_MILLIS <= 180000) {

          //burner fuel PID
          BURNER_FUEL_CUT_OFFSET = OPEN_SR_FUEL_PID.calculate(BURNER_TEMP_CROSSOVER , TT513_HX504_IN);
          BURNER_FUEL_CUT = FCV134_BURNER_FUEL_FLOW_FB + BURNER_FUEL_CUT_OFFSET;
          modbusTCPClient1.holdingRegisterWrite(42, BURNER_FUEL_CUT);//write channel 2 (FCV134)

          //blower pid
          steamGenPID();

          //check if out of threshold
          if ((BURNER_TEMP_CROSSOVER > (TT513_HX504_IN + 5)) || (BURNER_TEMP_CROSSOVER < (TT513_HX504_IN - 5))) {
            PREVIOUS_MILLIS = millis();
            break;
          }
          if (PT304_TWV308_INPUT_PRESSURE <= 171 && PT304_TWV308_INPUT_PRESSURE >= 169) {
            PREVIOUS_MILLIS = millis();
            break;
          }
        }

        else {
          FSM_STATE = STABILIZE_MODE;
          break;
        }
        break;

      case STABILIZE_MODE:
        //reformer stabilize
        break;

      default:
        Serial.println("Default.");
        break;

    }//end switch

  }//end if(GRN_BTN_FLAG && !ESTOP_FLAG)

  LOOP_MILLIS = millis();
  //end loop
}

void steamGenPID() {

  if (!modbusTCPClient6.connected()) {
    modbusTCPClient6.begin(serverIOEX6, 502);
  }
  // PT304_TWV308_INPUT_PRESSURE_RAW = modbusTCPClient2.holdingRegisterRead(3023);//ref:43017 read Channel 7 PT304
  PT304_TWV308_INPUT_PRESSURE = modbusTCPClient6.inputRegisterRead(6);// read Channel 6 PT304

  BLOWER_SPEED_OFFSET = STEAM_GEN_PID.calculate(170 , PT304_TWV308_INPUT_PRESSURE);
  Serial.print("BLOWER offset: "); Serial.println(BLOWER_SPEED_OFFSET);

  if (!modbusTCPClient1.connected()) {
    modbusTCPClient1.begin(serverIOEX1, 502);
  }
  modbusTCPClient1.holdingRegisterWrite(40, BLOWER_SPEED_OFFSET);

}
void superheatTest() {

  if (!modbusTCPClient3.connected()) {
    modbusTCPClient3.begin(serverIOEX3, 502);
  }
  TT303_HX504_STEAM_OUT = (modbusTCPClient3.holdingRegisterRead(42)) * (0.095); //tt303
  TT306_EJECTOR_STEAM_IN = (modbusTCPClient3.holdingRegisterRead(43)) * (0.095); //tt306

  if (!modbusTCPClient6.connected()) {
    modbusTCPClient6.begin(serverIOEX6, 502);
  }
  RO_PUMP_FEEDBACK = modbusTCPClient6.inputRegisterRead(1);//Ch1 ro water fb

  RO_SPEED_OFFSET = SUPER_HEAT_TT303.calc_reverse(130 , TT303_HX504_STEAM_OUT);

  if (!modbusTCPClient1.connected()) {
    modbusTCPClient1.begin(serverIOEX1, 502);
  }
  modbusTCPClient1.holdingRegisterWrite(41, RO_SPEED_OFFSET);//write channel 1 (WP_Speed)
  if (!modbusTCPClient7.connected()) {
    modbusTCPClient7.begin(serverIOEX7, 502);
  }
  modbusTCPClient7.coilWrite(10, ON); //WP_EN..Control is opposite

  Serial.print("RO_SPEED_OFFSET: "); Serial.println(RO_SPEED_OFFSET);
  Serial.print("TT303_HX504_STEAM_OUT: "); Serial.println(TT303_HX504_STEAM_OUT);
  Serial.print("TT306_EJECTOR_STEAM_IN: "); Serial.println(TT306_EJECTOR_STEAM_IN);

}
void connect_IO_Expanders() {

  //CONNECT TO ACROMAGS
  if (!modbusTCPClient1.connected()) {
    modbusTCPClient1.begin(serverIOEX1, 502);
  }
  if (!modbusTCPClient2.connected()) {
    modbusTCPClient2.begin(serverIOEX2, 502);
  }
  if (!modbusTCPClient3.connected()) {
    modbusTCPClient3.begin(serverIOEX3, 502);
  }
  if (!modbusTCPClient4.connected()) {
    modbusTCPClient4.begin(serverIOEX4, 502);
  }
  if (!modbusTCPClient5.connected()) {
    modbusTCPClient5.begin(serverIOEX5, 502);
  }
  if (!modbusTCPClient6.connected()) {
    modbusTCPClient6.begin(serverIOEX6, 502);
  }
  if (!modbusTCPClient7.connected()) {
    modbusTCPClient7.begin(serverIOEX7, 502);
  }
  if (!modbusTCPClient8.connected()) {
    modbusTCPClient8.begin(serverIOEX8, 502);
  }
}
void readPTs() {

  Serial.println("Reading PTs");
  if (!modbusTCPClient6.connected()) {
    modbusTCPClient6.begin(serverIOEX6, 502);
  }
  //checking PTs
  modbusTCPClient6.begin(serverIOEX6, 502);
  PT318_HX406_OUTPUT_PRESSURE = modbusTCPClient6.inputRegisterRead(3);//read Channel 3 PressureTransducer_318.. 1psi/80counts maximux
  PT213_RO_PRESSURE = modbusTCPClient6.inputRegisterRead(4);// read Channel 4 PT213
  PT420_STEAM_EJECTOR_PRESSURE = modbusTCPClient6.inputRegisterRead(5);// read Channel 5 PT420
  PT304_TWV308_INPUT_PRESSURE = modbusTCPClient6.inputRegisterRead(6);// read Channel 6 PT304

  Serial.print("PT318: "); Serial.println(PT318_HX406_OUTPUT_PRESSURE);
  Serial.print("PT213: "); Serial.println(PT213_RO_PRESSURE);
  Serial.print("PT420: "); Serial.println(PT420_STEAM_EJECTOR_PRESSURE);
  Serial.print("PT304: "); Serial.println(PT304_TWV308_INPUT_PRESSURE);

  if (PT318_HX406_OUTPUT_PRESSURE >= 240) {
    FSM_STATE = INITIALIZE;
    ERROR = 3;
    Serial.print("PT318 >240PSI: ");
    Serial.println(PT318_HX406_OUTPUT_PRESSURE);
  }
  else {
    ERROR = 0;
  }
  if ( PT213_RO_PRESSURE >= 240) {
    FSM_STATE = INITIALIZE;
    ERROR = 4;
    Serial.print("PT213 >240PSI: ");
    Serial.println(PT213_RO_PRESSURE);
  }
  else {
    ERROR = 0;
  }
  if (PT420_STEAM_EJECTOR_PRESSURE >= 240) {
    FSM_STATE = INITIALIZE;
    ERROR = 5;
    Serial.print("PT420 >240PSI: ");
    Serial.println(PT420_STEAM_EJECTOR_PRESSURE);
  }
  else {
    ERROR = 0;
  }
  if (PT304_TWV308_INPUT_PRESSURE >= 240) {
    FSM_STATE = INITIALIZE;
    ERROR = 6;
    Serial.print("PT305 >240PSI: ");
    Serial.println(PT304_TWV308_INPUT_PRESSURE);
  }
  else {
    ERROR = 0;
  }

}
void readTCs() {

  if (!modbusTCPClient3.connected()) {
    modbusTCPClient3.begin(serverIOEX3, 502);
  }
  if (!modbusTCPClient4.connected()) {
    modbusTCPClient4.begin(serverIOEX4, 502);
  }
  if (!modbusTCPClient5.connected()) {
    modbusTCPClient5.begin(serverIOEX5, 502);
  }

  //read acromag 4

  switch (TC_CHECK_COUNTER) {
    case 1:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000) {

        TT142_SR_FUEL = modbusTCPClient3.holdingRegisterRead(40) * (0.095);
        TT301_HX406_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(41) * (0.095);
        TT303_HX504_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(42) * (0.095);
        TT306_EJECTOR_STEAM_IN = modbusTCPClient3.holdingRegisterRead(43) * (0.095);
        TT313_HX402_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(44) * (0.095);
        TT319_HX402_STEAM_SYSTEM = modbusTCPClient3.holdingRegisterRead(45) * (0.095);
        TT407_STEAM_REFORMER_OUT_LREF = modbusTCPClient3.holdingRegisterRead(46) * (0.095);
        TT408_HTS_IN_LREF = modbusTCPClient3.holdingRegisterRead(47) * (0.095);

        if (TT301_HX406_STEAM_OUT > 1000) {
          Serial.println("TT301_HX406_STEAM_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 13;
        }
        if (TT303_HX504_STEAM_OUT > 1000) {
          Serial.println("TT303_HX504_STEAM_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 14;
        }
        if (TT306_EJECTOR_STEAM_IN > 1000) {
          Serial.println("TT306_EJECTOR_STEAM_IN > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 15;
        }
        if (TT313_HX402_STEAM_OUT > 1000) {
          Serial.println("TT313_HX402_STEAM_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 16;
        }
        if (TT319_HX402_STEAM_SYSTEM > 1000) {
          Serial.println("TT319_HX402_STEAM_SYSTEM > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 17;
        }
        if (TT407_STEAM_REFORMER_OUT_LREF > 1000) {
          Serial.println("TT407_STEAM_REFORMER_OUT_LREF > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 18;
        }
        if (TT408_HTS_IN_LREF > 1000) {
          Serial.println("TT408_HTS_IN_LREF > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 19;
        }

        /*
          Serial.print("TT142_SR_FUEL: ");Serial.println(TT142_SR_FUEL);
          Serial.print("TT301_HX406_STEAM_OUT : ");Serial.println(TT301_HX406_STEAM_OUT);
          Serial.print("TT303_HX504_STEAM_OUT : ");Serial.println(TT303_HX504_STEAM_OUT);
          Serial.print("TT306_EJECTOR_STEAM_IN : ");Serial.println(TT306_EJECTOR_STEAM_IN);
          Serial.print("TT313_HX402_STEAM_OUT : ");Serial.println(TT313_HX402_STEAM_OUT);
          Serial.print("TT319_HX402_STEAM_SYSTEM : ");Serial.println(TT319_HX402_STEAM_SYSTEM);
          Serial.print("TT407_STEAM_REFORMER_OUT_LREF : ");Serial.println(TT407_STEAM_REFORMER_OUT_LREF);
          Serial.print("TT408_HTS_IN_LREF : ");Serial.println(TT408_HTS_IN_LREF);
        */

        PREVIOUS_MILLIS_3 = millis();
        TC_CHECK_COUNTER = 2;
        break;
      }

    case 2:
      //read acromag5
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000) {

        TT410_HTS_OUT_LREF = modbusTCPClient4.holdingRegisterRead(40) * (0.095);
        TT411_FPZ_OUT_LREF = modbusTCPClient4.holdingRegisterRead(41) * (0.095);
        TT430_SMR_TUBES_INLET = modbusTCPClient4.holdingRegisterRead(42) * (0.095);
        TT511_SILICON_CARBIDE_OUT = modbusTCPClient4.holdingRegisterRead(43) * (0.095);
        TT512_SILICON_CARBIDE_OUT = modbusTCPClient4.holdingRegisterRead(44) * (0.095);
        TT513_HX504_IN = modbusTCPClient4.holdingRegisterRead(45) * (0.095);
        TT514_HX504_OUT = modbusTCPClient4.holdingRegisterRead(46) * (0.095);
        TT441_SMR_TUBE1_OUT = modbusTCPClient4.holdingRegisterRead(47) * (0.095);

        //     if (TT410_HTS_OUT_LREF > 1000) {
        //     Serial.println("TT410_HTS_OUT_LREF > 1000C");
        //   FSM_STATE = INITIALIZE;
        // ERROR = 20;
        //}
        if (TT411_FPZ_OUT_LREF > 1000) {
          Serial.println("TT411_FPZ_OUT_LREF > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 21;
        }
        if (TT430_SMR_TUBES_INLET > 1000) {
          Serial.println("TT430_SMR_TUBES_INLET > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 22;
        }
        if (TT511_SILICON_CARBIDE_OUT > 1000) {
          Serial.println("TT511_SILICON_CARBIDE_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 23;
        }
        //     if (TT512_SILICON_CARBIDE_OUT > 1000) {
        //      Serial.println("TT512_SILICON_CARBIDE_OUT > 1000C");
        //    FSM_STATE = INITIALIZE;
        //  ERROR = 24;
        //}
        if (TT513_HX504_IN > 1000) {
          Serial.println("TT513_HX504_IN > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 25;
        }
        if (TT514_HX504_OUT > 1000) {
          Serial.println("TT514_HX504_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 26;
        }
        if (TT441_SMR_TUBE1_OUT > 800) {
          Serial.println("TT441_SMR_TUBE1_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 27;
        }

        /*
          Serial.print("TT410_HTS_OUT_LREF : ");Serial.println(TT410_HTS_OUT_LREF);
          Serial.print("TT411_FPZ_OUT_LREF : ");Serial.println(TT411_FPZ_OUT_LREF);
          Serial.print("TT430_SMR_TUBES_INLET : ");Serial.println(TT430_SMR_TUBES_INLET);
          Serial.print("TT511_SILICON_CARBIDE_OUT : ");Serial.println(TT511_SILICON_CARBIDE_OUT);
          Serial.print("TT512_SILICON_CARBIDE_OUT : ");Serial.println(TT512_SILICON_CARBIDE_OUT);
          Serial.print("TT513_HX504_IN : ");Serial.println(TT513_HX504_IN);
          Serial.print("TT514_HX504_OUT : ");Serial.println(TT514_HX504_OUT);
          Serial.print("TT441_SMR_TUBE1_OUT : ");Serial.println(TT441_SMR_TUBE1_OUT);
        */

        TC_CHECK_COUNTER = 3;
        PREVIOUS_MILLIS_3 = millis();
        break;
      }

    case 3:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_3 >= 3000) {

        TT442_SMR_TUBE2_OUT = modbusTCPClient5.holdingRegisterRead(40) * (0.095);
        TT443_SMR_TUBE3_OUT = modbusTCPClient5.holdingRegisterRead(41) * (0.095);
        TT444_SMR_TUBE4_OUT = modbusTCPClient5.holdingRegisterRead(42) * (0.095);
        TT445_SMR_TUBE5_OUT = modbusTCPClient5.holdingRegisterRead(43) * (0.095);
        TT446_SMR_TUBE6_OUT = modbusTCPClient5.holdingRegisterRead(44) * (0.095);
        TT447_SMR_TUBE7_OUT = modbusTCPClient5.holdingRegisterRead(45) * (0.095);
        TT448_SMR_TUBE8_OUT = modbusTCPClient5.holdingRegisterRead(46) * (0.095);
        TT449_SMR_TUBE9_OUT = modbusTCPClient5.holdingRegisterRead(47) * (0.095);

        if (TT442_SMR_TUBE2_OUT > 800) {
          Serial.println("TT442_SMR_TUBE2_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 28;
        }
        if (TT443_SMR_TUBE3_OUT > 800) {
          Serial.println("TT443_SMR_TUBE3_OUT > 1000C");
          FSM_STATE = INITIALIZE;
          ERROR = 29;
        }
        if (TT444_SMR_TUBE4_OUT > 800) {
          Serial.println("TT444_SMR_TUBE4_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 30;
        }
        if (TT445_SMR_TUBE5_OUT > 800) {
          Serial.println("TT445_SMR_TUBE5_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 31;
        }
        if (TT446_SMR_TUBE6_OUT > 800) {
          Serial.println("TT446_SMR_TUBE6_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 32;
        }
        if (TT447_SMR_TUBE7_OUT > 800) {
          Serial.println("TT447_SMR_TUBE7_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 33;
        }
        if (TT448_SMR_TUBE8_OUT > 800) {
          Serial.println("TT448_SMR_TUBE8_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 34;
        }
        if (TT449_SMR_TUBE9_OUT > 800) {
          Serial.println("TT449_SMR_TUBE9_OUT > 700C");
          FSM_STATE = INITIALIZE;
          ERROR = 35;
        }

        /*
          Serial.print("TT442_SMR_TUBE2_OUT : ");Serial.println(TT442_SMR_TUBE2_OUT);
          Serial.print("TT443_SMR_TUBE3_OUT : ");Serial.println(TT443_SMR_TUBE3_OUT);
          Serial.print("TT444_SMR_TUBE4_OUT : ");Serial.println(TT444_SMR_TUBE4_OUT);
          Serial.print("TT445_SMR_TUBE5_OUT : ");Serial.println(TT445_SMR_TUBE5_OUT);
          Serial.print("TT446_SMR_TUBE6_OUT : ");Serial.println(TT446_SMR_TUBE6_OUT);
          Serial.print("TT447_SMR_TUBE7_OUT : ");Serial.println(TT447_SMR_TUBE7_OUT);
          Serial.print("TT448_SMR_TUBE8_OUT : ");Serial.println(TT448_SMR_TUBE8_OUT);
          Serial.print("TT449_SMR_TUBE9_OUT : ");Serial.println(TT449_SMR_TUBE9_OUT);
        */

        TC_CHECK_COUNTER = 1;
        PREVIOUS_MILLIS_3 = millis();
        DB_TX();
        break;
      }

  }
}
void readOut() {
  lcd.setBacklight(HIGH);
  switch (READOUT_COUNTER) {

    case 1:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_4 >= 3000) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("P1_");
        lcd.print((int)PT304_TWV308_INPUT_PRESSURE);//nine tubes
        lcd.setCursor(7, 0);
        lcd.print("P2_");
        lcd.print((int)PT318_HX406_OUTPUT_PRESSURE);//nine tubes
        lcd.setCursor(14, 0);
        lcd.print(FSM_STATE);
        lcd.setCursor(0, 1);
        lcd.print("P3_");
        lcd.print((int)PT420_STEAM_EJECTOR_PRESSURE);//nine tubes
        lcd.setCursor(7, 1);
        lcd.print("P4_");
        lcd.print((int)PT213_RO_PRESSURE);//nine tubes
        READOUT_COUNTER = 2;
        PREVIOUS_MILLIS_4 = millis();
        break;
      }
    case 2:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_4 >= 3000) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("S9_");
        lcd.print((int)TT449_SMR_TUBE9_OUT);//nine tubes
        lcd.setCursor(7, 0);
        lcd.print("H1_");
        lcd.print((int)TT511_SILICON_CARBIDE_OUT);//nine tubes
        lcd.setCursor(14, 0);
        lcd.print(FSM_STATE);
        lcd.setCursor(0, 1);
        lcd.print("H2_");
        lcd.print((int)TT513_HX504_IN);//nine tubes
        lcd.setCursor(7, 1);
        lcd.print("SH_");
        lcd.print((int)TT301_HX406_STEAM_OUT);//nine tubes
        READOUT_COUNTER = 3;
        PREVIOUS_MILLIS_4 = millis();
        break;
      }
    case 3:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_4 >= 3000) {
        lcd.begin(16, 2);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("S1_");
        lcd.print((int)TT441_SMR_TUBE1_OUT);//nine tubes
        lcd.setCursor(7, 0);
        lcd.print("S2_");
        lcd.print((int)TT442_SMR_TUBE2_OUT);//nine tubes
        lcd.setCursor(14, 0);
        lcd.print(FSM_STATE);
        lcd.setCursor(0, 1);
        lcd.print("S3_");
        lcd.print((int)TT443_SMR_TUBE3_OUT);//nine tubes
        lcd.setCursor(7, 1);
        lcd.print("S4_");
        lcd.print((int)TT444_SMR_TUBE4_OUT);//nine tubes
        READOUT_COUNTER = 4;
        PREVIOUS_MILLIS_4 = millis();
        break;
      }
    case 4:
      if (CURRENT_MILLIS - PREVIOUS_MILLIS_4 >= 3000) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("S5_");
        lcd.print((int)TT445_SMR_TUBE5_OUT);//nine tubes
        lcd.setCursor(7, 0);
        lcd.print("S6_");
        lcd.print((int)TT446_SMR_TUBE6_OUT);//nine tubes
        lcd.setCursor(14, 0);
        lcd.print(FSM_STATE);
        lcd.setCursor(0, 1);
        lcd.print("S7_");
        lcd.print((int)TT447_SMR_TUBE7_OUT);//nine tubes
        lcd.setCursor(7, 1);
        lcd.print("S8_");
        lcd.print((int)TT448_SMR_TUBE8_OUT);//nine tubes
        READOUT_COUNTER = 1;
        PREVIOUS_MILLIS_4 = millis();
        break;
      }
  }

}
void integrityCheck() {


  if (!SENSOR_INTEGRITY_CHECK) {


    while (!modbusTCPClient1.connected()) {
      modbusTCPClient1.begin(serverIOEX1, 502);
      Serial.println("CONNECTING IOEX1.");
    }
    while (!modbusTCPClient2.connected()) {
      modbusTCPClient2.begin(serverIOEX2, 502);
      Serial.println("CONNECTING IOEX2.");
    }
    while (!modbusTCPClient3.connected()) {
      modbusTCPClient3.begin(serverIOEX3, 502);
      Serial.println("CONNECTING IOEX3.");
    }
    while (!modbusTCPClient4.connected()) {
      modbusTCPClient4.begin(serverIOEX4, 502);
      Serial.println("CONNECTING IOEX4.");
    }
    while (!modbusTCPClient5.connected()) {
      modbusTCPClient5.begin(serverIOEX5, 502);
      Serial.println("CONNECTING IOEX5.");
    }
    while (!modbusTCPClient7.connected()) {
      modbusTCPClient7.begin(serverIOEX7, 502);
      Serial.println("CONNECTING IOEX7.");
    }
    while (!modbusTCPClient8.connected()) {
      modbusTCPClient8.begin(serverIOEX8, 502);
      Serial.println("CONNECTING IOEX8.");
    }
    while (!modbusTCPClient6.connected()) {
      modbusTCPClient6.begin(serverIOEX6, 502);
      Serial.println("CONNECTING IOEX6.");
    }

    PT318_HX406_OUTPUT_PRESSURE = modbusTCPClient6.inputRegisterRead(3);// read Channel 3 PressureTransducer_318.. 1psi/80counts maximum
    PT213_RO_PRESSURE = modbusTCPClient6.inputRegisterRead(4);// read Channel 4 PT213
    PT420_STEAM_EJECTOR_PRESSURE = modbusTCPClient6.inputRegisterRead(5);//read Channel 5 PT420
    PT304_TWV308_INPUT_PRESSURE = modbusTCPClient6.inputRegisterRead(6);// read Channel 6 PT304

    if (PT318_HX406_OUTPUT_PRESSURE >= 240) {
      FSM_STATE = INITIALIZE;
      ERROR = 3;
    }
    else {
      ERROR = 0;
    }
    if (PT213_RO_PRESSURE >= 240) {
      FSM_STATE = INITIALIZE;
      ERROR = 4;
    }
    else {
      ERROR = 0;
    }
    if (PT420_STEAM_EJECTOR_PRESSURE >= 240) {
      FSM_STATE = INITIALIZE;
      ERROR = 5;
    }
    else {
      ERROR = 0;
    }
    if (PT304_TWV308_INPUT_PRESSURE >= 240) {
      FSM_STATE = INITIALIZE;
      ERROR = 6;
    }
    else {
      ERROR = 0;
    }

    TT142_SR_FUEL = modbusTCPClient3.holdingRegisterRead(40) * (0.095);
    TT301_HX406_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(41) * (0.095);
    TT303_HX504_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(42) * (0.095);
    TT306_EJECTOR_STEAM_IN = modbusTCPClient3.holdingRegisterRead(43) * (0.095);
    TT313_HX402_STEAM_OUT = modbusTCPClient3.holdingRegisterRead(44) * (0.095);
    TT319_HX402_STEAM_SYSTEM = modbusTCPClient3.holdingRegisterRead(45) * (0.095);
    TT407_STEAM_REFORMER_OUT_LREF = modbusTCPClient3.holdingRegisterRead(46) * (0.095);
    TT408_HTS_IN_LREF = modbusTCPClient3.holdingRegisterRead(47) * (0.095);

    TT410_HTS_OUT_LREF = modbusTCPClient4.holdingRegisterRead(40) * (0.095);
    TT411_FPZ_OUT_LREF = modbusTCPClient4.holdingRegisterRead(41) * (0.095);
    TT430_SMR_TUBES_INLET = modbusTCPClient4.holdingRegisterRead(42) * (0.095);
    TT511_SILICON_CARBIDE_OUT = modbusTCPClient4.holdingRegisterRead(43) * (0.095);
    TT512_SILICON_CARBIDE_OUT = modbusTCPClient4.holdingRegisterRead(44) * (0.095);
    TT513_HX504_IN = modbusTCPClient4.holdingRegisterRead(45) * (0.095);
    TT514_HX504_OUT = modbusTCPClient4.holdingRegisterRead(46) * (0.095);
    TT441_SMR_TUBE1_OUT = modbusTCPClient4.holdingRegisterRead(47) * (0.095);

    TT442_SMR_TUBE2_OUT = modbusTCPClient5.holdingRegisterRead(40) * (0.095);
    TT443_SMR_TUBE3_OUT = modbusTCPClient5.holdingRegisterRead(41) * (0.095);
    TT444_SMR_TUBE4_OUT = modbusTCPClient5.holdingRegisterRead(42) * (0.095);
    TT445_SMR_TUBE5_OUT = modbusTCPClient5.holdingRegisterRead(43) * (0.095);
    TT446_SMR_TUBE6_OUT = modbusTCPClient5.holdingRegisterRead(44) * (0.095);
    TT447_SMR_TUBE7_OUT = modbusTCPClient5.holdingRegisterRead(45) * (0.095);
    TT448_SMR_TUBE8_OUT = modbusTCPClient5.holdingRegisterRead(46) * (0.095);
    TT449_SMR_TUBE9_OUT = modbusTCPClient5.holdingRegisterRead(47) * (0.095);

    //analog outputs
    modbusTCPClient1.holdingRegisterWrite(40, OFF); //write channel 0 (BLWRSpeed)
    modbusTCPClient1.holdingRegisterWrite(41, OFF); //write channel 1 (WP_Speed)
    modbusTCPClient1.holdingRegisterWrite(42, OFF); //write channel 2 (FCV134)
    modbusTCPClient1.holdingRegisterWrite(43, OFF); //write channel 3 (FCV205)
    modbusTCPClient2.holdingRegisterWrite(40, OFF); //write channel 0 (FCV141)

    //digital outputs
    modbusTCPClient7.coilWrite(5, OFF); //XV801
    modbusTCPClient7.coilWrite(9, 1); //BLWR_EN ON bc opposite
    modbusTCPClient7.coilWrite(10, 1); //WP_EN ON bc opposite
    modbusTCPClient7.coilWrite(6, OFF); //TWV308
    modbusTCPClient7.coilWrite(4, OFF); //XV1100
    modbusTCPClient7.coilWrite(3, OFF); //XV501
    modbusTCPClient7.coilWrite(8, OFF); //BMM_CR2
    modbusTCPClient7.coilWrite(7, OFF); //TWV901
    modbusTCPClient7.coilWrite(2, OFF); //xv909

    FCV134_BURNER_FUEL_FLOW_FB = modbusTCPClient6.inputRegisterRead(7);// read Channel 7 FCV134
    if (!(FCV134_BURNER_FUEL_FLOW_FB < 10 )) {
      ERROR = 2;
      FSM_STATE = INITIALIZE;
      SENSOR_INTEGRITY_CHECK = false;
    }
    else {
      SENSOR_INTEGRITY_CHECK = true;
    }

    modbusTCPClient1.holdingRegisterWrite(40, 0); //write channel 0 (BLWRSpeed) off

    //get that dang dynamic pressure switch off!!
    BLOWER_SPEED_FEEDBACK = modbusTCPClient6.holdingRegisterRead(0) * (.001); //10volts/10000counts
    while (BLOWER_SPEED_FEEDBACK > 10) {
      modbusTCPClient1.holdingRegisterWrite(40, 0); //write channel 0 (BLWRSpeed) off
      BLOWER_SPEED_FEEDBACK = modbusTCPClient6.holdingRegisterRead(0) * (.001); //10volts/20000counts
      delay(1);
    }
  }
}
void readOCI() {
  // Serial.println("Reading OCI417.");
  OCI_RESULT = NODE.readInputRegisters(OCI_INPUT_STATUS_REGISTER, 2); //address,qty
  //do something with data if read is successful
  if (OCI_RESULT == NODE.ku8MBSuccess)
  {
    OCI_INPUT_STATUS_WORD = NODE.getResponseBuffer(0);
    // Serial.print("OCI INPUT WORD: ");Serial.println(OCI_INPUT_STATUS_WORD,BIN);
    COMBUSTION_PRESSURE_SWITCH = bitRead(OCI_INPUT_STATUS_WORD, 7); //COMBUSTION AIR SW

    OCI_OUTPUT_STATUS_WORD = NODE.getResponseBuffer(1);
    //Serial.print("OCI OUTPUT WORD: ");Serial.println(OCI_OUTPUT_STATUS_WORD,BIN);
    BMM_PROOF_OF_FLAME = bitRead(OCI_OUTPUT_STATUS_WORD, 0);
    if (FSM_STATE > BMM_IGNITION && BMM_PROOF_OF_FLAME == false) {
      FSM_STATE = INITIALIZE;
      ERROR = 10;
    }
    BMM_ALARM_STATUS = bitRead(OCI_OUTPUT_STATUS_WORD, 1);
    if (FSM_STATE > BMM_IGNITION && BMM_ALARM_STATUS == true) {
      FSM_STATE = INITIALIZE;
      ERROR = 10;
    }
    //OCI_TO_BMM_COM=bitRead(OCI_OUTPUT_STATUS_WORD,2);
    //  if(FSM_STATE>6 && OCI_TO_BMM_COM==true){FSM_STATE=0;ERROR=10;}
  }
}
void readBtn() {
  //read if there has been a change for the inputs
  //function for reading buttons and signaling indicators

  //check status of inputs xor last di status woth current di status
  if (!modbusTCPClient8.connected()) {
    modbusTCPClient8.begin(serverIOEX8, 502);
  }
  CURRENT_DI_STATUS_WORD = modbusTCPClient8.inputRegisterRead(48);

  DUN_PSL = bitRead(CURRENT_DI_STATUS_WORD, 4);
  if (DUN_PSL) {
    ERROR = 36;
  }
  DUN_PSH = bitRead(CURRENT_DI_STATUS_WORD, 3);
  if (DUN_PSH) {
    ERROR = 37;
  }
  DUN_ZSL = bitRead(CURRENT_DI_STATUS_WORD, 5);
  if (!DUN_ZSL && FSM_STATE > BMM_IGNITION) {
    ERROR = 38;
  }

  DI_STATUS_CHANGE = CURRENT_DI_STATUS_WORD ^ LAST_DI_STATUS_WORD; //xor to check for a change in inputs
  Serial.print("CURRENT_DI_STATUS_WORD: "); Serial.println(CURRENT_DI_STATUS_WORD);
  Serial.print("DI_STATUS_CHANGE: "); Serial.println(DI_STATUS_CHANGE);
  LAST_DI_STATUS_WORD = CURRENT_DI_STATUS_WORD;


  if (DI_STATUS_CHANGE ) { //check for a change in inputs

    //estop
    if (bitRead(DI_STATUS_CHANGE, 0)) {
      if (bitRead(CURRENT_DI_STATUS_WORD, 0)) { //estop
        ESTOP_FLAG = true; GRN_BTN_FLAG = false; AMB_BTN_FLAG = false; ERROR = 1;
        modbusTCPClient7.coilWrite(0, OFF); //green pilot OFF
        modbusTCPClient7.coilWrite(0, OFF); //AMB pilot OFF
      }
      else {
        ESTOP_FLAG == false;
      }
    }
    if (ESTOP_FLAG == false) {

      //amb button
      if (bitRead(DI_STATUS_CHANGE, 2)) {
        if (bitRead(CURRENT_DI_STATUS_WORD, 2) && FSM_STATE == STABILIZE_MODE) {
          AMB_BTN_FLAG = true;
          //modbusTCPClient7.coilWrite(0,0);//grn pilot light
          modbusTCPClient7.coilWrite(7, 1); //twv901 switch reformate to PSA
          modbusTCPClient7.coilWrite(1, 1); //amb pilot light ON
        }
        else {}
      }

      //grn btn
      if (bitRead(DI_STATUS_CHANGE, 1)) {
        if (bitRead(CURRENT_DI_STATUS_WORD, 1)) {
          GRN_BTN_FLAG = true;
          modbusTCPClient7.coilWrite(0, 1); //grn pilot
          //modbusTCPClient7.coilWrite(2,0);//amb pilot
        }
      }
    }
  }
}
void error_Checker() {
  //may be redundant but for safety
  //shut gas down if an issue

  if (ERROR) {
    Serial.println("ERROR CHECKER TRIGGERED!");

    GRN_BTN_FLAG = false; AMB_BTN_FLAG = false; ESTOP_FLAG = true;

    if (!modbusTCPClient1.connected()) {
      modbusTCPClient1.begin(serverIOEX1, 502);
    }
    modbusTCPClient1.holdingRegisterWrite(42, OFF); //write channel 2 (FCV134)
    if (!modbusTCPClient2.connected()) {
      modbusTCPClient2.begin(serverIOEX2, 502);
    }
    modbusTCPClient1.holdingRegisterWrite(40, OFF); //write channel 4 (FCV141)

    if (!modbusTCPClient7.connected()) {
      modbusTCPClient7.begin(serverIOEX7, 502);
    }
    modbusTCPClient7.coilWrite(6, OFF); //TWV308
    modbusTCPClient7.coilWrite(4, OFF); //XV1100
    modbusTCPClient7.coilWrite(3, OFF); //XV501
    modbusTCPClient7.coilWrite(8, OFF); //BMM_CR2
    modbusTCPClient7.coilWrite(7, OFF); //TWV901
    modbusTCPClient7.coilWrite(2, ON); //CH4 digital output xv909


  }
  else {
    ESTOP_FLAG = false;
  }


}
void steamPressureLow() {

}
void monitor_SR_Tube_Temps() {


}
void blinkGRN() {

  if (CURRENT_MILLIS - PREVIOUS_MILLIS_5 >= 500 && GRN_BTN_FLAG == false && ESTOP_FLAG == false) {
    PREVIOUS_MILLIS_5 = millis();
    if (GRN_PLT_STATE == false) {
      GRN_PLT_STATE = true;
    }
    else {
      GRN_PLT_STATE = false;
    }
    modbusTCPClient7.coilWrite(0, GRN_PLT_STATE); //grn pilot
  }
}
void blinkAMB() {
  if (CURRENT_MILLIS - PREVIOUS_MILLIS_6 >= 500 && AMB_BTN_FLAG == false && ESTOP_FLAG == false) {
    PREVIOUS_MILLIS_6 = millis();
    if (AMB_PLT_STATE == false) {
      AMB_PLT_STATE = true;
    }
    else {
      AMB_PLT_STATE = false;
    }
    modbusTCPClient7.coilWrite(1, AMB_PLT_STATE); //grn pilot
  }

}
void DB_RX() {
  /*regRX = telGetValue(TEL_ADDR, REGRX);
    if (regRX) {
    telWriteValue(TEL_ADDR, REGRX, 0x00);
    regRX = telGetValue(TEL_ADDR, REGRX);
    SUPERHEAT_TIMER = telGetValue(TEL_ADDR, SHTMR);
    BMM_OFF_TIMER = telGetValue(TEL_ADDR, BOTMR);
    BMM_START_TIMER = telGetValue(TEL_ADDR, BSTMR);
    BMM_PURGE_TIMER = telGetValue(TEL_ADDR, BPTMR);
    BMM_IGNITION_TIMER = telGetValue(TEL_ADDR, BCTMR);
    BURNER_RAMP_TIMER = telGetValue(TEL_ADDR, BRTMR);
    BURNER_REACH_END_TIMER = telGetValue(TEL_ADDR, BETMR);
    STEAM_GENERATION_TIMER = telGetValue(TEL_ADDR, SGTMR);
    STEAM_AT_170PSI_TIMER = telGetValue(TEL_ADDR, SPTMR);
    OPEN_SR_FUEL_TIMER = telGetValue(TEL_ADDR, SRTMR);
    BLOWER_PURGE_SPEED = telGetValue(TEL_ADDR, BLPSD);
    BLOWER_IGNITION_SPEED = telGetValue(TEL_ADDR, BLISD);
    BLOWER_RAMP_END = telGetValue(TEL_ADDR, BLEND);
    BLOWER_TOP_SPEED = telGetValue(TEL_ADDR, BLTSD);
    RO_PUMP_AT_10_GRAMS_PER_SEC = telGetValue(TEL_ADDR, WP10G);
    RO_PUMP_TOP_SPEED = telGetValue(TEL_ADDR, WPTSD);
    FCV205_AT_35_PERCENT = telGetValue(TEL_ADDR, F205S);
    FCV205_AT_50_PERCENT = telGetValue(TEL_ADDR, F205E);
    FCV134_BURNER_FUEL_FLOW_IGNITION = telGetValue(TEL_ADDR, BFIGN);
    FCV134_BURNER_FUEL_FLOW_RAMP_END = telGetValue(TEL_ADDR, BFRED);
    FCV141_SR_FUEL_START_PERCENT = telGetValue(TEL_ADDR, SRFST);
    FT132_PIPE_DIA_CONV = telGetValue(TEL_ADDR, FTDIA);
    FT132_COUNTS_TO_G_PER_SEC = telGetValue(TEL_ADDR, FTGPS);
    FT132_4MA_OFFSET = telGetValue(TEL_ADDR, FT4MA);
    BURNER_TEMP_RAMP_END = telGetValue(TEL_ADDR, BTRED);
    BURNER_TEMP_CROSSOVER = telGetValue(TEL_ADDR, BTCOV);
    SR_FUEL_CUT = telGetValue(TEL_ADDR, SRFCT);
    }*/

}
void DB_TX() {
  /*
    //motor fb,flow fb,fcv position fb
    telWriteValue(TEL_ADDR, BL_FB, BLOWER_SPEED_FEEDBACK);
    telWriteValue(TEL_ADDR, WP_FB, RO_PUMP_FEEDBACK);
    telWriteValue(TEL_ADDR, BF_FB, FCV134_BURNER_FUEL_FLOW_FB);
    telWriteValue(TEL_ADDR, SR_FB, FT132_ADJUSTED_MEASURE);
    //pressure transducers
    telWriteValue(TEL_ADDR, PT213, PT213_RO_PRESSURE);
    telWriteValue(TEL_ADDR, PT318, PT318_HX406_OUTPUT_PRESSURE);
    telWriteValue(TEL_ADDR, PT420, PT420_STEAM_EJECTOR_PRESSURE);
    telWriteValue(TEL_ADDR, PT304, PT304_TWV308_INPUT_PRESSURE);
    //thermocouples
    telWriteValue(TEL_ADDR, TT142, TT142_SR_FUEL);
    telWriteValue(TEL_ADDR, TT301, TT301_HX406_STEAM_OUT);
    telWriteValue(TEL_ADDR, TT303, TT303_HX504_STEAM_OUT);
    telWriteValue(TEL_ADDR, TT306, TT306_EJECTOR_STEAM_IN);
    telWriteValue(TEL_ADDR, TT313, TT313_HX402_STEAM_OUT);
    telWriteValue(TEL_ADDR, TT319, TT319_HX402_STEAM_SYSTEM);
    telWriteValue(TEL_ADDR, TT407, TT407_STEAM_REFORMER_OUT_LREF);
    telWriteValue(TEL_ADDR, TT408, TT408_HTS_IN_LREF);
    telWriteValue(TEL_ADDR, TT410, TT410_HTS_OUT_LREF);
    telWriteValue(TEL_ADDR, TT411, TT411_FPZ_OUT_LREF);
    telWriteValue(TEL_ADDR, TT430, TT430_SMR_TUBES_INLET);
    telWriteValue(TEL_ADDR, TT511, TT511_SILICON_CARBIDE_OUT);
    telWriteValue(TEL_ADDR, TT512, TT512_SILICON_CARBIDE_OUT);
    telWriteValue(TEL_ADDR, TT513, TT513_HX504_IN);
    telWriteValue(TEL_ADDR, TT514, TT514_HX504_OUT);
    telWriteValue(TEL_ADDR, TT441, TT441_SMR_TUBE1_OUT);
    telWriteValue(TEL_ADDR, TT442, TT442_SMR_TUBE2_OUT);
    telWriteValue(TEL_ADDR, TT443, TT443_SMR_TUBE3_OUT);
    telWriteValue(TEL_ADDR, TT444, TT444_SMR_TUBE4_OUT);
    telWriteValue(TEL_ADDR, TT445, TT445_SMR_TUBE5_OUT);
    telWriteValue(TEL_ADDR, TT446, TT446_SMR_TUBE6_OUT);
    telWriteValue(TEL_ADDR, TT447, TT447_SMR_TUBE7_OUT);
    telWriteValue(TEL_ADDR, TT448, TT448_SMR_TUBE8_OUT);
    telWriteValue(TEL_ADDR, TT449, TT449_SMR_TUBE9_OUT);
    //oci to bmm interface
    telWriteValue(TEL_ADDR, OCIIN, OCI_INPUT_STATUS_WORD);
    telWriteValue(TEL_ADDR, OCIOT, OCI_OUTPUT_STATUS_WORD);*/

}
void DB_INIT() {
  /*
    telWriteValue(TEL_ADDR, REGRX, regRX);
    telWriteValue(TEL_ADDR, SHTMR, SUPERHEAT_TIMER);
    telWriteValue(TEL_ADDR, BOTMR, BMM_OFF_TIMER);
    telWriteValue(TEL_ADDR, BSTMR, BMM_START_TIMER);
    telWriteValue(TEL_ADDR, BPTMR, BMM_PURGE_TIMER);
    telWriteValue(TEL_ADDR, BCTMR, BMM_IGNITION_TIMER);
    telWriteValue(TEL_ADDR, BRTMR, BURNER_RAMP_TIMER);
    telWriteValue(TEL_ADDR, BETMR, BURNER_REACH_END_TIMER);
    telWriteValue(TEL_ADDR, SGTMR, STEAM_GENERATION_TIMER);
    telWriteValue(TEL_ADDR, SPTMR, STEAM_AT_170PSI_TIMER);
    telWriteValue(TEL_ADDR, SRTMR, OPEN_SR_FUEL_TIMER);
    telWriteValue(TEL_ADDR, BLPSD, BLOWER_PURGE_SPEED);
    telWriteValue(TEL_ADDR, BLISD, BLOWER_IGNITION_SPEED);
    telWriteValue(TEL_ADDR, BLEND, BLOWER_RAMP_END);
    telWriteValue(TEL_ADDR, BLTSD, BLOWER_TOP_SPEED);
    telWriteValue(TEL_ADDR, WP10G, RO_PUMP_AT_10_GRAMS_PER_SEC);
    telWriteValue(TEL_ADDR, WPTSD, RO_PUMP_TOP_SPEED);
    telWriteValue(TEL_ADDR, F205S, FCV205_AT_35_PERCENT);
    telWriteValue(TEL_ADDR, F205E, FCV205_AT_50_PERCENT);
    telWriteValue(TEL_ADDR, BFIGN, FCV134_BURNER_FUEL_FLOW_IGNITION);
    telWriteValue(TEL_ADDR, BFRED, FCV134_BURNER_FUEL_FLOW_RAMP_END);
    telWriteValue(TEL_ADDR, SRFST, FCV141_SR_FUEL_START_PERCENT);
    telWriteValue(TEL_ADDR, FTDIA, FT132_PIPE_DIA_CONV);
    telWriteValue(TEL_ADDR, FTGPS, FT132_COUNTS_TO_G_PER_SEC);
    telWriteValue(TEL_ADDR, FT4MA, FT132_4MA_OFFSET);
    telWriteValue(TEL_ADDR, BTRED, BURNER_TEMP_RAMP_END);
    telWriteValue(TEL_ADDR, BTCOV, BURNER_TEMP_CROSSOVER);
    telWriteValue(TEL_ADDR, SRFCT, SR_FUEL_CUT);
  */
}
void preTransmission() {
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}
void postTransmission() {
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

/*

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.