running out of dynamic memory

I am getting this message while compiling "Global variables use 1995 bytes (97%) of dynamic memory, leaving 53 bytes for local variables"

The following code is part of my Gassensors library (ie Gassensors.cpp) and i have 9 other cases similar to case 2. when i comment out the whole case i am using just 284 bytes of dynamic memory.

 void Gassensors::printallgases(uint8_t type)
  {
switch (_type) {

        case 2:
        {int mq2gases[]={0,1,2,3,4,5,6};
        Serial.print("HYDROGEN:");
        Serial.print(gasppm(_pin, _type, mq2gases[0]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("LPG:");
        Serial.print(gasppm(_pin, _type, mq2gases[1]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("METHANE:");
        Serial.print(gasppm(_pin, _type, mq2gases[2]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("CARBON_MONOXIDE:");
        Serial.print(gasppm(_pin, _type, mq2gases[3]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("ALCOHOL:");
        Serial.print(gasppm(_pin, _type, mq2gases[4]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("SMOKE:");
        Serial.print(gasppm(_pin, _type, mq2gases[5]));
        Serial.print( "ppm" );
        Serial.print("    ");
        Serial.print("PROPANE:");
        Serial.print(gasppm(_pin, _type, mq2gases[6]));
        Serial.print( "ppm" );
        Serial.print("\n");}
        break;

       //nine other cases here
}
}

int Gassensors::gasppm(uint8_t pin, uint8_t type, int mqgases_id, uint8_t Gas_Name)
  {
    return (MQGetGasPercentage(rs_ro_ratio(pin), mqgases_id ));
  }

int Gassensors::MQGetGasPercentage(float rs_ro_ratio, int gas_id )
{
  switch (_type) {

    case 2:
     //Serial.print("calculating mq2 gases: xxxxxx     ");
     // Serial.print(gas_id);
     //Serial.print("gas_id      ");

      if ( accuracy == 0 ) {
        if ( gas_id == GAS_HYDROGEN ) {
         // Serial.print("calculating mq2 GAS_HYDROGEN");
          return (pow(10, ((-2.109 * (log10(rs_ro_ratio))) + 2.983)));
        } else if ( gas_id == GAS_LPG ) {
         // Serial.print("calculating mq2 GAS_LPG");
          return (pow(10, ((-2.123 * (log10(rs_ro_ratio))) + 2.758)));
        } else if ( gas_id == GAS_METHANE ) {
          //Serial.print("calculating mq2 GAS_METHANE");
          return (pow(10, ((-2.622 * (log10(rs_ro_ratio))) + 3.635)));
        } else if ( gas_id == GAS_CARBON_MONOXIDE ) {
         // Serial.print("calculating mq2 GAS_CARBON_MONOXIDE");
          return (pow(10, ((-2.955 * (log10(rs_ro_ratio))) + 4.457)));
        } else if ( gas_id == GAS_ALCOHOL ) {
         // Serial.print("calculating mq2 GAS_ALCOHOL");
          return (pow(10, ((-2.692 * (log10(rs_ro_ratio))) + 3.545)));
        } else if ( gas_id == GAS_SMOKE ) {
          //Serial.print("calculating mq2 GAS_SMOKE");
          return (pow(10, ((-2.331 * (log10(rs_ro_ratio))) + 3.596)));
        } else if ( gas_id == GAS_PROPANE ) {
          //Serial.print("calculating mq2 GAS_PROPANE");
          return (pow(10, ((-2.174 * (log10(rs_ro_ratio))) + 2.799)));
        }
      }
      else if ( accuracy == 1 ) {
        if ( gas_id == GAS_HYDROGEN ) {
          return (pow(10, ((-2.109 * (log10(rs_ro_ratio))) + 2.983)));
        } else if ( gas_id == GAS_LPG ) {
          return (pow(10, ((-2.123 * (log10(rs_ro_ratio))) + 2.758)));
        } else if ( gas_id == GAS_METHANE ) {
          return (pow(10, ((-2.622 * (log10(rs_ro_ratio))) + 3.635)));
        } else if ( gas_id == GAS_CARBON_MONOXIDE ) {
          return (pow(10, ((-2.955 * (log10(rs_ro_ratio))) + 4.457)));
        } else if ( gas_id == GAS_ALCOHOL ) {
          return (pow(10, ((-2.692 * (log10(rs_ro_ratio))) + 3.545)));
        } else if ( gas_id == GAS_SMOKE ) {
          return (pow(10, (-0.976 * pow((log10(rs_ro_ratio)), 2) - 2.018 * (log10(rs_ro_ratio)) + 3.617)));
        } else if ( gas_id == GAS_PROPANE ) {
          return (pow(10, ((-2.174 * (log10(rs_ro_ratio))) + 2.799)));
        }
      }
      break;

      //nine other cases here
}

Can someone please help me understand what is using up the dynamic memory.

Everywhere you have something like this:

Serial.print( "ppm" );

replace it with something like this:

Serial.print( F("ppm") );

Regards,
Ray L.

you may want to store the strings of fixed text in flash rather than copying them to dynamic memory:
some articles about this :slight_smile:
http://playground.arduino.cc/Learning/Memory

example:

Serial.print(F("HYDROGEN:"));

Z

All the text that you print is one of the possible reasons. Using the F macro will help; change all your Serial.prints from e.g.

       Serial.print("HYDROGEN:");

To

       Serial.print(F("HYDROGEN:"));

And without seeing the rest of your code, it's difficult to say.

Its all variables. Strings are killers. If you can get away from them completely, you would be happier. The F() thing for doing Serial.print() helps a lot. But, if you can pass bytes around instead of strings and let whatever your talking to interpret/display them, do that.

-jim lee

All the codes for gassensors are in this repository.

Codes related to my questiion are under newgassensorslibrary.

Thanks a lot guys using Serial.print(F(" ")) worked out.

when i use "#define xyz (1)" am i using dynamic memory or what