/********* Pleasedontcode.com ********** Pleasedontcode thanks you for automatic code generation! Enjoy your code! - Terms and Conditions: You have a non-exclusive, revocable, worldwide, royalty-free license for personal and commercial use. Attribution is optional; modifications are allowed, but you're responsible for code maintenance. We're not liable for any loss or damage. For full terms, please visit pleasedontcode.com/termsandconditions. - Project: Scanner - Source Code compiled for: Arduino Mega - Source Code created on: 2023-11-06 23:53:40 - Source Code generated by: AlexWind ********* Pleasedontcode.com **********/ /****** DEFINITION OF LIBRARIES *****/ #include /****** FUNCTION PROTOTYPES *****/ void updateInputs(); float lookupPhyDataFromVoltage(float voltage, int segmentPoints, const float* voltagePhyDataLookup); float mapFloat(float x, float inMin, float inMax, float outMin, float outMax); void convertInputsFromRawToPhyData(); /***** DEFINITION OF ANALOG INPUT PINS *****/ const uint8_t sensorPinA0 = A0; /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/ const uint8_t segmentPointsVoltageTemperaturePinA0 = 3; const float voltageTemperaturePinA0Lookup[2][segmentPointsVoltageTemperaturePinA0] PROGMEM = { {0.0, 2.0, 3.0}, // Voltage [V] {20.0, 60.0, 100.0} // Temperature [°C] }; /***** DEFINITION OF INPUT RAW VARIABLES *****/ unsigned int sensorPinA0RawData = 0; // Analog Input /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/ float sensorPinA0PhyData = 0.0; // Temperature [°C] void setup() { // Put your setup code here, to run once: pinMode(sensorPinA0, INPUT); Serial.begin(9600); // Initialize serial communication } void loop() { // Put your main code here, to run repeatedly: updateInputs(); // Refresh input data convertInputsFromRawToPhyData(); // After that updateInput function is called, so raw data are transformed into physical data according to the characteristic curve // Check if temperature is above 100°C if (sensorPinA0PhyData > 100.0) { Serial.println("Temperature is above 100°C"); } delay(1000); // Wait for 1 second before repeating the loop } void updateInputs() { sensorPinA0RawData = analogRead(sensorPinA0); } float lookupPhyDataFromVoltage(float voltage, int segmentPoints, const float* voltagePhyDataLookup) { // Search table for appropriate value. uint8_t index = 0; const float *voltagePointer = &voltagePhyDataLookup[0]; const float *phyDataPointer = &voltagePhyDataLookup[segmentPoints]; // Perform minimum and maximum voltage saturation based on characteristic curve voltage = min(voltage, voltagePointer[segmentPoints - 1]); voltage = max(voltage, voltagePointer[0]); while (pgm_read_float(voltagePointer[index]) <= voltage && index < segmentPoints) index++; // If index is zero, physical value is smaller than our table range if (index == 0) { return mapFloat(voltage, pgm_read_float(voltagePointer[0]), // X1 pgm_read_float(voltagePointer[1]), // X2 pgm_read_float(phyDataPointer[0]), // Y1 pgm_read_float(phyDataPointer[1])); // Y2 } // If index is maxed out, physical value is larger than our range. else if (index == segmentPoints) { return mapFloat(voltage, pgm_read_float(voltagePointer[segmentPoints - 2]), // X1 pgm_read_float(voltagePointer[segmentPoints - 1]), // X2 pgm_read_float(phyDataPointer[segmentPoints - 2]), // Y1 pgm_read_float(phyDataPointer[segmentPoints - 1])); // Y2 } // index is between 0 and max, just right else { return mapFloat(voltage, pgm_read_float(voltagePointer[index - 1]), // X1 pgm_read_float(voltagePointer[index]), // X2 pgm_read_float(phyDataPointer[index - 1]), // Y1 pgm_read_float(phyDataPointer[index])); // Y2 } } float mapFloat(float x, float inMin, float inMax, float outMin, float outMax) { return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } void convertInputsFromRawToPhyData() { float voltage = 0.0; voltage = sensorPinA0RawData * (5.0 / 1023.0); sensorPinA0PhyData = lookupPhyDataFromVoltage(voltage, segmentPointsVoltageTemperaturePinA0, &(voltageTemperaturePinA0Lookup[0][0])); }