|
| 1 | +// SPDX-FileCopyrightText: 2024 Limor Fried for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +/*************************************************** |
| 6 | + This is an example for the SHT4x Humidity & Temp Trinkey |
| 7 | + ****************************************************/ |
| 8 | + |
| 9 | +#include <Adafruit_SHT4x.h> |
| 10 | +#include <Adafruit_NeoPixel.h> |
| 11 | +#include <Adafruit_FreeTouch.h> |
| 12 | + |
| 13 | + |
| 14 | +Adafruit_SHT4x sht4 = Adafruit_SHT4x(); |
| 15 | +Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); |
| 16 | +Adafruit_FreeTouch touch = Adafruit_FreeTouch(1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); |
| 17 | + |
| 18 | +void setup() { |
| 19 | + Serial.begin(115200); |
| 20 | + |
| 21 | + pixel.begin(); // Initialize pins for output |
| 22 | + pixel.setBrightness(10); |
| 23 | + pixel.show(); // Turn off ASAP |
| 24 | + |
| 25 | + uint8_t i=0; |
| 26 | + while (!Serial) { |
| 27 | + // swirl the neopix while we wait |
| 28 | + pixel.setPixelColor(0, Wheel(i++)); |
| 29 | + pixel.show(); |
| 30 | + delay(10); // will pause until serial console opens |
| 31 | + } |
| 32 | + pixel.setPixelColor(0, 0); |
| 33 | + pixel.show(); |
| 34 | + |
| 35 | + if (! touch.begin()) |
| 36 | + Serial.println("Failed to begin QTouch on pin 1"); |
| 37 | + |
| 38 | + Serial.println("# Adafruit SHT4x Trinkey Factory Test"); |
| 39 | + if (! sht4.begin()) { |
| 40 | + Serial.println("# Couldn't find SHT4x"); |
| 41 | + while (1) delay(1); |
| 42 | + } |
| 43 | + Serial.print("# Found SHT4x sensor. "); |
| 44 | + Serial.print("Serial number 0x"); |
| 45 | + Serial.println(sht4.readSerial(), HEX); |
| 46 | + |
| 47 | + sht4.setPrecision(SHT4X_HIGH_PRECISION); |
| 48 | + sht4.setHeater(SHT4X_NO_HEATER); |
| 49 | + Serial.println("# Serial number, Temperature in *C, Relative Humidity %, Touch"); |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +void loop() { |
| 54 | + sensors_event_t humidity, temp; |
| 55 | + |
| 56 | + uint32_t timestamp = millis(); |
| 57 | + sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data |
| 58 | + timestamp = millis() - timestamp; |
| 59 | + |
| 60 | + Serial.print(sht4.readSerial()); |
| 61 | + Serial.print(", "); |
| 62 | + Serial.print(temp.temperature); |
| 63 | + Serial.print(", "); |
| 64 | + Serial.print(humidity.relative_humidity); |
| 65 | + Serial.print(", "); |
| 66 | + Serial.println(touch.measure()); |
| 67 | + |
| 68 | + // blink! |
| 69 | + pixel.setPixelColor(0, 0x0000FF); |
| 70 | + pixel.show(); |
| 71 | + delay(25); |
| 72 | + pixel.setPixelColor(0, 0x0); |
| 73 | + pixel.show(); |
| 74 | + |
| 75 | + // 1 second between readings |
| 76 | + delay(1000); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +// Input a value 0 to 255 to get a color value. |
| 81 | +// The colours are a transition r - g - b - back to r. |
| 82 | +uint32_t Wheel(byte WheelPos) { |
| 83 | + WheelPos = 255 - WheelPos; |
| 84 | + if(WheelPos < 85) { |
| 85 | + return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 86 | + } |
| 87 | + if(WheelPos < 170) { |
| 88 | + WheelPos -= 85; |
| 89 | + return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 90 | + } |
| 91 | + WheelPos -= 170; |
| 92 | + return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 93 | +} |
0 commit comments