|
| 1 | +#include <Adafruit_DotStar.h> |
| 2 | +#include <Adafruit_GFX.h> // Core graphics library |
| 3 | +#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 |
| 4 | +#include <Adafruit_DPS310.h> |
| 5 | +#include <Adafruit_AHTX0.h> |
| 6 | + |
| 7 | +#define NUM_DOTSTAR 5 |
| 8 | +#define BG_COLOR ST77XX_BLACK |
| 9 | + |
| 10 | +// display! |
| 11 | +Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RESET); |
| 12 | +// LEDs! |
| 13 | +Adafruit_DotStar pixels(NUM_DOTSTAR, PIN_DOTSTAR_DATA, PIN_DOTSTAR_CLOCK, DOTSTAR_BRG); |
| 14 | +// sensors! |
| 15 | +Adafruit_DPS310 dps; |
| 16 | +Adafruit_AHTX0 aht; |
| 17 | + |
| 18 | +uint8_t LED_dutycycle = 0; |
| 19 | +uint16_t firstPixelHue = 0; |
| 20 | + |
| 21 | +void setup() { |
| 22 | + Serial.begin(115200); |
| 23 | + delay(100); |
| 24 | + |
| 25 | + pixels.begin(); // Initialize pins for output |
| 26 | + pixels.show(); // Turn all LEDs off ASAP |
| 27 | + pixels.setBrightness(20); |
| 28 | + |
| 29 | + pinMode(BUTTON_DOWN, INPUT_PULLDOWN); |
| 30 | + pinMode(BUTTON_SELECT, INPUT_PULLDOWN); |
| 31 | + pinMode(BUTTON_UP, INPUT_PULLDOWN); |
| 32 | + |
| 33 | + //analogReadResolution(13); |
| 34 | + |
| 35 | + tft.init(240, 240); // Initialize ST7789 screen |
| 36 | + pinMode(TFT_BACKLIGHT, OUTPUT); |
| 37 | + digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on |
| 38 | + |
| 39 | + tft.fillScreen(BG_COLOR); |
| 40 | + tft.setTextSize(2); |
| 41 | + tft.setTextColor(ST77XX_YELLOW); |
| 42 | + tft.setTextWrap(false); |
| 43 | + |
| 44 | + // check DPS! |
| 45 | + tft.setCursor(0, 0); |
| 46 | + tft.setTextColor(ST77XX_YELLOW); |
| 47 | + tft.print("DP310? "); |
| 48 | + |
| 49 | + |
| 50 | + if (! dps.begin_I2C()) { |
| 51 | + tft.setTextColor(ST77XX_RED); |
| 52 | + tft.println("FAIL!"); |
| 53 | + while (1) delay(100); |
| 54 | + } |
| 55 | + tft.setTextColor(ST77XX_GREEN); |
| 56 | + tft.println("OK!"); |
| 57 | + dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES); |
| 58 | + dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES); |
| 59 | + |
| 60 | + // check AHT! |
| 61 | + tft.setCursor(0, 20); |
| 62 | + tft.setTextColor(ST77XX_YELLOW); |
| 63 | + tft.print("AHT20? "); |
| 64 | + |
| 65 | + if (! aht.begin()) { |
| 66 | + tft.setTextColor(ST77XX_RED); |
| 67 | + tft.println("FAIL!"); |
| 68 | + while (1) delay(100); |
| 69 | + } |
| 70 | + tft.setTextColor(ST77XX_GREEN); |
| 71 | + tft.println("OK!"); |
| 72 | + |
| 73 | + pinMode(LED_BUILTIN, OUTPUT); |
| 74 | + pinMode(SPEAKER, OUTPUT); |
| 75 | + |
| 76 | + ledcSetup(0, 2000 * 80, 8); |
| 77 | + ledcAttachPin(LED_BUILTIN, 0); |
| 78 | + |
| 79 | + ledcSetup(1, 2000 * 80, 8); |
| 80 | + ledcAttachPin(SPEAKER, 1); |
| 81 | + ledcWrite(1, 0); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +void loop() { |
| 87 | + |
| 88 | + |
| 89 | + /********************* sensors */ |
| 90 | + sensors_event_t humidity, temp, pressure; |
| 91 | + |
| 92 | + tft.setCursor(0, 0); |
| 93 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 94 | + dps.getEvents(&temp, &pressure); |
| 95 | + |
| 96 | + tft.print("DP310: "); |
| 97 | + tft.print(temp.temperature, 0); |
| 98 | + tft.print(" C "); |
| 99 | + tft.print(pressure.pressure, 0); |
| 100 | + tft.print(" hPa"); |
| 101 | + tft.println(" "); |
| 102 | + Serial.printf("DPS310: %0.1f *C %0.2f hPa\n", temp.temperature, pressure.pressure); |
| 103 | + |
| 104 | + |
| 105 | + tft.setCursor(0, 20); |
| 106 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 107 | + aht.getEvent(&humidity, &temp); |
| 108 | + |
| 109 | + tft.print("AHT20: "); |
| 110 | + tft.print(temp.temperature, 0); |
| 111 | + tft.print(" C "); |
| 112 | + tft.print(humidity.relative_humidity, 0); |
| 113 | + tft.print(" %"); |
| 114 | + tft.println(" "); |
| 115 | + Serial.printf("AHT20: %0.1f *C %0.2f rH\n", temp.temperature, humidity.relative_humidity); |
| 116 | + |
| 117 | + |
| 118 | + /****************** BUTTONS */ |
| 119 | + tft.setCursor(0, 40); |
| 120 | + tft.setTextColor(ST77XX_YELLOW); |
| 121 | + tft.print("Buttons: "); |
| 122 | + if (! digitalRead(BUTTON_DOWN)) { |
| 123 | + tft.setTextColor(0x808080); |
| 124 | + } else { |
| 125 | + Serial.println("DOWN pressed"); |
| 126 | + tft.setTextColor(ST77XX_WHITE); |
| 127 | + } |
| 128 | + tft.print("DOWN "); |
| 129 | + |
| 130 | + if (! digitalRead(BUTTON_SELECT)) { |
| 131 | + tft.setTextColor(0x808080); |
| 132 | + } else { |
| 133 | + Serial.println("SELECT pressed"); |
| 134 | + tft.setTextColor(ST77XX_WHITE); |
| 135 | + } |
| 136 | + tft.print("SEL "); |
| 137 | + |
| 138 | + if (! digitalRead(BUTTON_UP)) { |
| 139 | + tft.setTextColor(0x808080); |
| 140 | + } else { |
| 141 | + Serial.println("UP pressed"); |
| 142 | + tft.setTextColor(ST77XX_WHITE); |
| 143 | + } |
| 144 | + tft.println("UP"); |
| 145 | + |
| 146 | + /************************** CAPACITIVE */ |
| 147 | + uint16_t touchread; |
| 148 | + |
| 149 | + tft.setCursor(0, 60); |
| 150 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 151 | + tft.print("Captouch 6: "); |
| 152 | + touchread = touchRead(6); |
| 153 | + if (touchread < 10000 ) { |
| 154 | + tft.setTextColor(0x808080, BG_COLOR); |
| 155 | + } else { |
| 156 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 157 | + } |
| 158 | + tft.print(touchread); |
| 159 | + tft.println(" "); |
| 160 | + Serial.printf("Captouch #6 reading: %d\n", touchread); |
| 161 | + |
| 162 | + tft.setCursor(0, 80); |
| 163 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 164 | + tft.print("Captouch 7: "); |
| 165 | + touchread = touchRead(7); |
| 166 | + if (touchread < 20000 ) { |
| 167 | + tft.setTextColor(0x808080, BG_COLOR); |
| 168 | + } else { |
| 169 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 170 | + } |
| 171 | + tft.print(touchread); |
| 172 | + tft.println(" "); |
| 173 | + Serial.printf("Captouch #7 reading: %d\n", touchread); |
| 174 | + |
| 175 | + |
| 176 | + tft.setCursor(0, 100); |
| 177 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 178 | + tft.print("Captouch 8: "); |
| 179 | + touchread = touchRead(8); |
| 180 | + if (touchread < 20000 ) { |
| 181 | + tft.setTextColor(0x808080, BG_COLOR); |
| 182 | + } else { |
| 183 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 184 | + } |
| 185 | + tft.print(touchread); |
| 186 | + tft.println(" "); |
| 187 | + Serial.printf("Captouch #8 reading: %d\n", touchread); |
| 188 | + |
| 189 | + |
| 190 | + /************************** ANALOG READ */ |
| 191 | + uint16_t analogread; |
| 192 | + |
| 193 | + tft.setCursor(0, 120); |
| 194 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 195 | + tft.print("Analog 0: "); |
| 196 | + analogread = analogRead(A0); |
| 197 | + if (analogread < 8000 ) { |
| 198 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 199 | + } else { |
| 200 | + tft.setTextColor(ST77XX_RED, BG_COLOR); |
| 201 | + } |
| 202 | + tft.print(analogread); |
| 203 | + tft.println(" "); |
| 204 | + Serial.printf("Analog A0 reading: %d\n", analogread); |
| 205 | + |
| 206 | + |
| 207 | + tft.setCursor(0, 140); |
| 208 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 209 | + tft.print("Analog 1: "); |
| 210 | + analogread = analogRead(A1); |
| 211 | + if (analogread < 8000 ) { |
| 212 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 213 | + } else { |
| 214 | + tft.setTextColor(ST77XX_RED, BG_COLOR); |
| 215 | + } |
| 216 | + tft.print(analogread); |
| 217 | + tft.println(" "); |
| 218 | + Serial.printf("Analog A1 reading: %d\n", analogread); |
| 219 | + |
| 220 | + |
| 221 | + tft.setCursor(0, 160); |
| 222 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 223 | + tft.print("Analog 2: "); |
| 224 | + analogread = analogRead(A2); |
| 225 | + if (analogread < 8000 ) { |
| 226 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 227 | + } else { |
| 228 | + tft.setTextColor(ST77XX_RED, BG_COLOR); |
| 229 | + } |
| 230 | + tft.print(analogread); |
| 231 | + tft.println(" "); |
| 232 | + Serial.printf("Analog A2 reading: %d\n", analogread); |
| 233 | + |
| 234 | + tft.setCursor(0, 180); |
| 235 | + tft.setTextColor(ST77XX_YELLOW, BG_COLOR); |
| 236 | + tft.print("Light: "); |
| 237 | + analogread = analogRead(A3); |
| 238 | + tft.setTextColor(ST77XX_WHITE, BG_COLOR); |
| 239 | + tft.print(analogread); |
| 240 | + tft.println(" "); |
| 241 | + Serial.printf("Light sensor reading: %d\n", analogread); |
| 242 | + |
| 243 | + /************************** Beep! */ |
| 244 | + if (digitalRead(BUTTON_SELECT)) { |
| 245 | + Serial.println("** Beep! ***"); |
| 246 | + tone(SPEAKER, 988, 100); // tone1 - B5 |
| 247 | + tone(SPEAKER, 1319, 200); // tone2 - E6 |
| 248 | + delay(100); |
| 249 | + //tone(SPEAKER, 2000, 100); |
| 250 | + } |
| 251 | + |
| 252 | + /************************** LEDs */ |
| 253 | + // pulse red LED |
| 254 | + ledcWrite(0, LED_dutycycle); |
| 255 | + LED_dutycycle += 32; |
| 256 | + |
| 257 | + // rainbow dotstars |
| 258 | + for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip... |
| 259 | + int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels()); |
| 260 | + pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue))); |
| 261 | + } |
| 262 | + pixels.show(); // Update strip with new contents |
| 263 | + firstPixelHue += 256; |
| 264 | +} |
| 265 | + |
| 266 | + |
| 267 | +void tone(uint8_t pin, float frequecy, float duration) { |
| 268 | + ledcSetup(1, frequecy * 80, 8); |
| 269 | + ledcAttachPin(pin, 1); |
| 270 | + ledcWrite(1, 128); |
| 271 | + delay(duration); |
| 272 | + ledcWrite(1, 0); |
| 273 | +} |
0 commit comments