arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Example7_SensitivityDemo.ino (3862B)


      1 /******************************************************************************
      2   Sensitivity Demo
      3 
      4   Marshall Taylor @ SparkFun Electronics
      5 
      6   April 4, 2017
      7 
      8   https://github.com/sparkfun/CCS811_Air_Quality_Breakout
      9   https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library
     10 
     11   Hardware Connections (Breakoutboard to Arduino):
     12   3.3V to 3.3V pin
     13   GND to GND pin
     14   SDA to A4
     15   SCL to A5
     16 
     17   Generates random temperature and humidity data, and uses it to compensate the CCS811.
     18   This just demonstrates how the algorithm responds to various compensation points.
     19   Use NTCCompensated or BME280Compensated for real-world examples.
     20 
     21   Resources:
     22   Uses Wire.h for i2c operation
     23 
     24   Development environment specifics:
     25   Arduino IDE 1.8.1
     26 
     27   This code is released under the [MIT License](http://opensource.org/licenses/MIT).
     28 
     29   Please review the LICENSE.md file included with this example. If you have any questions
     30   or concerns with licensing, please contact techsupport@sparkfun.com.
     31 
     32   Distributed as-is; no warranty is given.
     33 ******************************************************************************/
     34 float temperatureVariable = 25.0; //in degrees C
     35 float humidityVariable = 65.0;    //in % relative
     36 
     37 #include <Wire.h>
     38 #include "SparkFunCCS811.h" //Click here to get the library: http://librarymanager/All#SparkFun_CCS811
     39 
     40 #define CCS811_ADDR 0x5B //Default I2C Address
     41 //#define CCS811_ADDR 0x5A //Alternate I2C Address
     42 
     43 CCS811 myCCS811(CCS811_ADDR);
     44 
     45 void setup()
     46 {
     47   Serial.begin(115200);
     48   Serial.println("CCS811 EnvironmentalReadings Example");
     49 
     50   Wire.begin();
     51 
     52   //This begins the CCS811 sensor and prints error status of .beginWithStatus()
     53   CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
     54   Serial.print("CCS811 begin exited with: ");
     55   Serial.println(myCCS811.statusString(returnCode));
     56 }
     57 
     58 void loop()
     59 {
     60   Serial.println();
     61   //Randomize the Temperature and Humidity
     62   humidityVariable = (float)random(0, 10000) / 100;     //0 to 100%
     63   temperatureVariable = (float)random(500, 7000) / 100; // 5C to 70C
     64   Serial.println("New humidity and temperature:");
     65   Serial.print("  Humidity: ");
     66   Serial.print(humidityVariable, 2);
     67   Serial.println("% relative");
     68   Serial.print("  Temperature: ");
     69   Serial.print(temperatureVariable, 2);
     70   Serial.println(" degrees C");
     71   myCCS811.setEnvironmentalData(humidityVariable, temperatureVariable);
     72 
     73   Serial.println("Environmental data applied!");
     74   myCCS811.readAlgorithmResults(); //Dump a reading and wait
     75   delay(1000);
     76   //Print data points
     77   for (int i = 0; i < 10; i++)
     78   {
     79     if (myCCS811.dataAvailable())
     80     {
     81       //Calling readAlgorithmResults() function updates the global tVOC and CO2 variables
     82       myCCS811.readAlgorithmResults();
     83 
     84       Serial.print("CO2[");
     85       Serial.print(myCCS811.getCO2());
     86       Serial.print("] tVOC[");
     87       Serial.print(myCCS811.getTVOC());
     88       Serial.print("] millis[");
     89       Serial.print(millis());
     90       Serial.print("]");
     91       Serial.println();
     92     }
     93     else if (myCCS811.checkForStatusError())
     94     {
     95       //If the CCS811 found an internal error, print it.
     96       printSensorError();
     97     }
     98     delay(1000); //Wait for next reading
     99   }
    100 }
    101 
    102 //printSensorError gets, clears, then prints the errors
    103 //saved within the error register.
    104 void printSensorError()
    105 {
    106   uint8_t error = myCCS811.getErrorRegister();
    107 
    108   if (error == 0xFF) //comm error
    109   {
    110     Serial.println("Failed to get ERROR_ID register.");
    111   }
    112   else
    113   {
    114     Serial.print("Error: ");
    115     if (error & 1 << 5)
    116       Serial.print("HeaterSupply");
    117     if (error & 1 << 4)
    118       Serial.print("HeaterFault");
    119     if (error & 1 << 3)
    120       Serial.print("MaxResistance");
    121     if (error & 1 << 2)
    122       Serial.print("MeasModeInvalid");
    123     if (error & 1 << 1)
    124       Serial.print("ReadRegInvalid");
    125     if (error & 1 << 0)
    126       Serial.print("MsgInvalid");
    127     Serial.println();
    128   }
    129 }