Example6_TwentyMinuteTest.ino (3467B)
1 /****************************************************************************** 2 Run for 20 minutes 3 4 Nathan Seidle @ SparkFun Electronics 5 Marshall Taylor @ SparkFun Electronics 6 7 April 4, 2017 8 9 https://github.com/sparkfun/CCS811_Air_Quality_Breakout 10 https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library 11 12 Hardware Connections (Breakoutboard to Arduino): 13 3.3V to 3.3V pin 14 GND to GND pin 15 SDA to A4 16 SCL to A5 17 18 Calculates the current run time and indicates when 20 minutes has passed 19 20 Read the TVOC and CO2 values from the SparkFun CSS811 breakout board 21 22 A new sensor requires at 48-burn in. Once burned in a sensor requires 23 20 minutes of run in before readings are considered good. 24 25 Resources: 26 Uses Wire.h for i2c operation 27 28 Development environment specifics: 29 Arduino IDE 1.8.1 30 31 This code is released under the [MIT License](http://opensource.org/licenses/MIT). 32 33 Please review the LICENSE.md file included with this example. If you have any questions 34 or concerns with licensing, please contact techsupport@sparkfun.com. 35 36 Distributed as-is; no warranty is given. 37 ******************************************************************************/ 38 #include <Wire.h> 39 40 #include "SparkFunCCS811.h" //Click here to get the library: http://librarymanager/All#SparkFun_CCS811 41 42 #define CCS811_ADDR 0x5B //Default I2C Address 43 //#define CCS811_ADDR 0x5A //Alternate I2C Address 44 45 CCS811 myCCS811(CCS811_ADDR); 46 47 void setup() 48 { 49 Serial.begin(115200); 50 Serial.println("20 minute test"); 51 52 Wire.begin(); 53 54 //This begins the CCS811 sensor and prints error status of .beginWithStatus() 55 CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus(); 56 Serial.print("CCS811 begin exited with: "); 57 Serial.println(myCCS811.statusString(returnCode)); 58 } 59 60 void loop() 61 { 62 if (myCCS811.dataAvailable()) 63 { 64 myCCS811.readAlgorithmResults(); 65 66 Serial.print("CO2["); 67 Serial.print(myCCS811.getCO2()); 68 Serial.print("] tVOC["); 69 Serial.print(myCCS811.getTVOC()); 70 Serial.print("] millis["); 71 Serial.print(millis()); 72 Serial.print("] "); 73 printRunTime(); 74 Serial.println(); 75 } 76 else if (myCCS811.checkForStatusError()) 77 { 78 printSensorError(); 79 } 80 81 delay(1000); //Wait for next reading 82 } 83 84 //Prints the amount of time the board has been running 85 //Does the hour, minute, and second calcs 86 void printRunTime() 87 { 88 char buffer[50]; 89 90 unsigned long runTime = millis(); 91 92 int hours = runTime / (60 * 60 * 1000L); 93 runTime %= (60 * 60 * 1000L); 94 int minutes = runTime / (60 * 1000L); 95 runTime %= (60 * 1000L); 96 int seconds = runTime / 1000L; 97 98 sprintf(buffer, "RunTime[%02d:%02d:%02d]", hours, minutes, seconds); 99 Serial.print(buffer); 100 101 if (hours == 0 && minutes < 20) 102 Serial.print(" Not yet valid"); 103 } 104 105 //printSensorError gets, clears, then prints the errors 106 //saved within the error register. 107 void printSensorError() 108 { 109 uint8_t error = myCCS811.getErrorRegister(); 110 111 if (error == 0xFF) //comm error 112 { 113 Serial.println("Failed to get ERROR_ID register."); 114 } 115 else 116 { 117 Serial.print("Error: "); 118 if (error & 1 << 5) 119 Serial.print("HeaterSupply"); 120 if (error & 1 << 4) 121 Serial.print("HeaterFault"); 122 if (error & 1 << 3) 123 Serial.print("MaxResistance"); 124 if (error & 1 << 2) 125 Serial.print("MeasModeInvalid"); 126 if (error & 1 << 1) 127 Serial.print("ReadRegInvalid"); 128 if (error & 1 << 0) 129 Serial.print("MsgInvalid"); 130 Serial.println(); 131 } 132 }