hello,
i'm using a code to calculate alcohol concentration using mq-3 sensor. i hav a probleme in the lcd to display the result. The result must be : x.xxxx ppm but i receive the results in the wrong place and i can read it.
here the code :
#include "arduino.h"
#include <LiquidCrystal.h>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
float concentration(float x) {
const float A[] = {
2.71494E+02, -3.10999E+02, 6.85051E+02, -3.47587E+02, 7.47499E+01 };
float result;
float B[4];
B[0] = x*x;
B[1] = B[0]*x;
B[2] = B[1]*x;
B[3] = B[2]*x;
result = A[0]*x+A[1]*B[0]+A[2]*B[1]+A[3]*B[2]+A[4]*B[3];
return result;
}
char messages[5][16] = {
"Heating sensor", "64 seconds", "Autozero", "Ready!", "ppm"};
int L;
float x;
float x0[5];
float x_initial;
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 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() {
lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2000);
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);
float sensor_value_AnalogIn = 7; // Analog output to a multimeter or datalogger (1V = 1000 ppm).
for (int i=0; i<5; i++)
{
x0[i]= analogRead(analogPin);
delay(500);
}
x_initial = (x0[0]+x0[1]+x0[2]+x0[3]+x0[4])/5.0; // Autozero. Average of 5 initial measures.
lcd.clear();
text_screen(messages[3], 0, 0);
delay(2000);
}
void loop() {
x = analogRead(analogPin);
x = (x-x_initial)*3.3; // Calculate real voltage.
if(x<0)
x = 0;
x = concentration(x);
delayMicroseconds(100);
// Float to string conversion.
char buffer[15]; // you need some space
dtostrf(x, 16, 5, buffer);
lcd.clear();
for (int i=0; i<strlen(buffer); i++)
{
lcd.setCursor(i,1);
lcd.write(buffer[i]);
}
text_screen(messages[4], 10, 0);
}
Thanks for help.