brookedot

Untitled

Feb 26th, 2024
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /**************************************************************************
  2. This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3.  
  4. Pick one up today in the adafruit shop!
  5. ------> http://www.adafruit.com/category/63_98
  6.  
  7. This example is for a 128x64 pixel display using I2C to communicate
  8. 3 pins are required to interface (two I2C and one reset).
  9.  
  10. Adafruit invests time and resources providing this open
  11. source code, please support Adafruit and open-source
  12. hardware by purchasing products from Adafruit!
  13.  
  14. Written by Limor Fried/Ladyada for Adafruit Industries,
  15. with contributions from the open source community.
  16. BSD license, check license.txt for more information
  17. All text above, and the splash screen below must be
  18. included in any redistribution.
  19. **************************************************************************/
  20.  
  21. #include <SPI.h>
  22. #include <Wire.h>
  23. #include <Adafruit_GFX.h>
  24. #include <Adafruit_SSD1306.h>
  25. #include "Adafruit_SGP30.h"
  26.  
  27. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  28. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  29.  
  30. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  31. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  32. #define SCREEN_ADDRESS 0x3C
  33. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  34.  
  35. // Declaration for an SGP30 display connected to I2C (SDA, SCL pins)
  36. Adafruit_SGP30 sgp;
  37.  
  38. int counter = 0;
  39. int fanPin = 9;
  40.  
  41. void setup() {
  42. Serial.begin(9600);
  43. pinMode(fanPin, OUTPUT);
  44.  
  45. // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  46. if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  47. Serial.println(F("SSD1306 allocation failed"));
  48. for(;;); // Don't proceed, loop forever
  49. }
  50.  
  51. while (!Serial) { delay(10); } // Wait for serial console to open!
  52.  
  53. Serial.println("SGP30 test");
  54.  
  55. if (! sgp.begin()){
  56. Serial.println("Sensor not found :(");
  57. while (1);
  58. }
  59. Serial.print("Found SGP30 serial #");
  60. Serial.print(sgp.serialnumber[0], HEX);
  61. Serial.print(sgp.serialnumber[1], HEX);
  62. Serial.println(sgp.serialnumber[2], HEX);
  63.  
  64. // Clear the buffer
  65. display.clearDisplay();
  66. display.display();
  67. delay(2000);
  68.  
  69.  
  70. testdrawtriangle(); // Draw triangles (outlines)
  71.  
  72. testdrawchar(); // Draw characters of the default font
  73. }
  74.  
  75.  
  76. void loop() {
  77. // If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
  78. //float temperature = 22.1; // [°C]
  79. //float humidity = 45.2; // [%RH]
  80. //sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
  81.  
  82. if (! sgp.IAQmeasure()) {
  83. Serial.println("Measurement failed");
  84. return;
  85. }
  86. Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
  87. Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
  88.  
  89. if (! sgp.IAQmeasureRaw()) {
  90. Serial.println("Raw Measurement failed");
  91. return;
  92. }
  93. Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t");
  94. Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("");
  95.  
  96. delay(1000);
  97.  
  98. counter++;
  99. if (counter == 30) {
  100. counter = 0;
  101.  
  102. uint16_t TVOC_base, eCO2_base;
  103. if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
  104. Serial.println("Failed to get baseline readings");
  105. return;
  106. }
  107. Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
  108. Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
  109. }
  110.  
  111. // Fan off
  112. analogWrite(fanPin, 0);
  113. delay(3000);
  114.  
  115. // Medium speed
  116. analogWrite(fanPin, 150);
  117. delay(3000);
  118.  
  119. // Max speed
  120. analogWrite(fanPin, 255);
  121. delay(3000);
  122. }
  123.  
  124. void testdrawtriangle(void) {
  125. display.clearDisplay();
  126.  
  127. for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
  128. display.drawTriangle(
  129. display.width()/2 , display.height()/2-i,
  130. display.width()/2-i, display.height()/2+i,
  131. display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
  132. display.display();
  133. delay(1);
  134. }
  135.  
  136. delay(2000);
  137. }
  138.  
  139.  
  140. void testdrawchar(void) {
  141. display.clearDisplay();
  142.  
  143. display.setTextSize(2); // Normal 1:1 pixel scale
  144. display.setTextColor(SSD1306_WHITE); // Draw white text
  145. display.setCursor(0, 0); // Start at top-left corner
  146.  
  147. display.cp437(true); // Use correct CP437 character codes
  148.  
  149. display.printf("TVOC: %d ppb\n", sgp.TVOC);
  150. display.printf("eCO2: %d ppm\n", sgp.eCO2);
  151.  
  152. display.println(); // New line
  153. display.display();
  154. delay(2000);
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
Advertisement
Add Comment
Please, Sign In to add comment