qt2040_mcp9808_example.ino
1 // SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 /**************************************************************************/ 6 /*! 7 This is a demo for the Adafruit QT2040 Trinkey and the MCP9808 temperature 8 sensor. 9 QT2040 Trinkey - https://www.adafruit.com/product/5056 10 MCP9808 - https://www.adafruit.com/product/5027 11 12 */ 13 /**************************************************************************/ 14 #include "Adafruit_MCP9808.h" 15 #include <Adafruit_NeoPixel.h> 16 17 // Create the neopixel strip with the built in definitions NUM_NEOPIXEL and 18 // PIN_NEOPIXEL 19 Adafruit_NeoPixel pixel = 20 Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); 21 22 // Create the MCP9808 temperature sensor object 23 Adafruit_MCP9808 mcp9808 = Adafruit_MCP9808(); 24 25 long previousMillis = 0; 26 long intervalTemp = 2000; 27 bool last_button = false; 28 29 void setup() { 30 Serial.begin(115200); 31 delay(100); 32 33 pinMode(PIN_SWITCH, INPUT_PULLUP); // Setup the BOOT button 34 35 pixel.begin(); 36 pixel.setBrightness(20); 37 pixel.show(); // Initialize all pixels to 'off' 38 39 if (!mcp9808.begin(0x18)) { 40 Serial.println("Couldn't find MCP9808! Check your connections and verify " 41 "the address is correct."); 42 while (1) 43 ; 44 } 45 46 mcp9808.setResolution(3); 47 } 48 49 uint8_t j = 0; 50 51 void loop() { 52 bool curr_button = !digitalRead(PIN_SWITCH); 53 54 pixel.setPixelColor(0, Wheel(j++)); 55 pixel.show(); 56 57 unsigned long currentMillis = millis(); 58 if (currentMillis - previousMillis > intervalTemp) { 59 previousMillis = currentMillis; 60 // Read and print out the temperature. 61 float c = mcp9808.readTempC(); 62 float f = mcp9808.readTempF(); 63 Serial.print("Temp: "); 64 Serial.print(c, 4); 65 Serial.print("*C\t and "); 66 Serial.print(f, 4); 67 Serial.println("*F."); 68 } 69 70 if (curr_button && !last_button) { 71 Serial.println("Button pressed!"); 72 } 73 if (!curr_button && last_button) { 74 !Serial.println("Button released!"); 75 } 76 last_button = curr_button; 77 78 delay(10); 79 } 80 81 // Input a value 0 to 255 to get a color value. 82 // The colours are a transition r - g - b - back to r. 83 uint32_t Wheel(byte WheelPos) { 84 if (WheelPos < 85) { 85 return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 86 } else if (WheelPos < 170) { 87 WheelPos -= 85; 88 return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3); 89 } else { 90 WheelPos -= 170; 91 return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3); 92 } 93 }