Created
October 25, 2020 09:02
-
-
Save DeanSupertramp/6a362ed6beeca14f24d9fa68346282dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Andrea Alecce | |
// www.antima.it | |
// 25-10-2020 | |
// | |
#define MQ5_pin A0 | |
float R0 = 16981.25; //Resistenza ricavata dalla fase di calibrazione | |
float sensorValue = 0.0; | |
float sensor_volt = 0.0; | |
int campioni = 100; | |
int i, j = 0; | |
float Vout = 0.0; | |
float RS_gas = 0.0; | |
float ratio = 0.0; | |
double ppm_log = 0; | |
double ppm = 0; | |
float percentuale = 0; | |
float CO[2] = { -0.1547, 0.9143}; //data format:{ m, q}; | |
float Alcohol[2] = { -0.2617, 1.113}; //data format:{ m, q}; | |
float H2[2] = { -0.2428, 0.7474}; //data format:{ m, q}; | |
float Metano[2] = { -0.3958, 0.8973}; //data format:{ m, q}; | |
float GPL[2] = { -0.4132, 0.7928}; //data format:{ m, q}; | |
String GAS[6] = {"undefined", "CO", "Alcohol", "H2", "Metano", "GPL"}; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(MQ5_pin, INPUT); | |
Serial.println("Avvio programma: "); | |
} | |
void loop() { | |
Serial.println(""); | |
Serial.println("##############################################################################"); | |
Serial.println(""); | |
for (i = 0 ; i < campioni ; i++) { | |
sensorValue += analogRead(MQ5_pin); | |
delay(10); | |
} | |
sensorValue = sensorValue / campioni; | |
sensor_volt = sensorValue / 1024 * 5; | |
RS_gas = (5.0 - sensor_volt) * 10000 / sensor_volt; | |
ratio = RS_gas / R0; | |
Serial.print("sensVolt = "); Serial.print("\t"); Serial.print(sensor_volt); Serial.print("\t"); | |
Serial.print("RS_gas = "); Serial.print(RS_gas); Serial.print("\t"); | |
Serial.print("R0 = "); Serial.print("\t"); Serial.print(R0); Serial.print("\t"); | |
Serial.print("ratio = "); Serial.print(ratio); Serial.println("\t"); | |
Serial.println(""); | |
j = 1; | |
conversione(ratio, CO[0], CO[1]); Serial.println(" CO"); | |
conversione(ratio, Alcohol[0], Alcohol[1]); Serial.println(" Alcohol"); | |
conversione(ratio, H2[0], H2[1]); Serial.println(" H2"); | |
conversione(ratio, Metano[0], Metano[1]); Serial.println(" Metano"); | |
conversione(ratio, GPL[0], GPL[1]); Serial.println(" GPL"); | |
j = 0; | |
} | |
void conversione(float ratio, float m, float q) { | |
Serial.println("-------------------------------------------------------------------------------"); | |
String wGas = GAS[j]; | |
ppm_log = (log10(ratio) - q) / m; | |
ppm = pow(10, ppm_log); | |
percentuale = ppm / 10000; // *100 / 1 milione | |
Serial.print("GAS = "); | |
Serial.println(wGas); | |
if (ppm > 1000) { | |
Serial.print(" Attenzione, "); Serial.print(wGas); Serial.println(" in eccesso!!!"); | |
} | |
Serial.print("ppm_log = "); Serial.print("\t"); Serial.print(ppm_log); Serial.print("\t"); | |
Serial.print("ppm = "); Serial.print("\t"); Serial.print(ppm); Serial.print("\t"); | |
Serial.print("percentuale = "); Serial.print("\t"); Serial.print(percentuale); Serial.print("\t"); Serial.print("%"); | |
j++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment