esp32 arduino framework tank level sensor slave code

this is the slave code , master code is on another page

fuel gauge
a touchless fuel or water gauge for boats, or other stuff

A high percentage of this code probably came from someone else. I do not know how to write code, but I can sometimes paste things together that work. Even if the code shown here did not come from one of the sources below, I most likely learned how to make it work with help from them. My apologies to anyone I missed. Please make use of anything you see here, but do not count on good coding practice as I have just done what was needed to make it work. But it does work, unless otherwise noted. Thanks to everyone who has shared their knowledge and good luck to those who are learning. https://www.jarutex.com/index.php/2021/12/19/8868/ https://www.arduino.cc/ https://www.sparkfun.com/ https://randomnerdtutorials.com/ https://youtu.be/3BWSSpcn95A https://stackoverflow.com/ https://youtu.be/684KSAvYbw4 https://exploreembedded.com/ https://www.youtube.com/watch?v=JmvMvIphMnY&t https://prosperityconsulting.com

#include <esp_now.h>
#include <WiFi.h>
#include <Preferences.h>  // library to set up write-to-flash function 
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define DELETEBEFOREPAIR 0
//int slaveNumber = 787878;
int slaveNumber = 949494;
byte lockCalibrate;

Preferences preferences;  // Assignment to set up write-to-flash function

/*
  pins used : 04,15, 14, 25,26,32,33:: s1= 04, s5=15, s4=14,, s2=33,  s3=32 switch is on pin 26
  // using touchpins: 0, 3, 6, 7, 8, 9  which are physical pins s1= 04, s5=15, s4=14,, s2=33,  s3=32
  //  MAJOR MILESTONE FOR SLAVE.   UP TO THREE SLAVES WILL SEND AND RECIVE DATA BROADCAST ADDRESS HAS TO BE MANUALLY CHANGED TO THAT OF THE MASTER SO THAT THE SLAVES ARE NOT SENDING TO EACHOTHER. setting the address should BE AUTOMATED AT SOME POINT.
  done
  - wifi / espnow fuctions work
  - the inputs from the slaves is seperated, currently by hard coded values,
  -reestablish good data transfer from slave to master
  - read values from sensor, store reference values (calibration), send to master/display unit
  -reestablish good data transfer from master to slave
  todo
  - change hard coded values to automatic mac addresses
  - get mac addresses automatically
  - move the calibration button from the slave to the master to reduce component and assembly time and costs
  - clean up code. make vars unique and make as much code into functions as practical
  - suspend wirit functions while setting configuration
  - MAKE IT ALL WORK WITH MORE THAN ONE SENSOR MODULE


*/

int numberOfSamples = 2000;     // the number of samples to take for averaging sensor readings
int delayTimeForAverage = 50;  // gap between printout, for troupble shooting

// REPLACE WITH THE MAC Address in slave to the single master board (the master uses 0xFF broadcast address)
//uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
//uint8_t broadcastAddress[] = { 0x30,0xC6,0xF7,0x05,0x9B,0x94 };
uint8_t broadcastAddress[] = { 0x30, 0xc6, 0xf7, 0x05, 0x9b, 0xb8 }; // mac of master display to send to

int LEDvar1 = 0;  //define varibles to hold sensor status to be sent to master/display
int LEDvar2 = 0;
int LEDvar3 = 0;
int LEDvar4 = 0;
int LEDvar5 = 0;

int  buttonStatus = 1; // initialize button pins and vars
int  pinValue = 1; //  digitalRead(26);

int  currentSensorVal1;  // initialize variables for the latest/current reading of the sensors
int  currentSensorVal2;
int  currentSensorVal3;
int  currentSensorVal4;
int  currentSensorVal5;
int	sendBackToMaster;   // this is to reset the request for calibration flag from the master

int  threshHoldValue1;  // INITIALIZE VARIABLEs TO STORE  drySensorValue used for calibrationr
int  threshHoldValue2;
int  threshHoldValue3;
int  threshHoldValue4;
int  threshHoldValue5;


// Init ESP Now with fallback - by esp no changes by Don
void InitESPNow() {
	WiFi.disconnect();
	if (esp_now_init() == ESP_OK) {
		Serial.println("ESPNow Init Success");
	} else {
		Serial.println("ESPNow Init Failed");
		// Retry InitESPNow, add a counte and then restart?
		// InitESPNow();
		// or Simply Restart
		ESP.restart();
	}
}

String SSID = "not set yet";
// config AP SSID  with indetifying mac -- by esp no changes by don
void configDeviceAP() {
	String Prefix = "Slave_dw:";
	String Mac = WiFi.macAddress();
	String SSID = Prefix + Mac;
	String Password = "123456789";
	bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
	if (!result) {
		Serial.println("AP Config failed.");
	} else {
		Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
	}
}

// variables used to store level sensor readings and other vars to be sent
int levelSense1;
int levelSense2;
int levelSense3;
int levelSense4;
int levelSense5;
int slaveIdenitier;
char optionalCharVar[32];
//int REsetFlgToOne;

// variables to store incoming values for: padding, button-pushed flag for calibration and master mac value
int incomingDoCalibrate1 = 5;  //1111;
byte incomingPadding;
String incomingDw_s;

String success;  // Variable to store if sending data was successful

//Structure of data that is sent and recived. It must match the structure of paired board
typedef struct struct_message {
	int slaveNumber;
	int sensor1;
	int sensor2;
	int sensor3;
	int sensor4;
	int sensor5;
	char dw_char[32];
	byte dw_pad;
	int doCalibrate1;
	int doCalibrate2;
	int doCalibrate3;
	//  int reSetCalFlag;
} struct_message;

// Create a struct_message called levelSensorReadings to hold sensor readings
struct_message outgoingValues;

// Create a struct_message to hold incoming sensor readings
struct_message incomingValues;

// Callback when data is sent - ALL BY ESP EXCEPT SOME TEXT MESSAGE CHANGES BY
void OnDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) {
	Serial.print("\r\nLast Packet Send Status:\t");
	Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
	if (status == 0) {
		success = "Delivery Success :)";
		String Mac = WiFi.macAddress();
		Serial.println("AP Config Success. Broadcasting from SLAVE with AP: " + String(Mac));
	} else {
		success = " OUTGOING DELIVER FAIL FAIL FAIL FAIL FAIL Delivery Fail :(";
	}
} // end of data sent call back
// END Callback when data is SENT

// on data RECEIVE callback function  --- THIS IS WHERE THE DATA IS RECEIVED
void OnDataRecv(const uint8_t* mac, const uint8_t* incomingData, int len) {
	memcpy(&incomingValues, incomingData, sizeof(incomingValues));
	char macStr[18];
	snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
			 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	Serial.print("////////////// Last Packet Recv FROM  >>>>>>>>>: "); Serial.println(macStr);
	Serial.print("Bytes received: ");
	Serial.println(len);
	Serial.print("Last Packet Recv Data: "); Serial.println(*incomingData);
	// Data being captured and stored to local variables
	incomingPadding = incomingValues.dw_pad;
	incomingDoCalibrate1 = incomingValues.doCalibrate1;
	lockCalibrate = incomingDoCalibrate1;
	Serial.print(" JUST RECEIVED  incomingDoCalibrate1 from incomingValues.doCalibrate1 value is >>> : ");
	Serial.println(incomingDoCalibrate1);
} // end of RECEIVE call back function

void setup() {
	// Init Serial Monitor
	Serial.begin(115200);
	preferences.begin("calibration", false);//for nonvolitile memory process  NO ERROR REPORTED IF MISSING!

	pinMode(25, OUTPUT); // this is for LED that indicates button has been pressed long enough
	pinMode(26, INPUT_PULLUP);  // enable internal pull-up	for calibration pushbutton
	int pinValue = 1;

	WiFi.mode(WIFI_AP_STA); 	// configure device AP mode
	configDeviceAP();

	// Init ESP-NOW
	if (esp_now_init() != ESP_OK) {
		Serial.println("Error initializing ESP-NOW");
		return;
	}
	//  register for Send CB to get the status of Trasnmitted packet
	esp_now_register_send_cb(OnDataSent);
	esp_now_peer_info_t peerInfo; // Register peer
	memcpy(peerInfo.peer_addr, broadcastAddress, 6);
	peerInfo.channel = 0;
	peerInfo.encrypt = false;
	if (esp_now_add_peer(&peerInfo) != ESP_OK) {  // Add peer
		Serial.println("Failed to add peer");
		return;
	}
	// Register for a callback function that will be called when data is received
	esp_now_register_recv_cb(OnDataRecv);
} //  END OF SETUP()

unsigned long startMillis = 0;
void loop() { // *********  *    this is the start of looop ****************
	displayStatusOfSlave(); // various troubleshooting information to display no variables set, can be removed. 
	
	checkCalibrationRequestSW(); // looks for calibration request from switch on the module
	checkCalibrationRequestWIFI(); // looks for calibration request from switch on the module


	readAndReport();  // get the sensor readings, store the values and display them

	valuesToSend();  // collect the sensor values and any other values to send 
	sendSensorValues(); // send the sensor values from this slave to the master using it's MAC address

}  //   *********************************** end of loop() *********************************

void displayStatusOfSlave() {// various informatina about slave to display for troublshooting. 
	Serial.println("// *******  *    this is the start of looop ***********");
	Serial.print("This is second  count  millis() / 1000               >>> "); Serial.println(millis() / 1000);
	Serial.print(" incomingDoCalibrate1   var to request calibration >>>>>>> : ");
	Serial.println(incomingDoCalibrate1);
	Serial.print(" incomingPad  var to set padding              >>>>>>>>>>>>>>> : ");
	Serial.println(incomingPadding);
	delay(5000);
	Serial.println("  CHECK FOR CALIBRATION FUNCTION IS NEXT - CHECKS FOR BUTTON PRESS OR nine nine nine nine");
	delay(5000);
}

void sendSensorValues() {// Send the data over esp-now
	esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingValues, sizeof(outgoingValues));
	if (result == ESP_OK) {
		Serial.println("FEB 11TH Sent with success");
	} else {
		Serial.println("FEB 11TH Error sending the data");
	}
	delay(500);
}

void checkCalibrationRequestSW() {
	//  ************************************ GET BUTTON VALUE  ****************************************
	pinValue = digitalRead(26); // this works from the pushbutton on the slave board.
	delay(10); // quick and dirty debounce filter
	if (pinValue == 0) {
		// END OF SECTION THAT GETS INPUT FROM THE BUTTON ON THE SLAVE BOARD.
		Serial.print("***INSIDE button FUCTION JUST BEFORE CALIBRATE FUNCTION >>>incomingDoCalibrate = :  ");
		Serial.println(incomingDoCalibrate1);
		delay(5000);
		Serial.println("*********************************GOING TO RUN THE CALIBRATE FUNCTION **************************");
		Serial.println("*********************************GOING TO RUN THE CALIBRATE FUNCTION **************************");
		Serial.println("*********************************GOING TO RUN THE CALIBRATE FUNCTION **************************");
		digitalWrite(4, HIGH); // blink an LED to show request has be registered
		digitalWrite(4, HIGH);  delay(500); digitalWrite(4, LOW);   delay(300); digitalWrite(4, HIGH);  delay(300);
		digitalWrite(4, LOW); delay(300); digitalWrite(4, HIGH); delay(300); digitalWrite(4, LOW);  delay(300);
		digitalWrite(4, HIGH);  delay(300); digitalWrite(4, LOW);
		threshHoldValue1 = runCalibrationFunction(); // RUN FUNCTION // fuction to Capture and store 'dry' value
	}
}

void checkCalibrationRequestWIFI() {
	//	WiFi.disconnect(true);
	delay(100);
	//  ************************************ Get Calibration trigger from master ***************************

	Serial.print("CHECKING FOR calibration request from master.  Value of doCalibrate is >>> :  ");
	Serial.println(incomingDoCalibrate1);
	delay(5000);
	if (incomingDoCalibrate1 == 9999) {
		//	if (lockCalibrate == 9999) {
		Serial.print("EXICUTING  CALIBRATE FUNCTION >>>incomingDoCalibrate = :  ");
		Serial.println("*********************************GOING TO RUN THE CALIBRATE FUNCTION **************************");
		Serial.println("*********************************GOING TO RUN THE CALIBRATE FUNCTION **************************");
		digitalWrite(4, HIGH); // blink an LED to show request has be registered
		digitalWrite(4, HIGH);  delay(500); digitalWrite(4, LOW);   delay(300); digitalWrite(4, HIGH);  delay(300);
		digitalWrite(4, LOW); delay(300); digitalWrite(4, HIGH); delay(300); digitalWrite(4, LOW);  delay(300);
		digitalWrite(4, HIGH);  delay(300); digitalWrite(4, LOW);

		threshHoldValue1 = runCalibrationFunction(); // RUN FUNCTION // fuction to Capture and store 'dry' value

		lockCalibrate = 1111; //   incomingDoCalibrate1 = 1111;
		Serial.print("***JUST  >>> AFTER <<< CALIBRATE FUNCTION ********incomingDoCalibrate = :  ");
		Serial.println(incomingDoCalibrate1);
	}
}


void readAndReport() {   // code by dw to get values, compare them and turn on LEDs as needed.
	Serial.println("------------------- IN LOOP ");
	int  savedThreshHoldValue1;  // intialize the variables used for calibration *************
	int  savedThreshHoldValue2;
	int  savedThreshHoldValue3;
	int  savedThreshHoldValue4;
	int  savedThreshHoldValue5;

	// READ SENSOR THREASHOLD VALUES STORED AT LAST CALIBRATION. USED TO COMPARE TO CURRENT SENSOR READINGS
	savedThreshHoldValue1 = preferences.getInt("sensor1", savedThreshHoldValue1);
	savedThreshHoldValue2 = preferences.getInt("sensor2", savedThreshHoldValue2);
	savedThreshHoldValue3 = preferences.getInt("sensor3", savedThreshHoldValue3);
	savedThreshHoldValue4 = preferences.getInt("sensor4", savedThreshHoldValue4);
	savedThreshHoldValue5 = preferences.getInt("sensor5", savedThreshHoldValue5);
	Serial.print("====================saved value one   ");
	Serial.println(savedThreshHoldValue1);
	delay(800);

	// get the AVERAGE value of sensor at the current time and store it to currentSensor Val watch for them to go below the stored threashold value
	currentSensorVal1 = touchread1();  delay(5);
	currentSensorVal2 = touchread2();   delay(5);  // 32
	currentSensorVal3 = touchread3();  delay(5); //15
	currentSensorVal4 = touchread4(); delay(5); //12
	currentSensorVal5 = touchread5(); delay(5); //33

	Serial.println("||||||||||||||||||||||||||||||||||||||||||||||||||||");
	Serial.println("       SAVED	 CURRENT	    DIFFERENCE =================");
	Serial.print(" value of padding : ");
	Serial.print(incomingValues.dw_pad);
	Serial.print("     value of pinvalue         >>>>> : "); Serial.println(pinValue);
	Serial.print("THIS IS THE CURRENT VALUE OF incomingDoCalibrate1  >>>>>>>>>>>>>>>>> : ");
	Serial.println(incomingDoCalibrate1);
	Serial.print("Value 1 > ");
	Serial.print(savedThreshHoldValue1);
	Serial.print("\t");
	Serial.print(currentSensorVal1);
	Serial.print("\t"); Serial.print("\t");
	Serial.println(savedThreshHoldValue1 - currentSensorVal1);
	Serial.print("Value 2 > ");
	Serial.print(savedThreshHoldValue2);
	Serial.print("\t");
	Serial.print(currentSensorVal2);
	Serial.print("\t"); Serial.print("\t");
	Serial.println(savedThreshHoldValue2 - currentSensorVal2);
	Serial.print("Value 3 > ");
	Serial.print(savedThreshHoldValue3);
	Serial.print("\t");
	Serial.print(currentSensorVal3);
	Serial.print("\t"); Serial.print("\t");
	Serial.println(savedThreshHoldValue3 - currentSensorVal3);
	Serial.print("Value 4 > ");
	Serial.print(savedThreshHoldValue4);
	Serial.print("\t");
	Serial.print(currentSensorVal4);
	Serial.print("\t"); Serial.print("\t");
	Serial.println(savedThreshHoldValue4 - currentSensorVal4);
	Serial.print("Value 5 > ");
	Serial.print(savedThreshHoldValue5);
	Serial.print("\t");
	Serial.print(currentSensorVal5);
	Serial.print("\t"); Serial.print("\t");
	Serial.println(savedThreshHoldValue5 - currentSensorVal5);
	delay(4000);

	if (currentSensorVal1 <= savedThreshHoldValue1) {
		Serial.println("HIGH  HIGH                     HIGH   HIGH");
		LEDvar1 = 1;
	} else {
		Serial.println("LOW   LOW LOW                   LOW    LOW");
		LEDvar1 = 0;
	}
	if (currentSensorVal2 <= savedThreshHoldValue2) {
		Serial.println("HIGH  HIGH                     HIGH   HIGH");
		LEDvar2 = 1;
	} else {
		Serial.println("LOW   LOW LOW                   LOW    LOW");
		LEDvar2 = 0;
	}
	if (currentSensorVal3 <= savedThreshHoldValue3) {
		Serial.println("HIGH  HIGH                     HIGH   HIGH");
		LEDvar3 = 1;
	} else {
		Serial.println("LOW   LOW LOW                   LOW    LOW");
		LEDvar3 = 0;
	}
	if (currentSensorVal4 <= savedThreshHoldValue4) {
		Serial.println("HIGH   HIGH                    HIGH   HIGH");
		LEDvar4 = 1;
	} else {
		Serial.println("LOW   LOW LOW                   LOW    LOW");
		LEDvar4 = 0;
	}
	if (currentSensorVal5 <= savedThreshHoldValue5) {
		Serial.println("HIGH  HIGH                     HIGH   HIGH");
		LEDvar5 = 1;
	} else {
		Serial.println("LOW   LOW LOW                   LOW    LOW");
		LEDvar5 = 0;
	}
	//	delay(100);
}  // end of readAndReport()
// end of readAndReport()

int  runCalibrationFunction() { // PUSH BUTTON TO COLLECT CURRENT SENSOR VALUES AND SAVE
	Serial.println("NOW INSIDE   PushButtonToSetDrySensor1()");

	threshHoldValue1 = currentSensorVal1 - incomingPadding; //sensor on analog in 0
	threshHoldValue2 = currentSensorVal2 - incomingPadding;
	threshHoldValue3 = currentSensorVal3 - incomingPadding;
	threshHoldValue4 = currentSensorVal4 - incomingPadding;
	threshHoldValue5 = currentSensorVal5 - incomingPadding;

	Serial.println(" BUTTON IS PUSHED ================     ");
	Serial.print("    VALUE OF   threshHoldValue1 ======   ");
	Serial.println(threshHoldValue1);
	Serial.print("    VALUE OF   threshHoldValueTWO == ===  ");
	Serial.println(threshHoldValue2);
	delay(1500); // this is just here to give time to read value
	// save the threshHoldvalue in eeprom
	dwEeprommRoutine(threshHoldValue1, threshHoldValue2, threshHoldValue3, threshHoldValue4, threshHoldValue5);
} //  end of this function  PushButtonToSetDrySensor1()
// end of PushButtonToSetDrySensor1()

//  DEFINE FUNCTION  *************** READ WRITE TO EEPROM  FUCTION **************************
void dwEeprommRoutine(int  threshHoldValue1, int  threshHoldValue2, int savedThreshHoldValue3, int  savedThreshHoldValue4, int  savedThreshHoldValue5) {
	digitalWrite(25, HIGH); // this section just blinks the LED to know it is in calibrate mode
	delay(700); digitalWrite(25, LOW); delay(500);
	digitalWrite(25, HIGH); delay(800); digitalWrite(25, LOW); 	delay(500);
	digitalWrite(25, HIGH); // end blink mode ,  now show the stored data

	Serial.println("**************IN EEPROM FUNCTION === IN EEPROM FUNCTION **********************=== -");
	Serial.println("************IN EEPROM FUNCTION === IN EEPROM FUNCTION ************************=== --");
	delay(500);
	Serial.print(" this is threshHoldValue1    >>>>>>>#########>> ");
	Serial.println(threshHoldValue1);
	delay(1000);

	preferences.putInt("sensor1", threshHoldValue1); // WRITE TO FLASH MEMOREY USING PREFERENCES FUCTION
	delay(50);
	preferences.putInt("sensor2", threshHoldValue2);
	delay(50);
	preferences.putInt("sensor3", threshHoldValue3);
	delay(50);
	preferences.putInt("sensor4", threshHoldValue4);
	delay(50);
	preferences.putInt("sensor5", threshHoldValue5);
	delay(50);

	Serial.print("       SAVED in Prefereces threshHoldValue1 >>> ");
	Serial.println(threshHoldValue1);
	//delay(3100);
	digitalWrite(25, LOW);//  turn off "button presssed"  signel LED
	//  REsetFlgToOne = 1;  // send back via levelSensorReadings.doCalibrate1
	// REsetFlgToOne = 1; // set flag back to normal to send back to master
	//  REsetFlgToOne = 1;
	//  buttonStatus = 1;
	//  pinValue = 1;
	delay(10);
	// incomingDoCalibrate1 = 1111;
	lockCalibrate = 1111;
}
// end dwEeprommRoutine  >> flash memory routine ============================================

void valuesToSend() {   // this is where the values to send get collected and ququed for sending
	outgoingValues.slaveNumber = slaveNumber;
	outgoingValues.sensor1 = LEDvar1;
	outgoingValues.sensor2 = LEDvar2;
	outgoingValues.sensor3 = LEDvar3;
	outgoingValues.sensor4 = LEDvar4;
	outgoingValues.sensor5 = LEDvar5;
	//  outgoingValues.reSetCalFlag = sendBackToMaster;
	// outgoingValues.reSetCalFlag = REsetFlgToOne;
}

void displayInformation() {
	// Display Readings in Serial Monitor
	Serial.println("INCOMING READINGS");
	Serial.print("dw_pad buffer/padding between stored and current values: ");
	Serial.print(incomingValues.dw_pad);
	Serial.print("boolian to hold flag var to request calibration : ");
	Serial.print(incomingValues.doCalibrate1);
	Serial.print("a variable that holds a string or char[32] value: ");
	Serial.println(incomingValues.dw_char);
	Serial.print(" dw_a and int to hold some flag value: ");
	Serial.println(incomingValues.slaveNumber);
	Serial.println("END OF PRINT DISPLAY ");
}

int Min1;    // initialize the varibles that are used in the averaging/smoothing functions
int Max1;
int sampleSensor1;
unsigned long Sum1;
unsigned long Average1;
int Min2;  // Global Variables 2
int Max2;
int Analog2;
unsigned long Sum2;
unsigned long Average2;
int Min3;     // Global Variables 3
int Max3;
int Analog3;
unsigned long Sum3;
unsigned long Average3;
int Min4;                     // Global Variables  4
int Max4;
int Analog4;
unsigned long Sum4;
unsigned long Average4;
int Min5;                 // Global Variables 5
int Max5;
int Analog5;
unsigned long Sum5;
unsigned long Average5;

int  touchread1() {   // FUNCTION  to average pin values
	Min1 = 1023;     //Initilize/reset to limit
	Max1 = 0;        //Initilize/reset to limit
	Sum1 = 0;        //Initialize/reset
	//Take numberOfSamples readings, find min, max, and average.  This loop takes about 100ms.
	for (int i = 0; i < numberOfSamples; i++) {
		sampleSensor1 = touchRead(04);
		Sum1 = Sum1 + sampleSensor1;   //Sum for averaging
		if (sampleSensor1 < Min1)
			Min1 = sampleSensor1;
		if (sampleSensor1 > Max1)
			Max1 = sampleSensor1;
	}
	Average1 = (Sum1 / numberOfSamples);
	// print results
	Serial.print(" Min1 = ");
	Serial.print(Min1);
	Serial.print(" Max1 = ");
	Serial.print(Max1);
	Serial.print(" Average1 = ");
	Serial.println(Average1);
	delay(delayTimeForAverage);
	return  Average1;
}  //End

int  touchread2() {   // FUNCTION  to average pin values
	Min2 = 1023;     //Initilize/reset to limit
	Max2 = 0;        //Initilize/reset to limit
	Sum2 = 0;        //Initialize/reset
	//Take numberOfSamples readings, find min, max, and average.  This loop takes about 100ms.
	for (int i = 0; i < numberOfSamples; i++) {
		Analog2 = touchRead(33);
		Sum2 = Sum2 + Analog2;   //Sum for averaging
		if (Analog2 < Min2)
			Min2 = Analog2;
		if (Analog2 > Max2)
			Max2 = Analog2;
	}
	Average2 = (Sum2 / numberOfSamples);
	// print results
	Serial.print(" Min2 = ");
	Serial.print(Min2);
	Serial.print(" Max2 = ");
	Serial.print(Max2);
	Serial.print(" Average2 = ");
	Serial.println(Average2);
	delay(delayTimeForAverage);
	return  Average2;
}  //End

int  touchread3() {   // FUNCTION  to average pin values
	Min3 = 1023;     //Initilize/reset to limit
	Max3 = 0;        //Initilize/reset to limit
	Sum3 = 0;        //Initialize/reset
	//Take numberOfSamples readings, find min, max, and average.  This loop takes about 100ms.
	for (int i = 0; i < numberOfSamples; i++) {
		Analog3 = touchRead(32);
		Sum3 = Sum3 + Analog3;   //Sum for averaging
		if (Analog3 < Min3)
			Min3 = Analog3;
		if (Analog3 > Max3)
			Max3 = Analog3;
	}
	Average3 = (Sum3 / numberOfSamples);
	// print results
	Serial.print(" Min3 = ");
	Serial.print(Min3);
	Serial.print(" Max3 = ");
	Serial.print(Max3);
	Serial.print(" Average3 = ");
	Serial.println(Average3);
	delay(delayTimeForAverage);
	return  Average3;
}  //End

int  touchread4() {   // FUNCTION  to average pin values
	Min4 = 1023;     //Initilize/reset to limit
	Max4 = 0;        //Initilize/reset to limit
	Sum4 = 0;        //Initialize/reset
	//Take numberOfSamples readings, find min, max, and average.  This loop takes about 100ms.
	for (int i = 0; i < numberOfSamples; i++) {
		Analog4 = touchRead(14);
		Sum4 = Sum4 + Analog4;   //Sum for averaging
		if (Analog4 < Min4)
			Min4 = Analog4;
		if (Analog4 > Max4)
			Max4 = Analog4;
	}
	Average4 = (Sum4 / numberOfSamples);
	// print results
	Serial.print(" Min4 = ");
	Serial.print(Min4);
	Serial.print(" Max4 = ");
	Serial.print(Max4);
	Serial.print(" Average4 = ");
	Serial.println(Average4);
	delay(delayTimeForAverage);
	return  Average4;

}  //End

int  touchread5() {   // FUNCTION  to average pin values
	Min5 = 1023;     //Initilize/reset to limit
	Max5 = 0;        //Initilize/reset to limit
	Sum5 = 0;        //Initialize/reset

	//Take numberOfSamples readings, find min, max, and average.  This loop takes about 100ms.
	for (int i = 0; i < numberOfSamples; i++) {
		//  Analog = analogRead(A0);

		Analog5 = touchRead(15);

		Sum5 = Sum5 + Analog5;   //Sum for averaging
		if (Analog5 < Min5)
			Min5 = Analog5;

		if (Analog5 > Max5)
			Max5 = Analog5;
	}
	Average5 = (Sum5 / numberOfSamples);
	// print results
	Serial.print(" Min5 = ");
	Serial.print(Min5);
	Serial.print(" Max5 = ");
	Serial.print(Max5);
	Serial.print(" Average5 = ");
	Serial.println(Average5);
	delay(delayTimeForAverage);
	return  Average5;
}  //End

Loading

Please share it. Thanks!