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