Adafruit_Learning_System_Gu.../FunHouse_Arduino_Demos/selftest/selftest.ino
Anne Barela 2ff396eff7
Disambiguate tone function
The program included a tone function which is also an Arduino built-in function so the compiler threw warnings it didn't know which to use. Changed the local function to fhtone to disambiguate as to which function to use and changed parameters to floats per the function spec.
2022-03-30 12:03:59 -04:00

279 lines
7.1 KiB
C++

// SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <Adafruit_DotStar.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <Adafruit_DPS310.h>
#include <Adafruit_AHTX0.h>
#define NUM_DOTSTAR 5
#define BG_COLOR ST77XX_BLACK
#define ST77XX_GREY 0x8410 // Colors are in RGB565 format
// display!
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RESET);
// LEDs!
Adafruit_DotStar pixels(NUM_DOTSTAR, PIN_DOTSTAR_DATA, PIN_DOTSTAR_CLOCK, DOTSTAR_BRG);
// sensors!
Adafruit_DPS310 dps;
Adafruit_AHTX0 aht;
uint8_t LED_dutycycle = 0;
uint16_t firstPixelHue = 0;
void setup() {
//while (!Serial);
Serial.begin(115200);
delay(100);
pixels.begin(); // Initialize pins for output
pixels.show(); // Turn all LEDs off ASAP
pixels.setBrightness(20);
pinMode(BUTTON_DOWN, INPUT_PULLDOWN);
pinMode(BUTTON_SELECT, INPUT_PULLDOWN);
pinMode(BUTTON_UP, INPUT_PULLDOWN);
//analogReadResolution(13);
tft.init(240, 240); // Initialize ST7789 screen
pinMode(TFT_BACKLIGHT, OUTPUT);
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
tft.fillScreen(BG_COLOR);
tft.setTextSize(2);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextWrap(false);
// check DPS!
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_YELLOW);
tft.print("DP310? ");
if (! dps.begin_I2C()) {
tft.setTextColor(ST77XX_RED);
tft.println("FAIL!");
while (1) delay(100);
}
tft.setTextColor(ST77XX_GREEN);
tft.println("OK!");
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
// check AHT!
tft.setCursor(0, 20);
tft.setTextColor(ST77XX_YELLOW);
tft.print("AHT20? ");
if (! aht.begin()) {
tft.setTextColor(ST77XX_RED);
tft.println("FAIL!");
while (1) delay(100);
}
tft.setTextColor(ST77XX_GREEN);
tft.println("OK!");
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SPEAKER, OUTPUT);
ledcSetup(0, 2000, 8);
ledcAttachPin(LED_BUILTIN, 0);
ledcSetup(1, 2000, 8);
ledcAttachPin(SPEAKER, 1);
ledcWrite(1, 0);
}
void loop() {
/********************* sensors */
sensors_event_t humidity, temp, pressure;
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
dps.getEvents(&temp, &pressure);
tft.print("DP310: ");
tft.print(temp.temperature, 0);
tft.print(" C ");
tft.print(pressure.pressure, 0);
tft.print(" hPa");
tft.println(" ");
Serial.printf("DPS310: %0.1f *C %0.2f hPa\n", temp.temperature, pressure.pressure);
tft.setCursor(0, 20);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
aht.getEvent(&humidity, &temp);
tft.print("AHT20: ");
tft.print(temp.temperature, 0);
tft.print(" C ");
tft.print(humidity.relative_humidity, 0);
tft.print(" %");
tft.println(" ");
Serial.printf("AHT20: %0.1f *C %0.2f rH\n", temp.temperature, humidity.relative_humidity);
/****************** BUTTONS */
tft.setCursor(0, 40);
tft.setTextColor(ST77XX_YELLOW);
tft.print("Buttons: ");
if (! digitalRead(BUTTON_DOWN)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("DOWN pressed");
tft.setTextColor(ST77XX_WHITE);
}
tft.print("DOWN ");
if (! digitalRead(BUTTON_SELECT)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("SELECT pressed");
tft.setTextColor(ST77XX_WHITE);
}
tft.print("SEL ");
if (! digitalRead(BUTTON_UP)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("UP pressed");
tft.setTextColor(ST77XX_WHITE);
}
tft.println("UP");
/************************** CAPACITIVE */
uint16_t touchread;
tft.setCursor(0, 60);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 6: ");
touchread = touchRead(6);
if (touchread < 10000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
}
tft.print(touchread);
tft.println(" ");
Serial.printf("Captouch #6 reading: %d\n", touchread);
tft.setCursor(0, 80);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 7: ");
touchread = touchRead(7);
if (touchread < 20000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
}
tft.print(touchread);
tft.println(" ");
Serial.printf("Captouch #7 reading: %d\n", touchread);
tft.setCursor(0, 100);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 8: ");
touchread = touchRead(8);
if (touchread < 20000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
}
tft.print(touchread);
tft.println(" ");
Serial.printf("Captouch #8 reading: %d\n", touchread);
/************************** ANALOG READ */
uint16_t analogread;
tft.setCursor(0, 120);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 0: ");
analogread = analogRead(A0);
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
}
tft.print(analogread);
tft.println(" ");
Serial.printf("Analog A0 reading: %d\n", analogread);
tft.setCursor(0, 140);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 1: ");
analogread = analogRead(A1);
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
}
tft.print(analogread);
tft.println(" ");
Serial.printf("Analog A1 reading: %d\n", analogread);
tft.setCursor(0, 160);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 2: ");
analogread = analogRead(A2);
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
}
tft.print(analogread);
tft.println(" ");
Serial.printf("Analog A2 reading: %d\n", analogread);
tft.setCursor(0, 180);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Light: ");
analogread = analogRead(A3);
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
tft.print(analogread);
tft.println(" ");
Serial.printf("Light sensor reading: %d\n", analogread);
/************************** Beep! */
if (digitalRead(BUTTON_SELECT)) {
Serial.println("** Beep! ***");
fhtone(SPEAKER, 988.0, 100.0); // tone1 - B5
fhtone(SPEAKER, 1319.0, 200.0); // tone2 - E6
delay(100);
//fhtone(SPEAKER, 2000.0, 100.0);
}
/************************** LEDs */
// pulse red LED
ledcWrite(0, LED_dutycycle);
LED_dutycycle += 32;
// rainbow dotstars
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
}
pixels.show(); // Update strip with new contents
firstPixelHue += 256;
}
void fhtone(uint8_t pin, float frequency, float duration) {
ledcSetup(1, frequency, 8);
ledcAttachPin(pin, 1);
ledcWrite(1, 128);
delay(duration);
ledcWrite(1, 0);
}