Advertisement
Narayan

sparkfun

Oct 18th, 2022 (edited)
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <Wire.h>
  3. #include "SparkFunCCS811.h"
  4.  
  5. #define pSDA 2
  6. #define pSCL 14
  7. #define DATA_MAX_IDX 50
  8. #define MLX60914_I2C_ADDR 0x5a
  9. #define CSS811_I2C_ADDR 0x5b
  10.  
  11. // Glbal objects for sensors
  12. CCS811 myCCS811(CSS811_I2C_ADDR);
  13.  
  14. // Variables to save values from sensors
  15. long long int dtVOC[DATA_MAX_IDX] = {0};
  16. long long int dCO2[DATA_MAX_IDX] = {0};
  17. int i = 0;
  18.  
  19. void setup(){
  20.   Serial.begin(115200);
  21.   Wire.begin(pSDA, pSCL);
  22.  
  23.   //This begins the CCS811 sensor and prints error status of .beginWithStatus()
  24.   CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
  25.   Serial.print("CCS811 begin exited with: ");
  26.   //Pass the error code to a function to print the results
  27.   Serial.println(myCCS811.statusString(returnCode));
  28.  
  29.   //This sets the mode to 10 second reads, and prints returned error status.
  30.   returnCode = myCCS811.setDriveMode(2);
  31.   Serial.print("Mode request exited with: ");
  32.   Serial.println(myCCS811.statusString(returnCode));
  33. }
  34.  
  35. void loop(){
  36.   if( myCCS811.dataAvailable() ){
  37.     myCCS811.readAlgorithmResults(); //Calling this function updates the global tVOC and CO2 variables
  38.     dCO2[i] = myCCS811.getCO2();
  39.     dtVOC[i] = myCCS811.getTVOC();
  40.     Serial.print("Iteration: ");
  41.     Serial.println(i);
  42.     Serial.print("CO2[");
  43.     Serial.print(dCO2[i]);
  44.     Serial.print("] -- tVOC[");
  45.     Serial.print(dtVOC[i]);
  46.     Serial.println("]");
  47.  
  48.     if( !(i % 50) ){
  49.       memset(dCO2, 0, sizeof(dCO2));
  50.       memset(dtVOC, 0, sizeof(dtVOC));
  51.       i = 0;
  52.     }
  53.     i++;
  54.   }
  55.   delay(2000);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement