map() and fmap () function

Can somebody explain me this code??? What I don't understand is the last 2 line from this code, who is in_min, in_max, out_min, out_max ???? Why should I map the sensorValue twice ? Why my Thanks

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  float sensorMin = 250.00;  // minimum value what sensor measure on analogRead
  float sensorMax = 1008.00; // max value measured on analogRead

  int x = analogRead(A0);

  float sensorValue = fmap(x, 0, 1023, sensorMin, sensorMax);
  Serial.println(sensorValue);

  float sensorValue2 = fmap(x, 0, 1023, 0.05, 10.00); /* 0.05 mg/L minimum acohol in pure aer what my MQ-3 sensor can detect and max 10 mg/L*/

  Serial.println(sensorValue2);

  delay(1000);
}

float fmap(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;
}

who is in_min, in_max, out_min, out_max

The values you passed to the function. When you did:-

fmap(x, 0, 1023, 0.05, 10.00)

To

float fmap(float x, float in_min, float in_max, float out_min, float out_max)

Then x would be the same as x in the main code, in_min would be 0, in_max would be 1023, out_min would be 0.05 and out_max would be 10.00

Hi gal_dorin,

It's just a floating point version of the map command. See http://arduino.cc/en/Reference/Map

I have readet on arduino.cc that " The map() function uses integer math so will not generate fractions “. I have used " floatmap()” and I don’t receive what I want.
What should I do to receive a fractions result ? It’s very important for my Breathalyzer. From 0 to 1 dosn’t show anything. At 0.8 mg/L I should receive a good fine… :smiley: :smiley: :smiley:

I have used " floatmap()" and I don't receive what I want.

I looked for your code, and couldn't find what I wanted, either. Hint, hint.

I have done something wrong in my code and I solved the problem;

/*

Breathalyzer with Arduino UNO using a MQ-2 alcohol sensor and Serial LCD from SparkFun
*/

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

// Set pin to the LCD's rxPin
int pin = 2;  
float sensorValue = 0; // The sensor value
int tempValue;
float sensorMin = 400;  // Minimum sensor value
float sensorMax = 1008;// Maximum sensor value
float outMin = 0.05;
float outMax = 10.00;

int x;

serLCD lcd(pin);

void setup()
{
  Serial.begin(9600);
}
float fmap (float sensorValue, float sensorMin, float sensorMax, float outMin, float outMax)
    {
  return (sensorValue - sensorMin) * (outMax - outMin) / (sensorMax - sensorMin) + outMin;
     }
void loop() {

  sensorValue = analogRead(A0); 
  //0.05 it is the minimum value that sensor can read and max is 10 mg/L
  sensorValue = fmap (sensorValue, sensorMin, sensorMax, 0.05, 10.00); 
  lcd.clear();
  lcd.selectLine(1);
  lcd.print("Alcool: ");
  if (sensorValue >= 0.05)
  {
    lcd.print(sensorValue);
  }
  else
  { 
    lcd.print("0");  
  }
  lcd.print("mg/L");
  x = analogRead(A0);
  Serial.println(x);
  tempValue = analogRead(A1);
  lcd.selectLine(2);
  lcd.print("Temp: ");
  lcd.print(tempValue);
  lcd.print(" ");
  lcd.print(char(223));
  lcd.print("C");
  delay(2000);
 
}

Good to see you got it working now.

To improve debugability you could try this variation

void loop() 
{
  int raw = analogRead(A0); 
  // uncomment for debug
  //Serial.print("RAW: ");
  //Serial.println(raw);

  //0.05 it is the minimum value that sensor can read and max is 10 mg/L
  sensorValue = fmap (raw, sensorMin, sensorMax, 0.05, 10.00);
  
  lcd.clear();
  lcd.selectLine(1);
  lcd.print("Alcool: ");
  if (sensorValue >= 0.05)
  {
    lcd.print(sensorValue);
  }
  else
  { 
    lcd.print("0");  
  }
  lcd.print("mg/L");

  x = analogRead(A0);
  Serial.println(x);

  tempValue = analogRead(A1);
  lcd.selectLine(2);
  lcd.print("Temp: ");
  lcd.print(tempValue);
  lcd.print(" ");
  lcd.print(char(223));
  lcd.print("C");
  delay(2000);

}