Analog input spikes

I am using a Mega 2560,
Connected to analog input pins are 4-20 ma sensors that is fed from 12vdc via 250 Ohm as a bridge divider for voltage measurement on input. This a straight conversion to 0-5vdc.

There is a 10k inline for the just in case. However after logging the values over some time there seems to be a sudden spike in measurement.
See from point 300.

I also have connected a sim 900C for sending sms upon triggers, Sd+RTC module for datalogging purposes, steady 5vdc 5Amp buck as PSU fed from 24Vdc.
Sensors are fed from a 2amp 12vDC buck.

Any ideas what can be done to smooth the spikes out. the purpose of the project is to detect sudden spikes and send a notification via sms.
It works very well except for the spikes that cause falls alarms.

I have used the same application on a Nano, Leonardo & simduino, For some reason the mega seems to be the one most constant in measuring analog input samples.

Please read the topic telling how to get the best out if this forum.
Post at least the code and schematics, not Fritzing pictures.

Can you tell the difference between the "real" and "false" spikes? if you can tell the difference maybe you can make the software tell the difference...

Its highly unlikely to be the Arduino itself. I'm sure there is a noise spike in the analog signal. Or, there's a slight possibility of a software problem.

Thanks for the response @ DVDdoug, So all the noise/ spikes on the graph is false as the load connected to the transducer is static. The line should be smooth and consistent. I have now installed a capacitor over the signal input 100uf and busy with a data log. if I connect the sensor to a 4-20 ma to 0-5 converter, The signal to the Arduino is much better. if i connect the sensor to a PLC, the signal is at its best.
However using the bridge divider method and converting it straight to 0-5vdc, its more cost effective.

Im sure a filter code could work, i just dont know how it would be structured.

Below is my code related to the analog.


#include <SoftwareSerial.h>

//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(11, 18); //SIM800L Tx 18 and 19 rx
// 18 is transmitting
// 19 is not recieving
// pin 11 is the new Rx port now



#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"


//********************************
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

const int chipSelect = 10;
String dataString = "";
String Timelog = "";
String LogString = "";
//*********************************

  String sensorValue1Str = "";
  String sensorValue2Str = "";
  String comma = " ; " ;
  float Sensor1scaled = 0;
  float Sensor2scaled = 0;

// These constants won't change:
const int sensorPin1 = A0;    // analog pin connected to decanting valve
const int sensorPin2 = A1;    // analog pin connected to drain valve
const int RelayOUT1 = A12;      // relay output pin to activate i-CAM
const int RelayOUT2 = A13;      // relay output pin to activate i-CAM

// variables:
//float sensorValue1 = 0;         // the sensor value
//float sensorValue2 = 0;         // the sensor value
float sensorHigh = 0;
float sensorLow  = 0;
float sensorHighT = 0;
float sensorLowT = 0;
float Scaling = 25.575;         // this value is modified to compensate for the pressure transducer range 1023/40bar = 25.575
float scalehigh  = 0.3;         // this is to scale the upper and trigger limit for decanting sensor " adjust sensitivity"
float scalelow = 0.3;           // this is to scale the lower and trigger limit for decanting sensor "adjust sensitivity"
float atmosphereP = 0;          //compensates for atmosphere pressure 0.8Bar in JHB


bool  Trigger1 = 0;            // gsm trigger
bool  Trigger2 = 0;            // gsm trigger
bool SendSMS = 0;              // trigger to start sensing sms
bool SMSsent = 0;              // interlock for sms already sent to prevent duplicate sms sending
bool SendSMS2 = 0;              // trigger to start sensing sms
bool SMSsent2 = 0;              // interlock for sms already sent to prevent duplicate sms sending
bool NormalCondition1 = 0;     // veryfy that condition is normal
bool NormalCondition2 = 0;     // veryfy that condition is normal
bool sensorFault1 = 0;       
bool sensorFault2 = 0;
bool smssentFault1 = 0;
bool smssentFault2 = 0;
bool countingdone1 = 0;
bool countingdone2 = 0;
bool connected1 = 0;
bool startup = 0;
bool SDstatus = 0;


int counting2 = 0;
int counting1 = 0;
int Checktime1 = 0;
int Checktime2 = 0;

String UpdatedTimestr = "";    // string used to update the time and store last value recieved
//String LastTIMEstr = "";
String LastTIMEstrF1 = "";
String LastTIMEstrF2 = "";
String LastTIMEstrF3 = "";
String LastTIMEstrF4 = "";
bool CheckComms = 0;
bool CheckReg = 0;
bool SignalStat = 0;
bool resend_smsFaulty1 = 0;
bool resend_smsFaulty2 = 0;
bool  resend_smsSensor1 = 0;
bool   resend_smsSensor2 = 0;
bool skipdelay = 0;
//char Time[] = " ";
String NetworkREG = "";
String NetworkREGCUT = "";
String COMMS = "";
String UpdatedTimestrCUT = "";
String  SignalValCUT = "";
String  SignalVal = "";
//String COMMSCUT = "";






///////////gsm - gsm - gsm - gsm - gsm - gsm - gsm /////////////
//#define PHONE_NUMBER1 "****************"  // 
//#define PHONE_NUMBER2 "******************"  // 
//#define PHONE_NUMBER3 "************"  // 
//#define PHONE_NUMBER4 "************"  // 

#define MESSAGE1  "*****************************" 
#define MESSAGE2  "*****************************"
#define MESSAGE3  "*****************************"
#define MESSAGE4  "*****************************"
#define MESSAGE5  ""*****************************"
#define MESSAGE6  "*****************************"
#define MESSAGE7  "*****************************"
#define MESSAGE8  "*****************************"
#define MESSAGE9  "*****************************"
#define MESSAGE10  "*****************************" 
#define MESSAGE11 "*****************************"
#define MESSAGE12  "*****************************"
#define MESSAGE13  "*****************************" 
#define MESSAGE14  "*****************************" 
#define MESSAGE15  "*****************************"
#define SERAILNO    "*****************************"



void setup()
{
  Serial.begin(9600);//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  mySerial.begin(9600);//Begin serial communication with Arduino and SIM800L
  
  //pinMode(10, OUTPUT);  digitalWrite(10, LOW);    //to ensure the cs SD pin is LOW
  pinMode(RelayOUT1, OUTPUT);   digitalWrite(RelayOUT1, HIGH);// Output used for the alarm trigger via Relay 
  pinMode(RelayOUT2, OUTPUT);   digitalWrite(RelayOUT2, HIGH);// Output used for the alarm trigger via Relay 
  
  rtc.begin();  

  delay(10000);  //allow time for modem to startup  

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");       }
    
    Serial.println("card initialized.");            
    delay(500);   
    WriteSDRawValues();
     
    digitalWrite(RelayOUT1, LOW); 
    digitalWrite(RelayOUT2, LOW);

  Serial.println(F("Running startup diagnostics"));
  checkAT();
  delay(5000);
  SetSaveCLK();
  delay(10000);
  GetModemTime(); 
  delay(5000);
  Serial.println(F("Starting up system"));

  delay(1000);
  Serial.println(F("modem should be ready now"));
  
  //---------------------------
   checkAT();
   checkSIGNAL(); 
   checkSIMINFO();
   checkREGISTER();
   resetCN();
   //Gettimelive(); 
   
  GetModemTime();
  delay(500);
  adjustTime();
  delay(500);
  Datetimeinfo();
 //rtc.adjust(DateTime(2021, 9, 14, 20, 33, 0));
 
  
  Serial.println(F("sending startup sms NOW..."));  delay(5000);
  checkAT();
  mySerial.println(F("AT+CMGF=1")); // Configuring TEXT mode
  updateSerial();
  mySerial.println(F("AT+CMGS=\"+**********************\""));//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  updateSerial();
  mySerial.println(F(MESSAGE1)); //text content
  updateSerial();
  mySerial.println(F(SERAILNO)); //text content
  updateSerial();
  if (SDstatus == 1){ mySerial.println(F("SD is OK")); updateSerial(); }
  if (SDstatus == 0){ mySerial.println(F("SD is Faulty")); updateSerial(); }
  mySerial.println(Timelog);
  updateSerial();
  mySerial.write(26);

 
  delay(3000);
  

}

void loop()
{

  Datetimeinfo();

  ReadSensorsMap();

  WriteSDRawValues();
  
      // removed the below code because we now have live time on the GSM MODULE itself.
      //  if (sensorFault2 == 1){
      if (sensorFault1 == 1 and sensorFault2 == 1){delay(2500);}
      
      Checktime1++;
      Serial.print(F(" counting for checking time VALUE : "));
      Serial.println(Checktime1);
      if (Checktime1 == 120){ Checktime1 = 0;  GetModemTime();  
      Serial.println(F("MODEM TIME UPDATED"));   }    // FOR DEBUGGING} 


   //GetModemTime(); 
   // delay(10);
   //Serial.println(F("MODEM TIME UPDATED")); // FOR DEBUGGING

  
    //Serial.println(F("Scaling sensors")); 
    //delay(2000); // FOR DEBUGGING 
    //ReadSensorsMap();

    Serial.println(F("Checking sensors for Faults")); 
   // delay(1000); // FOR DEBUGGING
    CheckSesnorFaults();
  
    Serial.println(F("Checking sensor 1 ***************"));
   // delay(3000); // FOR DEBUGGING
    CheckSensor1();
    Serial.println(F("Checking sensor 1 *************** COMPLETED "));
   // delay(3000);  // FOR DEBUGGING

    Serial.println(F("Checking sensor 2 ***************"));
   // delay(5000); // FOR DEBUGGING
    CheckSensor2();
    Serial.println(F("Checking sensor 2 *************** COMPLETED"));
    //delay(5000); // FOR DEBUGGING


    startup = 1; //1st loop will initialize the sensor disconnection checking step


    if (resend_smsFaulty1 == 1){  ResendFaulty1();  }
    if (resend_smsFaulty2 == 1){  ResendFaulty2();  }
    if (resend_smsSensor1 == 1){  ResendSensor1();  }
    if (resend_smsSensor2 == 1){  ResendSensor2();  }


}

 void checkAT(){

    int val1 = 0;
    while (val1 < 5 ){
    mySerial.println(F("AT")); //Once the handshake test is successful, it will back to OK
    debugComms();
    val1++;
    if (CheckComms == 1){val1 = 5;}
    Serial.println(F("comms stats : "));
    Serial.println(CheckComms);    
    Serial.print("val1 : ");
    Serial.println(val1);         }

 }

 void SetSaveCLK(){
  mySerial.println(F("AT+CLTS=1;&W")); //set the modem to autosync time and date with network opperator.
  updateSerial(); 
 }

 void GetModemTime(){
    mySerial.println(F("AT+CCLK?")); //get the modem clock time
    UpdatedTIME();
    //updateSerial();
 }

 void checkSIGNAL()
 // mySerial.println(F("AT+CSQ")); //Signal quality test, value range is 0-31 , 31 is the best
 // updateSerial(); 

{
    int sigval = 0;
    while (sigval < 5 ){
    mySerial.println(F("AT+CSQ")); //Request Signal status report from modem
    debugSIGNAL();
    sigval++;
    if (SignalStat == 1){sigval = 5;}
    Serial.print("");
    Serial.print(F("SIGNAL is OK  : "));
    Serial.print(SignalStat);   
    Serial.println(""); 
    Serial.print("sigval : ");
    Serial.print(sigval);   
    Serial.println("");   }
 }
 

 void checkSIMINFO(){
  mySerial.println(F("AT+CCID")); //Read SIM information to confirm whether the SIM is plugged
  updateSerial();
 }

 void checkREGISTER(){
    int val = 0;
    while (val < 5 ){
    mySerial.println(F("AT+CREG?")); //Once the handshake test is successful, it will back to OK
    debugREG();
    val++;
    if (CheckReg == 1){val = 5;}
    Serial.println(F("check network register status : "));
    Serial.println(CheckReg);    
    Serial.print("val : ");
    Serial.println(val);         }
 }

 void resetCN(){
  CheckComms = 0;
  CheckReg = 0;
  SignalStat = 0;
  
 }




 void debugComms() /// checking that coms between controller and modem is working
{   
  COMMS = "";
  
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
   COMMS = mySerial.readString();    
  }

  //while (mySerial.available()){
  //COMMS = mySerial.read();
  //}

  
     Serial.print(F(" Comms DATA recieved["));
     Serial.print(COMMS);
     Serial.println(F("]"));
    
     //COMMSCUT = COMMS.substring(5);
     //Serial.println("Comms Response is [");
     //Serial.print(COMMSCUT);
     //Serial.println("]");
     //if (COMMSCUT == "OK")    { Serial.println("Communication is avaiable");  CheckComms = 1; }
     //if (COMMSCUT == "ERROR") { Serial.println("Communication ERROR Please TRY Again");CheckComms = 0;}
     //if (COMMSCUT == NULL)    { Serial.println("Communication ERROR NULL recieved Please TRY Again");CheckComms = 0; }
 
      if (COMMS.substring(5,7) == "OK"){ Serial.println(F("Communication is avaiable"));  CheckComms = 1; }
    //if (COMMS.substring(5,10) == "ERROR"){ Serial.println(F("Communication ERROR Please TRY Again"));CheckComms = 0;}
      if (COMMS == NULL)    { Serial.println(F("Communication ERROR NULL recieved Please TRY Again"));CheckComms = 0; }
}
         //---------------------------------------------     

        //while(mySerial.available()) 
        //{  
   
        //  Serial.println(mySerial.read());//Forward what Software Serial received to Serial Port



       //   char c = mySerial.read();                
       //   //response+=c; 
       //   if (c=='E')  {  delay(10);  c=mySerial.read();     
       //   if (c=='R')  {  delay(10);  c=mySerial.read();  
       //  if (c=='R')  {  delay(10);  c=mySerial.read();
       //  if (c=='O')  {  delay(10);  c=mySerial.read();
       //  if (c=='R')  {  
       //  CheckComms = 0;
       //Resend_sms();            
       //SerialUSB.println("Resending sms");  // for debugging purposes
       //delay(5000); //for debugging purposes
       //  }  }  }  }  }     
       //  if (c=='O')  {   delay(10); c=mySerial.read();     
      //  if (c=='K')  {   
      //  CheckComms = 1;         
      //SerialUSB.println("OK was recieved"); // for debugiing purposes  
      //delay(5000); // for debugging purposes
      //   }  }
      //-------------------------------------------



       
    
  

//-------------------------------------------------------------

void debugREG() /// checking that system is registered to network

{
  NetworkREGCUT = "";
  NetworkREG = "";
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
   NetworkREG = mySerial.readString();   
  
  }
     Serial.println(F(""));
     Serial.print(F(" Network Register data["));
     Serial.print(NetworkREG);
     Serial.print(F("]"));
     Serial.println(F(""));

     NetworkREGCUT = NetworkREG.substring(18,21);
     Serial.println(F(""));
     Serial.print(F("Network Register STATUS is ["));
     Serial.print(NetworkREGCUT);
     Serial.print(F("]"));
     Serial.println(F(""));
     
     if (NetworkREGCUT == "0,0")  { Serial.println(F("Network is not registered now try again"));  CheckReg = 0; }
     if (NetworkREGCUT == "0,2")  { Serial.println(F("Network is not registered now but SEARCHING"));  CheckReg = 0; }
     if (NetworkREGCUT == "0,1")  { CheckReg = 1; Serial.println(F("Network is  REGISTERED you may proceed with data transmission"));}
     if (NetworkREGCUT == NULL)   { Serial.println(F("Network Register NULL recieved Please try again"));CheckReg = 0; }

           
          //17:08:53.164 ->  Netwrok REG [:
          //17:08:53.164 -> AT+CREG?
          //
          //17:08:53.164 -> +CREG: 0,0
          //17:08:53.198 -> 
          //17:08:53.198 -> OK
          //17:08:53.198 -> ]

}


 


void UpdatedTIME()
{
  UpdatedTimestrCUT = "";
  UpdatedTimestr = "";
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
   UpdatedTimestr = mySerial.readString();   
  
  }
   Serial.print(F("Time string recieved is["));
   Serial.print(UpdatedTimestr);
   Serial.print(F("]"));
    delay(5);
    
   UpdatedTimestrCUT = UpdatedTimestr.substring(19,36);
   Serial.println(F(""));
   Serial.print(F("GSM Time ONLY Value is ["));
   Serial.print(UpdatedTimestrCUT);
   Serial.print(F("]"));
   Serial.println(F(""));
    delay(5);   
    UpdatedTimestr = "";
    delay(5);
    UpdatedTimestr = UpdatedTimestrCUT;
}

//18:51:24.279 -> Time string recieved is[AT+CCLK?

//18:51:24.314 -> +CCLK: "04/01/01,01:56:30+08"
//18:51:24.348 -> 
//18:51:24.348 -> OK
//18:51:24.348 -> ]


//---------------------------------------------------------------------





//-------------------------------------------------------------------------
void debugSIGNAL() /// checking that system is registered to network

{
  SignalValCUT = "";
  SignalVal = "";
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
   SignalVal = mySerial.readString();   
  
  }
     Serial.println(F(""));
     Serial.print(F(" SIGNAL data is ["));
     Serial.print(SignalVal);
     Serial.print(F("]"));
     Serial.println(F(""));

     SignalValCUT = SignalVal.substring(15,18);  // looking for value from position 15 to 18
     Serial.println(F(""));
     Serial.print(F("Signal level is ["));
     Serial.print(SignalValCUT);
     Serial.print(F("]"));
     Serial.println(F(""));
     if (SignalValCUT == "0,0"){Serial.println(F("Network Signal is very POOR please try again"));  SignalStat = 0; }
     if (SignalValCUT == "0,1"){Serial.println(F("Network Signal is very POOR please try again"));  SignalStat = 0; }
     else { SignalStat = 1; Serial.println(F("Signal is avaiable you may proceed"));}
     if (SignalValCUT == NULL)  { Serial.println(F("Network Signal is NULL please try again"));SignalStat = 0; }

}       
//19:08:27.652 -> AT+CSQ

//19:08:27.652 -> +CSQ: 0,0
//19:08:27.652 -> 
//19:08:27.652 -> OK



    //-------------------------------------------------------------------

   void ReadSensorsMap(){
  
  int val1 = analogRead(0);
  val1 = map(val1, 193, 1023, 0, 1023);
  Sensor1scaled = val1/Scaling;
  sensorValue1Str = Sensor1scaled;
  
  int val2 = analogRead(1);
  val2 = map(val2, 193, 1023, 0, 1023);
  Sensor2scaled = val2/Scaling;
  sensorValue2Str = Sensor2scaled;
  
  dataString = String(sensorValue1Str + comma + sensorValue2Str);
  Serial.print(F("( Sensor maped value is : "));
  Serial.print(dataString);
  Serial.println(F(" )"));

  
}

////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSOR 1 BELOW
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////


void CheckSensor1(){
  
  ///////////////sensor1 triggers - sensor1 triggers - sensor1 triggers ///////////////////// dump valve***
  if (sensorFault1 == 0){
    
  //Serial.println(F("Sensor 1 check Started"));
  //ReadSensorsMap();
  //WriteSDRawValues();

  if (Sensor1scaled >= 2){  
  delay(10);
  ReadSensorsMap();
  // Check again if TRUE
  delay(10);  
  if (Sensor1scaled >= 2){
  WriteSDAlarmT1();
  Trigger1 = 1; 
  NormalCondition1 = 1; 
  SendSMS = 1;
  smsSensor1();
  Serial.println(F("sensor 1 activated"));

  }}

if (Trigger1 == 1){
  Serial.println(F("Trigger1 : "));
  Serial.println(Trigger1);
  counting1++;    
  }


if (counting1 == 5) {
    countingdone1 = 1;    }

 

if (Sensor1scaled <= 1.6) { NormalCondition1 = 0; }

if (NormalCondition1 == 0 and Trigger1 == 1 and countingdone1 == 1){
  Serial.println(F("Condition returned for sensor 1 to normal again"));
  SendSMS = 0; SMSsent = 0; Trigger1 = 0;countingdone1 = 0;counting1 = 0; }
  
if (NormalCondition1 == 0){ 
  Serial.println(F("Condition is Normal for sensor 1 and Monitoring status")); }
 }

//Serial.println(F("Sensor 1 check END"));
}


///////////////sensor1 triggers - sensor1 triggers - sensor1 triggers ///////////////////// dump valve ***
////---------------------------------------------------------------------------
////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSOR 1 ABOVE
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////





////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSOR 2 BELOW
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////

void CheckSensor2(){
  ///////////////sensor2 triggers - sensor2 triggers - sensor2 triggers ///////////////////// decanting valve ***
if (sensorFault2 == 0){
    

    Serial.println(F("Sensor 2 check Started"));
    ReadSensorsMap();
    WriteSDRawValues();

    //Adjust the trigger limits beacuse we have a variable trigger limit. 
    sensorHigh = Sensor2scaled + scalehigh;   // upper range trigger
    sensorLow  = Sensor2scaled - scalehigh;   // lower range trigger
    sensorHighT = sensorHigh + scalehigh;    // actual upper trigger range
    sensorLowT = sensorLow - scalelow;      // actual lower trigger range
 

  // REMOVED THE BELOW BECUASE WE NOW HAVE LIVE TIME ON THE MODEM ITSELF
  //skipdelay = 0;
 // Checktime2++;
 // if (Checktime2 == 50){ Checktime2 = 0;  Gettimelive(); skipdelay = 1;  }  
 // if (skipdelay == 0){ delay(2500);  }

  delay(2500);
  ReadSensorsMap();
  

 
      // the Below is the high trigger point, being removed due to sensitivity,
 if (Sensor2scaled >= sensorHighT){
    WriteSDAlarmT2HIGH();
    Serial.print(F("( Trigger2 : "));
    Serial.print(Trigger2);
    Serial.println(F(" )"));
    Serial.println(F("sensor 2 activated high limit reached"));
    //Trigger2 = 1;                      // removed to not trigger on high spikes
    //NormalCondition2 = 1;             // removed to not trigger on high spikes
    //SendSMS2 = 1;                     // removed to not trigger on high spikes
    //smsSensor2();                     // removed to not trigger on high spikes
 }    // the Above is the high trigger point, being removed due to sensitivity,
  

 if (Sensor2scaled <= sensorLowT){
  WriteSDAlarmT2LOW();
  Trigger2 = 1;
  Serial.print(F("( Trigger2 Value is : "));
  Serial.print(Trigger2);
  Serial.println(F(" )"));
  Serial.println(F("sensor 2 activated low limit reached"));  
  NormalCondition2 = 1;
  SendSMS2 = 1;
  smsSensor2();  
  }

if (Trigger2 == 1){
  Serial.print(F("( Trigger2 Value is : ")); 
  Serial.print(Trigger2);
  Serial.println(F(" )"));  
  counting2++;     }

if (counting2 == 5) {   countingdone2 = 1;    }

 if (countingdone2 == 1){
 if (Sensor2scaled >= sensorLowT and Sensor2scaled <= sensorHighT ){
    Serial.println(F("Condition returned to normal again for sensor2"));
    SendSMS2 = 0;
    SMSsent2 = 0;
    Trigger2 = 0;
    NormalCondition2 = 0; 
    counting2 = 0;      
    countingdone2 = 0;   }
    else {  
    counting2 = 0; 
    countingdone2 = 0;}
 }
    
if (NormalCondition2 == 0 and Trigger2 == 0){
  Serial.println(F("Condition is Normal for sensor 2 and Monitoring status"));}
}
Serial.println(F("Sensor 2 check END"));
}


///////////////sensor2 triggers - sensor2 triggers - sensor2 triggers ///////////////////// decanting valve ***
////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSOR 2 ABOVE
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////






////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSORS FOR DISCONECTION BELOW
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////


void CheckSesnorFaults(){
  /// Sensor 1 and 2 disconnection monitoring ///***
if (startup == 1 ){                        
if(Sensor1scaled <= -2.5 and smssentFault1 == 0) {  sensorFault1 = 1  ; WriteSDdisconnect1();  smsFaulty1();   delay(2500); }
if(Sensor2scaled <= -2.5 and smssentFault2 == 0) {  sensorFault2 = 1  ; WriteSDdisconnect2();  smsFaulty2();   delay(2500); }


  if(smssentFault1 == 1){
  if(Sensor1scaled >= -0.50){   sensorFault1 = 0;  smssentFault1 = 0;  }  }

  if(smssentFault2 == 1){
  if(Sensor2scaled >= -0.50){   sensorFault2 = 0;  smssentFault2 = 0;  }  }
}
/// Sensor 1 and 2 disconnection monitoring ///***
}


////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE FUNCTION TO CHECK SENSORS FOR DISCONECTION ABOVE
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////

void Datetimeinfo(){

    String Year = "";
    String Month = "";
    String Day = "";
    String Dayname = "";
    String Hour = "";
    String Min = "";
    String Second = "";
    String comma = "/" ;
    String doubleteken = ":" ;
    
    DateTime now = rtc.now();

    Year = (now.year());
    Month = (now.month());
    Day = (now.day());
    Dayname = (daysOfTheWeek[now.dayOfTheWeek()]);
    Hour = (now.hour());
    Min = (now.minute());
    Second = (now.second());
    Timelog = String(Year + comma + Month + comma + Day + " " + "(" + Dayname + ")" + " " + Hour + doubleteken + Min + doubleteken + Second);
    Serial.println(Timelog);
   /*
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    // For debugging only //
   */ 

    //delay(2000);
}


void adjustTime(){

    String Year = "";
    String Month = "";
    String Day = "";
    String Dayname = "";
    String Hour = "";
    String Min = "";
    String Second = "";
    String comma = "/" ;
    String doubleteken = ":" ; 
    int dayint = "";
    int monthint = "";
    int yearint = "";
    int hourint = "";
    int minint = "";
    int secondint = "";


   //myString.toInt()




   //GSM Time ONLY Value is [21/09/28,16:32:28]

   Year = UpdatedTimestrCUT.substring(0,4);
   yearint = Year.toInt();
   yearint = yearint + 2000;
   Serial.print(" Year value is : ");
   Serial.println(yearint);
   delay(500);
   
   Month = UpdatedTimestrCUT.substring(3,6);
   monthint = Month.toInt();
   Serial.print(" Month value is : ");
   Serial.println(monthint);
   delay(500);

   Day = UpdatedTimestrCUT.substring(6,8);
   dayint = Day.toInt();
   Serial.print(" Day value is : ");
   Serial.println(dayint);
   delay(500);

   Hour = UpdatedTimestrCUT.substring(9,12);
   hourint = Hour.toInt();
   Serial.print(" Hour value is : ");
   Serial.println(hourint);
   delay(500);

   Min = UpdatedTimestrCUT.substring(12,14);
   minint = Min.toInt();
   Serial.print(" Minnute value is : ");
   Serial.println(minint);
   delay(500);

   Second = UpdatedTimestrCUT.substring(15,18);
   secondint = Second.toInt();
   Serial.print(" Second value is : ");
   Serial.println(secondint);
   delay(500);
  
  rtc.adjust(DateTime(yearint, monthint, dayint, hourint, minint, secondint));




  
}
/*
17:44:25.242 -> GSM Time ONLY Value is [21/09/28,17:44:24]

17:44:33.814 -> >  Year value is : 2021
17:44:34.308 ->  Month value is : 9
17:44:34.801 ->  Day value is : 28
17:44:35.295 ->  Hour value is : 17
17:44:35.836 ->  Minnute value is : 44
17:44:36.331 ->  Second value is : 24
17:44:39.833 -> 2021/9/28 (Tuesday) 17:44:27



*/

void WriteSDRawValues(){

   LogString = String(dataString + comma + Timelog);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(LogString);
    delay(300);
    dataFile.close();
    delay(50);
    // print to the serial port too:
    Serial.print(F("( this is the data log saved to SD card : "));
    Serial.print(LogString);
    Serial.println(F(" )"));
    SDstatus = 1; 
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    SDstatus = 0;
  } 
}





void WriteSDGSMstatus(){

  String GSMstatusStr = "";
  
  GSMstatusStr = String("SignalVal" + comma + SignalValCUT + comma + "networkStauts" + comma + NetworkREGCUT + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("GSMstatus.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(GSMstatusStr);
    delay(200);
    dataFile.close();
    delay(10);
    // print to the serial port too:
    Serial.print(F("( this is the GSM status log saved to SD card : "));
    Serial.print(GSMstatusStr);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}



void WriteSDAlarmT1(){

  String AlarmT1 = "";

  AlarmT1 = String("Trigger 1 activated (dump valve)" + comma + "sensorValue" + comma + Sensor1scaled + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("AlarmLOG.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(AlarmT1);
    delay(1000);
    dataFile.close();
    delay(250);
    // print to the serial port too:
    Serial.print(F("( this is the AlarmT1 status log saved to SD card : "));
    Serial.print(AlarmT1);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void WriteSDAlarmT2LOW(){

  String AlarmT2LOW = "";

  AlarmT2LOW = String("Trigger 2 activated (decant valve LOW)" + comma + "sensorValue" + comma + Sensor2scaled + comma + "tigger Value" + comma + sensorLowT + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("AlarmLOG.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(AlarmT2LOW);
    delay(1000);
    dataFile.close();
    delay(250);
    // print to the serial port too:
    Serial.print(F("( this is the AlarmT2 status log saved to SD card : "));
    Serial.println(AlarmT2LOW);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void WriteSDAlarmT2HIGH(){

  String AlarmT2HIGH = "";

  AlarmT2HIGH = String("Trigger 2 activated (decant valve HIGH)" + comma + "sensorValue" + comma + Sensor2scaled + comma + "tigger Value" + comma + sensorHighT + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("AlarmLOG.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(AlarmT2HIGH);
    delay(1000);
    dataFile.close();
    delay(250);
    // print to the serial port too:
    Serial.print(F("( this is the AlarmT2 status log saved to SD card : "));
    Serial.println(AlarmT2HIGH);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}


void WriteSDdisconnect1(){

  String AlarmDisconnect1 = "";

  AlarmDisconnect1 = String("Sensor 1 Disconnected Dump valve" + comma + "sensorValue" + comma + Sensor1scaled + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("AlarmLOG.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(AlarmDisconnect1);
    delay(1000);
    dataFile.close();
    delay(250);
    // print to the serial port too:
    Serial.print(F("( this is the disconnect 1 status log saved to SD card : "));
    Serial.println(AlarmDisconnect1);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}


void WriteSDdisconnect2(){

  String AlarmDisconnect2 = "";

  AlarmDisconnect2 = String("Sensor 2 Disconnected decant line" + comma + "sensorValue" + comma + Sensor2scaled + comma + Timelog);
    
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("AlarmLOG.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(AlarmDisconnect2);
    delay(1000);
    dataFile.close();
    delay(250);
    // print to the serial port too:
    Serial.print(F("( this is the disconnect 1 status log saved to SD card : "));
    Serial.println(AlarmDisconnect2);
    Serial.println(F(" )"));
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    
  }
  while (mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
    
  }
}

void smsFaulty1(){

  if (sensorFault1 == 1 and smssentFault1 == 0) {
   digitalWrite(RelayOUT1, HIGH);
   digitalWrite(RelayOUT2, HIGH);  
   resetCN();  
   checkAT();  
   checkSIGNAL();   
   checkSIMINFO();  
   checkREGISTER();
   
   
   if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){  
  UpdatedTimestr = ""; delay(100);  
  GetModemTime();
  

  
  Serial.println(F("Sensor 1 has been disconnected"));
  SMSstep1();
  mySerial.print(F(MESSAGE13)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();mySerial.print(UpdatedTimestr); updateSerial();
  mySerial.write(26);Serial.println(F("sms sent for sensor 1 faulty condition")); 
  //sensorFault1 = 0;
  smssentFault1 = 1;
  delay(1500);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);                  
  resend_smsFaulty1 = 0;      }
  
if (CheckReg == 0 or SignalStat == 0){  
  LastTIMEstrF1 = "";
  resend_smsFaulty1 = 1; 
  delay(3000);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);
  //sensorFault1 = 0;
  smssentFault1 = 1;           
  LastTIMEstrF1 =  UpdatedTimestr;     
  Serial.println(F("Message could NOT be sent try again ** sensorFault 1 **"));
    }
   resetCN();
   digitalWrite(RelayOUT1, LOW);
   digitalWrite(RelayOUT2, LOW);
      }
          }

//---------------------------------------------------------------

void smsFaulty2(){
  if (sensorFault2 == 1 and smssentFault2 == 0) {
  digitalWrite(RelayOUT1, HIGH); 
  digitalWrite(RelayOUT2, HIGH);  
  resetCN(); 
  checkAT();  
  checkSIGNAL();   
  checkSIMINFO();  
  checkREGISTER();

  if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){  
    
  UpdatedTimestr = ""; delay(100);  
  GetModemTime();
  
  Serial.println(F("Sensor 2 has been disconnected"));
  SMSstep1(); 
  mySerial.print(F(MESSAGE14)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();  mySerial.print(UpdatedTimestr); updateSerial();
  mySerial.write(26);Serial.println(F("sms sent for sensor 2 faulty condition")); 
  //sensorFault2 = 0;
  smssentFault2 = 1;
  delay(1500);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);                        
  resend_smsFaulty2 = 0;      }


if (CheckReg == 0 or SignalStat == 0){ 
  LastTIMEstrF2 = ""; 
  resend_smsFaulty2 = 1; 
  delay(3000);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);
  //sensorFault2 = 0;
  smssentFault2 = 1;               
  LastTIMEstrF2 =  UpdatedTimestr;    
  Serial.println(F("Message could NOT be sent try again ** sensorFault 2 **"));     }  
  
  resetCN();   
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);}
  
        }

//--------------------------------------------------------------------------------

void smsSensor1(){
  ////////// sms on trigger 1 - sms on trigger 1 - sms on trigger 1 /////////////
if (SendSMS == 1 and SMSsent == 0 and Trigger1 == 1){
  digitalWrite(RelayOUT1, HIGH);  
  digitalWrite(RelayOUT2, HIGH); 
  resetCN();
  checkAT();  
  checkSIGNAL();  
  checkSIMINFO();  
  checkREGISTER(); 

  
    if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){      
  UpdatedTimestr = ""; delay(100);  
  GetModemTime();
  

   
  Serial.println(F("trigger1 activated for sms mode"));  
  SMSstep1();
  mySerial.print(F(MESSAGE2)); updateSerial(); mySerial.print(F(MESSAGE11)); updateSerial();  mySerial.print(F(MESSAGE10)); updateSerial();
  mySerial.print(UpdatedTimestr); updateSerial();
  mySerial.write(26);
  SMSsent = 1;
  Serial.println(F("sms sent cell 1 for trigger 1"));             
  digitalWrite(RelayOUT1, LOW);  
  digitalWrite(RelayOUT2, LOW);                  
  resend_smsSensor1 = 0;      }
  

  if (CheckReg == 0 or SignalStat == 0){  
  LastTIMEstrF3 = "";
  resend_smsSensor1 = 1; 
  delay(3000);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);
  SMSsent = 1;                   
  LastTIMEstrF3 =  UpdatedTimestr;  
  Serial.println(F("Message could NOT be sent try again ** smsSensor1 STEALING 1 **"));    }
  
   resetCN();    
   digitalWrite(RelayOUT1, LOW); 
   digitalWrite(RelayOUT2, LOW);}
   
    }
////////// sms on trigger 1 - sms on trigger 1 - sms on trigger 1 /////////////



//------------------------------------------------------------------------

void smsSensor2(){
////////// sms on trigger 2 - sms on trigger 2 - sms on trigger 2 /////////////
 if (SendSMS2 == 1 and SMSsent2 == 0 and Trigger2 == 1){
  digitalWrite(RelayOUT1, HIGH); 
  digitalWrite(RelayOUT2, HIGH); 
  resetCN(); 
  checkAT(); 
  checkSIGNAL();   
  checkSIMINFO();  
  checkREGISTER();


   if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){
  UpdatedTimestr = ""; delay(100);  
  GetModemTime();
  

   
  Serial.println(F("trigger2 activated for sms mode")); 
  delay(1000);
  SMSstep1(); 
  mySerial.print(F(MESSAGE3)); updateSerial(); mySerial.print(F(MESSAGE12)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();
  mySerial.print(UpdatedTimestr); updateSerial();
  mySerial.write(26);
  SMSsent2 = 1;
  Serial.println(F("sms sent cell 1 for trigger 2"));             
  digitalWrite(RelayOUT1, LOW);     
  digitalWrite(RelayOUT2, LOW);                  
  resend_smsSensor2 = 0;      }
  

  if (CheckReg == 0 or SignalStat == 0){  
  LastTIMEstrF3 = "";
  resend_smsSensor2 = 1; 
  delay(3000);
  digitalWrite(RelayOUT1, LOW);
  digitalWrite(RelayOUT2, LOW);
  SMSsent2 = 1;                   
  LastTIMEstrF3 =  UpdatedTimestr;
  Serial.println(F("Message could NOT be sent try again ** smsSensor1 STEALING 1 **"));    }
  
   resetCN();      
   digitalWrite(RelayOUT1, LOW);
   digitalWrite(RelayOUT2, LOW);}

}
////////// sms on trigger 2 - sms on trigger 2 - sms on trigger 2 /////////////


////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE RE SEND SMS FUNCTION BELOW
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////

void ResendFaulty1(){
  resetCN();
  checkAT();  
  checkSIGNAL();   
  checkSIMINFO();  
  checkREGISTER();  
  if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){       
  Serial.println(F("Sensor 1 has been disconnected"));
  Serial.println(F("Re-sending sms Sensor 1 disconnect"));
  SMSstep1(); 
  mySerial.print(F(MESSAGE13)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();mySerial.print(F(MESSAGE15)); updateSerial();
  mySerial.print(LastTIMEstrF1); updateSerial();
  mySerial.write(26);Serial.println(F("sms sent for sensor 1 faulty condition"));                    
  resend_smsFaulty1 = 0;   
  LastTIMEstrF1 = "";
  GetModemTime();
        }
  
  if (CheckReg == 0 or SignalStat == 0){  
  resend_smsFaulty1 = 1;     }

  resetCN();
   }
      



void ResendFaulty2(){
  resetCN();
  checkAT();  
  checkSIGNAL();   
  checkSIMINFO(); 
  checkREGISTER(); 
  if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){      
  Serial.println(F("Sensor 2 has been disconnected"));
  Serial.println(F("Re-sending sms Sensor 2 disconnect"));
  SMSstep1(); 
  mySerial.print(F(MESSAGE14)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();mySerial.print(F(MESSAGE15)); updateSerial();
  mySerial.print(LastTIMEstrF2); updateSerial();
  mySerial.write(26);Serial.println(F("sms sent for sensor 2 faulty condition"));                 
  resend_smsFaulty2 = 0;  
  LastTIMEstrF2 = ""; 
  GetModemTime();
           } 

  if (CheckReg == 0 or SignalStat == 0){  
  resend_smsFaulty2 = 1;     }

  resetCN();
   }



void ResendSensor1(){
  ////////// sms on trigger 1 - sms on trigger 1 - sms on trigger 1 /////////////
  resetCN();
  checkAT();  
  checkSIGNAL();   
  checkSIMINFO(); 
  checkREGISTER(); 
  if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){      
  Serial.println(F("trigger1 activated for sms mode"));  
  Serial.println(F("Re-sending sms for triger 1 activated")); 
  SMSstep1(); 
  mySerial.print(F(MESSAGE2)); updateSerial(); mySerial.print(F(MESSAGE11)); updateSerial();mySerial.print(F(MESSAGE10)); updateSerial();
  mySerial.print(LastTIMEstrF3); updateSerial();
  mySerial.write(26);Serial.println(F("sms sent cell 1 for trigger 1"));                   
  resend_smsSensor1 = 0;   
  LastTIMEstrF3 = "";
  GetModemTime();
         }     

  if (CheckReg == 0 or SignalStat == 0){  
    resend_smsSensor1 = 1;     }

    resetCN();
   }
   
////////// sms on trigger 1 - sms on trigger 1 - sms on trigger 1 /////////////




void ResendSensor2(){
////////// sms on trigger 2 - sms on trigger 2 - sms on trigger 2 /////////////
  resetCN();
  checkAT();  
  checkSIGNAL();   
  checkSIMINFO();  
  checkREGISTER(); 
  if    (CheckReg == 1 and SignalStat == 1 and CheckComms == 1){      
  Serial.println("trigger2 activated for sms mode"); 
  Serial.println(F("Re-sending sms for triger 2 activated"));
  SMSstep1(); 
  mySerial.print(MESSAGE3); updateSerial(); mySerial.print(MESSAGE12); updateSerial();mySerial.print(MESSAGE10); updateSerial();
  mySerial.print(LastTIMEstrF4); updateSerial();
  mySerial.write(26);Serial.println("sms sent cell 1 for trigger 2");                    
  resend_smsSensor2 = 0;  
  LastTIMEstrF4 = "";
  GetModemTime();
             }     

  if (CheckReg == 0 or SignalStat == 0){  
    resend_smsSensor2 = 1;     }

    resetCN();
   }
               
////////// sms on trigger 2 - sms on trigger 2 - sms on trigger 2 /////////////
////////////////////////////////////*********************************////////////////////////////////
//*////////////////////////////////////*********************************////////////////////////////////
// * THIS IS THE RE SEND SMS FUNCTION ABOVE
// */////////////////////////////////////*********************************////////////////////////////////
////////////////////////////////////*********************************////////////////////////////////

/*
 *The below is a breakdown of the sms send function 
 *it will call repeated functions
 */

 void SMSstep1(){
  mySerial.println(F("AT"));
  updateSerial();
  mySerial.println(F("AT+CMGF=1")); 
  updateSerial();  
  mySerial.println(F("AT+CMGS=\"+*******************\"")); 
  updateSerial(); 
 }

Good day @Railroader , Here is a quickly drawn pic of the setup.
Can you assist with an idea to filter out the noise? the abnormal spikes as per the graph is not from the sensor. I don't know if its from the analog rail on board the Mega?
BTW i am a new user so forgive me for not uploading my entire code, It does not allow me to post all 8 pages. below is some related to the analogs.
If the same application is connected to a PLC there is no noise. This is very confusing.
The PLC's used is Siemens S7 and also Mitsubishi FX5.
I have tried something similar to use the ARD mega as a Multichannel signal converter and feed raw values using modbusTCP/ip into a server, The noise on all analog are the same.

Does the sensor have it's own power supply ? Does it have a GND wire ?
Do you use a breadboard and jumper wires ? Breadboards have often bad contacts and jumper wires can be broken.

A 4-20mA sensor is meant for a industrial environment. That output is not acceptable. I think you should find the cause of the problem instead of trying to use a software filter.

The 10k protection resistor is good.

How about a "star" ground. That is better than GND wires just going anywhere.

Your sketch is not between code-tags and sadly not readable.
There is a </> button for code tags.

```
Your sketch
````

You can fix that by editing your previous post, or make a new post with code-tags.

The snippets of code have no meaning. There is even a website for that: https://snippets-r-us.com/
I know you can not do much on this forum when beginning. That is a problem that many have with a serious question :cry:

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please edit your post to add code tags.

What does this mean? Please post a schematic of the wiring.

4-20 ma sensors that is fed from 12vdc via 250 Ohm as a bridge divider for voltage measurement on input

If you are using the 5V supply for reference, that could be where the spike is coming from. If the 5V supply glitches, the ref voltage derived from it will also glitch and your ADC result will glitch. The Mega has 2.56 and 1.1V internal refs that are much more stable. Use a load resistor that will give you about 2.5V at 20mA (120 Ohms is the closest standard, or two 250s in parallel) and use the 2.56V internal reference. Correct the ADC to voltage in the program.

I will try this, Its a good idea thanks @JCA34F.

I still can't read the sketch in Reply #4, please fix that.

I posted the entire code.
In the ide its divided in tabs to keep track on all my functions. Its very complicated all on one page.

@Koepel thanks for the response. the sensor is loop powered it does not have a Separate source output or hart. Its the conventional type. All grounds are soldered together. I don’t think theres an issue on the wires. I did install a 100uf cap over the analog and it seems to be a bit more steady. But still have that slight ripple. Not sure what els to try except to run a few test on the ADC supply on the mega itself. Any recommendations?

I would try connecting a ceramic capacitor between the analog input and gnd. I suggest soldering, not plugging it into a breadboard.
10nF, 100nF, 1uF....

Averaging lets say 10 last samples could be thinkable.

Its not clear from your graph what spikes are phantom and what are real.
Is the spike at 839 real?
Or you trying to get rid of the spikes at 638?

Typically one would add a capacitor at the A/D input pin to the Arduino ground. But to sense some "bad" spikes (vs some "phantom" spikes) you would have to define how fast a spike to be sensed is and modify the 10k series resistor + the above referenced capacitor.