Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
/********* 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: "Hydrogen Sensor" - Source Code NOT compiled for: Arduino Mega - Source Code created on: 2024-06-20 14:50:21 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* Create an Arduino project utilizing the */ /* MQUnifiedsensor library to read and process data */ /* from an MQ-8 hydrogen sensor. Implement functions */ /* to convert raw analog data from pin A0 to physical */ /* values and ensure real-time updates in the loop. */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include
// https://github.com/miguel5612/MQSensorsLib /****** FUNCTION PROTOTYPES *****/ void setup(void); void loop(void); void updateInputs(); void convertInputsFromRawToPhyData(); float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup); float map_f(float x, float in_min, float in_max, float out_min, float out_max); /***** DEFINITION OF ANALOG INPUT PINS *****/ const uint8_t H2Sensor_MQ8_AOUT_PIN_A0 = A0; /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/ const uint8_t SEGMENT_POINTS_voltage__PIN_A0 = 1; const float voltage__PIN_A0_lookup[2][SEGMENT_POINTS_voltage__PIN_A0] = { {0.0}, // Voltage [V] {0.0} // [] }; /***** DEFINITION OF INPUT RAW VARIABLES *****/ /***** used to store raw data *****/ unsigned int H2Sensor_MQ8_AOUT_PIN_A0_rawData = 0; // Analog Input /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/ /***** used to store data after characteristic curve transformation *****/ float H2Sensor_MQ8_AOUT_PIN_A0_phyData = 0.0; // [] /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/ // Definitions for MQ8 sensor #define placa "Arduino UNO" #define Voltage_Resolution 5 #define pin A0 #define type "MQ-8" #define ADC_Bit_Resolution 10 #define RatioMQ8CleanAir 70 // Declare Sensor MQUnifiedsensor MQ8(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type); void setup(void) { // put your setup code here, to run once: pinMode(H2Sensor_MQ8_AOUT_PIN_A0, INPUT); Serial.begin(9600); // Init serial port // Set math model and constants for H2 concentration MQ8.setRegressionMethod(1); MQ8.setA(976.97); MQ8.setB(-0.688); MQ8.init(); // Initialize sensor // Calibration Serial.print("Calibrating please wait."); float calcR0 = 0; for (int i = 1; i <= 10; i++) { MQ8.update(); calcR0 += MQ8.calibrate(RatioMQ8CleanAir); Serial.print("."); } MQ8.setR0(calcR0 / 10); Serial.println(" done!"); if (isinf(calcR0) || calcR0 == 0) { Serial.println("Warning: Connection issue detected, please check your wiring and supply"); while (1); } MQ8.serialDebug(true); } void loop(void) { // put your main code here, to run repeatedly: updateInputs(); // Refresh input data convertInputsFromRawToPhyData(); // Transform raw data to physical data according to characteristic curve MQ8.update(); MQ8.readSensor(); MQ8.serialDebug(); delay(500); } void updateInputs() { H2Sensor_MQ8_AOUT_PIN_A0_rawData = analogRead(H2Sensor_MQ8_AOUT_PIN_A0); } /* BLOCK lookup_phyData_from_voltage */ float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup) { // Search table for appropriate value. uint8_t index = 0; const float *voltagePointer = &voltage_phyData_lookup[0]; const float *phyDataPointer = &voltage_phyData_lookup[segment_points]; // Perform minimum and maximum voltage saturation based on characteristic curve voltage = min(voltage, voltagePointer[segment_points-1]); voltage = max(voltage, voltagePointer[0]); while( voltagePointer[index] <= voltage && index < segment_points ) index++; // If index is zero, physical value is smaller than our table range if( index==0 ) { return map_f( voltage, voltagePointer[0], // X1 voltagePointer[1], // X2 phyDataPointer[0], // Y1 phyDataPointer[1] ); // Y2 } // If index is maxed out, phyisical value is larger than our range. else if( index==segment_points ) { return map_f( voltage, voltagePointer[segment_points-2], // X1 voltagePointer[segment_points-1], // X2 phyDataPointer[segment_points-2], // Y1 phyDataPointer[segment_points-1] ); // Y2 } // index is between 0 and max, just right else { return map_f( voltage, voltagePointer[index-1], // X1 voltagePointer[index], // X2 phyDataPointer[index-1], // Y1 phyDataPointer[index] ); // Y2 } } /* END BLOCK lookup_phyData_from_voltage */ /* BLOCK map_f */ float map_f(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } /* END BLOCK map_f */ /* BLOCK convertInputsFromRawToPhyData */ void convertInputsFromRawToPhyData() { float voltage = 0.0; voltage = H2Sensor_MQ8_AOUT_PIN_A0_rawData * (5.0 / 1023.0); H2Sensor_MQ8_AOUT_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage__PIN_A0, &(voltage__PIN_A0_lookup[0][0])); } /* END BLOCK convertInputsFromRawToPhyData */ /* END CODE */
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)