Display the value of MQ-3 sensor on SerLCD with Arduino Uno

I created another program without the Button and my analog value is max 1012. That means I have a problem with the Button function (maybe librarie).

No, your problem is that you are getting more readings than you are averaging. If you take 6 readings, all of which are near 1020, add them up (total is about 6120) and then divide by 5, you will get a value over 1023. (Approximately 1224).

This appears to be what is happening to you.

Try this, instead:

  sensorValue = 0; // Initialize to 0

  for (int i=0; i< sampleCount ; i++)
  { 
    sensorValue += analogRead(A0); // Read and add up the sensor value
  }
  sensorValue /= sampleCount; // Divide by the actual number of readings taken

Can I use it like this because I want to skip the first and the last reading and to average just the middle values. I want to read 5 times and average the middle 3 Values...

int sensorValue = 0;   // The sensor value
  sensorValue = analogRead(A0);

    // only while the button is pushed
    if (btn.isButtonPressed()) {
   
        // read the sensor X times and average the total, skipping the 1st Y readings
        for (int i=0; i< skipCount ; i++) analogRead(A0);
        for (int i=0; i< sampleCount ; i++) 
        {
          sensorValue += analogRead(A0);
        }
        sensorValue /= sampleCount;

Hi,

I used the sequece that you said and I have the same problem, returns values bigger then 1023.
Here is the part of code that I use:

#define sampleCount 5

void loop() {

  btn.loop();
  int sensorValue = 0;   // The sensor value
  sensorValue = analogRead(A0);

    // only while the button is pushed
    if (btn.isButtonPressed()) {
   
        // read the sensor X times and average the total, skipping the 1st Y readings
        //for (int i=0; i< skipCount ; i++) analogRead(A0);
        for (int i=0; i< sampleCount ; i++) 
        {
          sensorValue += analogRead(A0);
        }
        sensorValue /= sampleCount;

I used the sequece that you said and I have the same problem, returns values bigger then 1023.

No, you did not!

  int sensorValue = 0;   // The sensor value
  sensorValue = analogRead(A0);

You have read the sensor once now.

        for (int i=0; i< sampleCount ; i++) 
        {
          sensorValue += analogRead(A0);
        }

Here, you read the sensor sampleCount MORE times.

        sensorValue /= sampleCount;

Here, you assume that you have read sampleCount times, when, in reality, you have read sampleCount+1 times.

If you INSIST on having the first reading, outside the loop, you MUST divide by (sampleCount+1) to get the correct average.

Now I really did like you said and it's working.

Here is the entire code. The libraries are attached above in this post.

/*

Breathalyzer with Arduino UNO using a MQ-3 alcohol sensor and Serial LCD from SparkFun

*/

#include <SoftwareSerial.h>
#include "serLCD.h"
#include "Button.h"

// Set pin to the LCD's rxPin
int pin = 2;  

serLCD lcd(pin);
// Set button pin
Button btn(7);


void setup()
{
  Serial.begin(9600);
  lcd.clear();
  //Push the button to start
  lcd.print("Apasa-ti butonul");
  lcd.print("pentru a incepe!");
  
  btn.setup();            // setup & describe the button's normal state
  btn.normallyOpen();        // button closes circuit when pressed
    //btn.normallyClosed();    // button opens circuit when pressed
  btn.buttonCold();        // button shorts to sink
    //btn.buttonHot();        // button shorts to voltage

  bool isButtonPressed ( void ); // is the button in the opposite of normal state ?
  bool buttonMoved ( void ); // did the button change ?
}

#define skipCount 2
#define sampleCount 5

void loop() 
{

  btn.loop();
  int sensorValue = 0;   // The sensor value
  sensorValue = analogRead(A0);
    // only while the button is pushed
    if (btn.isButtonPressed()) {
   
        // read the sensor X times and average the total, skipping the 1st Y readings
        for (int i=0; i< skipCount ; i++) analogRead(A0);
        for (int i=0; i< sampleCount ; i++) 
        {
          sensorValue += analogRead(A0);
        }
        sensorValue /= sampleCount + 1;
  
  //detection concentartion scope is between 0.04 to 4 mg/L
  float mappedValue = map(sensorValue, 120, 1023, (0.04*100.00),(4.00*100.00))/100.00; 
  lcd.clear();
  
  lcd.print("Alcool: ");
  if (mappedValue >= 0.04)
    {
    lcd.print(mappedValue);
    }
    else 
    {
      lcd.print("0.00");
    }
  lcd.print("mg/L");
  
  //averaged raw value
  lcd.print(sensorValue);
  
  delay(2000);
    }
}

I mark with orange my opinion about readings. However something it's not owrite....

#define skipCount 2
#define sampleCount 5

void loop() 
{

  btn.loop();
  int sensorValue = 0;   // The sensor value
  sensorValue = analogRead(A0);            [color=orange]// first read[/color]
    // only while the button is pushed
    if (btn.isButtonPressed()) {
   
        [color=orange]// read the sensor 5 or 6,  I'm not sure [/color]and average the total, skipping the 1st Y readings
        for (int i=0; i< skipCount ; i++) analogRead(A0);    [color=orange]// read 2 times and skip them[/color]
        for (int i=0; i< sampleCount ; i++)  [color=orange]// read 5 times[/color]
        {
          sensorValue += analogRead(A0); [color=orange]// sum the values[/color]
        }
        sensorValue /= sampleCount + 1;  [color=orange]// divide the sum to 6[/color]