the code is :
#include <arduino.h>
#include <LiquidCrystal.h>
#include <sstream>
#include <string>
#include <iostream>
#include <math.h>
/*****************************************************************************************************/
#define CALIBARAION_SAMPLE_TIMES 50 //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_INTERVAL 5 //define the time interal(in milisecond) between each samples in the
//cablibration phase
#define READ_SAMPLE_INTERVAL 50 //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES 5 //define the time interal(in milisecond) between each samples in normal operation
#define RO_CLEAN_AIR_FACTOR 60.0 //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,//which is derived from the chart in datasheet
#define VCC 4.49
#define Rl 7.42 // Load resistance 1 kohm
float nodepoints[3][2]= {
{
1,42 }
, {
3,20.9 }
, {
5,15.05 }
};
char messages[6][16] = {
"Heating sensor", "64 seconds", "Autozero", "Ready!", "% Vol= ","Rs= "};
int Ledalert=7;
float x;
float y;
float calibration_result;
float logRs;
float x0[5];
float L[5]={0};
float x_initial;
float Rs;
float Ro;
float input;
LiquidCrystal lcd(12, 11,5, 4, 3, 2); // Wiring microcontroller - LCD:
const int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
const int MQ3_analogPin = A0; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
void text_screen( char messages[], int colum, int row) {
lcd.setCursor( colum, row);
lcd.print(messages);
}
void setup() {
pinMode(Ledalert, OUTPUT);
lcd.begin(16,2);
Serial.begin(9600);
lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2000);
Serial.print("Heating ...");
Serial.print(" ");
Serial.print(" \n");
for (int j = 0; j<4; j++)
{
lcd.clear(); // Heating sensor 4x16 = 64 seconds
text_screen(messages[0], 0, 0);
for (int i = 0; i<16; i++){
if (pushbutton == 1){ // Pressing pushbutton stops initial heating and enters in measuring mode.
break;
}
lcd.setCursor(i, 1);
lcd.write(62);
delay(1000);
}
}
lcd.clear();
text_screen(messages[2], 0, 0);
delay(1000);
Serial.print("Calcul Ro...\n");
Ro = MQCalibration(MQ3_analogPin);//Calibrating the sensor. Please make sure the sensor is in clean air
//when you perform the calibration
Serial.print("Ro calculated...\n");
Serial.print("Ro=");
Serial.print(Ro);
Serial.print("kohm");
Serial.print("\n");
lcd.clear();
text_screen(messages[3], 0, 0);
delay(2000);
lcd.clear();
text_screen(messages[3], 0, 0);
delay(2000);
}
void loop() {
x = analogRead(MQ3_analogPin);
y = ADC_measure (analogRead(MQ3_analogPin));
Rs = Rs_sensor((MQ3_analogPin));
logRs = log(Rs);
Serial.print ("ADC= ");
Serial.print (ADC_measure ( analogRead(MQ3_analogPin)));
Serial.print("\n");
Serial.print("Rs=");
Serial.print(Rs_sensor((MQ3_analogPin)));
Serial.print(" ");
Serial.print("\n");
Serial.print("log(Rs)= ");
// Moyenne flottante
float val= 0.00;
for (int i = 0; i<10; i++){
val +=(Rs_sensor((MQ3_analogPin)));
delayMicroseconds(100);
}
val = val/10;
Serial.print(log(Rs_sensor((MQ3_analogPin))));
Serial.print(" ");
Serial.print("\n");
// Resultat courbe de calibrage
if ( val > 0){
float result= reMap(nodepoints,val);
calibration_result = result;
Serial.print("calibration result= ");
Serial.print(result);
Serial.println(" ");
Serial.print("\n");
}
delayMicroseconds(100);
// Alrame de teneur faible
if (calibration_result < 0.30){
digitalWrite(Ledalert, HIGH);
}
else {
digitalWrite(Ledalert, LOW);
}
Serial.print("val= ");
Serial.print(val,3);
Serial.print(" ");
Serial.print("\n");
// Float to string conversion.
char buffer[16]; // you need some space
char buffer2[16];
dtostrf(reMap(nodepoints,val),16,2, buffer);
dtostrf(Rs,16,2, buffer2);
lcd.clear();
for (int i=0; i<strlen(buffer); i++)
{
lcd.setCursor(i,0);
lcd.write(buffer[i]);
text_screen(messages[4], 1, 0);
lcd.setCursor(i,8);
lcd.write(buffer2[i]);
text_screen(messages[5], 1, 1);
}
delay(2000);
}
float ADC_measure (int adc_reading)
{
return ((float) adc_reading * (float) VCC) / 1024.0;// convert to voltage
}
/*******************Calcul de Rs**********************
connection : VCC -ETOH sensor - 1k resistor - GND
Measuring voltage Vs between ETOH and ETOH sensor and 1 K resistor
Rs = Rl *(VCC/Vs-1)
*/
float Rs_sensor( float MQ3_analogPin)
{
Rs = Rl *(VCC/(ADC_measure (analogRead(MQ3_analogPin))) -1 );
return Rs;
}
/*******************Resistance calculation*********************/
float MQResistanceCalculation(int ADC_measure)
{
return ( ((float)Rl*(1023-ADC_measure)/ADC_measure));
}
/**********************Calibration Mesure**************************/
//float Rs0=
/*int i = 0;
Serial.print("enter number ");
Serial.print(i + 1);
Serial.println(" ?");
float f = Serial.parseFloat();
/*
/***************************** MQCalibration ****************************************
************************************************************************************/
float MQCalibration(int MQ3_analogPin){
int i;
float Rval=0;
for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples
Rval += MQResistanceCalculation(analogRead(MQ3_analogPin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
Rval = Rval/CALIBARAION_SAMPLE_TIMES; //calculate the average value
Rval = Rval/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet
return Rval;
}
/*******************************************fonction de calcul teneur en alcool*****************************************/
float reMap(float pts[3][2], float input) {
int rr;
float bb,mm;
for (int nn=0; nn < 2; nn++) {
float minsensor = min (1024,pts [1][nn]);
float maxsensor = max (0, pts [1][nn]);
if (input >= minsensor && input <= maxsensor)
{
/*if (input >= pts[nn][0] && input <= pts[nn+1][0])
mm = (log(pts[nn][1])-log(pts[nn+1][1]))/(log(pts[nn][0])-log(pts[nn+1][0]));
mm = (log((input/pts[nn][1])))/mm;
mm = mm + log(pts[nn][0]);
mm = pow(10,mm);
rr= mm;
*/
for (int nn=0; nn < 2; nn++)
mm = (pts[nn+1][1]-pts[nn][1])/(pts[0][nn+1]-pts[0][nn]);
mm = (input-pts[nn][1])/mm;
mm = mm + pts [0][nn];
mm = rr;
}
else if (input< minsensor)
{
mm = 0;
mm = rr;
}
else if (input > maxsensor)
{
mm = 100;
mm = rr;
}
return(rr);
}
}

New Topic/Question
Reply


MultiQuote




|