// tidied up but not tested // Combined resistive tank level sender and thermistor (on exhaust pipe) // output to NEXTION display also buzzer if temperature too high // written C. Michael Feb 2018 - Nov 2019 // Resistive tank sender converted to litres and gallons [here 18 element reed switch sender) // precision of sender in this example is around 2 galls; 10 litres. /* voltage read in is val = V * res / (RES0+res) with V=5 volts: 5V val 0v | RES0 | res | "res" is leads to resistive sender 5V pinA4 GND */ long RES0 = 220L; // ohms - divider circuit - actual value used - similar size to RESVAL // Tank sender details => int NRES=18; // 0,..17 from top: ohms int RESVAL[] = {34, 42, 51, 60, 69, 78, 87, 97, 106, 114, 121, 129, 135, 143, 165, 187, 214, 244}; // Tank details Volume in litres (average up/down) int VOL[] = {458, 437, 412, 391, 369, 345, 324, 299, 277, 254, 232, 207, 186, 160, 139, 119, 96, 80}; // Tank details Volume in gallons int VOLG[] = {101, 96, 91, 86, 81, 76, 71, 66, 61, 56, 51, 45, 41, 35, 30, 25, 21, 18}; int inPinV = 4; // Vol resistor join voltage connected to analog pin A4 int inPinT = 6; // Temp resistor join voltage A6 int alPin = 4; // buzzer - digital output int res = 0; // measured resistance of sender long prod; // use long (4-byte) integers to have precision [faster than floats] int val = 0; int nmin, nmint, i, imin, Fangle; #include // used to drive NEXTION display SoftwareSerial mySerial(5,6); // 5 Rx 6 Tx [could use RX TX pins - but care loading program] char volString[6]; void setup() { pinMode(alPin, OUTPUT); digitalWrite(alPin,LOW); Serial.begin(9600); // serial output option is for testing only mySerial.begin(9600); } void loop() { val = analogRead(inPinV); // read the input pin 1023 is 5v at A4 if ( val > 1020 ) { val = 900; } // in case of open circuit to sender prod = val * RES0; // long: since product spills over 2 byte int res = prod / ( 1023L - val ); // actually range 0,..1023 vs comparator rep .5,..1023.5 Serial.print(" Measured R= "); Serial.print(res); imin = 0; nmin = abs(RESVAL[0]-res) ; for (i = 1; i < NRES; i++){ nmint = abs(RESVAL[i]-res); if ( nmint < nmin ) // search for nearest fit {nmin = nmint; imin = i; } } Serial.print(" Closest match res="); Serial.print(RESVAL[imin]); Serial.print(" litres="); Serial.println(VOL[imin]); // read resistance of thermistor similarly - using pin A6 and 10K resistance int tempReading = analogRead(inPinT); // float resT = (10000.0 * tempReading )/( 1023.1 - tempReading) ; double tempK = log(10000.0 / ( (1024.0 / tempReading) - 1.0) ); // log R tempK = 1.0 / ( 0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) *tempK ); float tempC = tempK - 273.15; // float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Serial.print(resT); Serial.print(" temp C="); Serial.println(tempC); sprintf(volString,"%d L", VOL[imin]); // NEXTION output see .tft file created by editor // write numerical values as Text (need "" sent) mySerial.print(F("t0.txt=\"")); mySerial.print(volString); mySerial.print("\""); mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); sprintf(volString,"%d G", VOLG[imin]); mySerial.print(F("t2.txt=\"")); mySerial.print(volString); mySerial.print("\""); mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); int tempCi = int( tempC + 0.5); sprintf(volString,"%d C", tempCi); mySerial.print(F("t1.txt=\"")); mySerial.print(volString); mySerial.print("\""); mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); // gauge pointers for Volume and Temperature: 90 is up roughly match red sector // Fuel % of max range 0,..200 say Fangle = VOLG[imin] * 2; mySerial.print(F("z0.val=")); mySerial.print(Fangle); mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); // 72C = 130 100C = 170 40 deg per segment // Temp*2 -14 works for 72C at red Fangle = tempCi*2 - 14; mySerial.print(F("z1.val=")); mySerial.print(Fangle); mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); if( tempCi > 74 ){ // Alarm: flashing colour band plus buzzer for(i = 0; i < 10; i++){ mySerial.print(F("h0.bco=63488")); // red mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); digitalWrite(alPin,HIGH); delay(500); mySerial.print(F("h0.bco=65120")); // yellow mySerial.write(0xff); mySerial.write(0xff); mySerial.write(0xff); digitalWrite(alPin,LOW); delay(500); } } digitalWrite(alPin,LOW); delay(2000); }