@@ -27,7 +27,7 @@ void setup() {
2727 // while (!Serial);
2828 Serial.begin (115200 );
2929 delay (100 );
30-
30+
3131 pixels.begin (); // Initialize pins for output
3232 pixels.show (); // Turn all LEDs off ASAP
3333 pixels.setBrightness (20 );
@@ -37,7 +37,7 @@ void setup() {
3737 pinMode (BUTTON_UP, INPUT_PULLDOWN);
3838
3939 // analogReadResolution(13);
40-
40+
4141 tft.init (240 , 240 ); // Initialize ST7789 screen
4242 pinMode (TFT_BACKLIGHT, OUTPUT);
4343 digitalWrite (TFT_BACKLIGHT, HIGH); // Backlight on
@@ -52,8 +52,8 @@ void setup() {
5252 tft.setTextColor (ST77XX_YELLOW);
5353 tft.print (" DP310? " );
5454
55-
56- if (! dps.begin_I2C ()) {
55+
56+ if (! dps.begin_I2C ()) {
5757 tft.setTextColor (ST77XX_RED);
5858 tft.println (" FAIL!" );
5959 while (1 ) delay (100 );
@@ -67,8 +67,8 @@ void setup() {
6767 tft.setCursor (0 , 20 );
6868 tft.setTextColor (ST77XX_YELLOW);
6969 tft.print (" AHT20? " );
70-
71- if (! aht.begin ()) {
70+
71+ if (! aht.begin ()) {
7272 tft.setTextColor (ST77XX_RED);
7373 tft.println (" FAIL!" );
7474 while (1 ) delay (100 );
@@ -79,12 +79,18 @@ void setup() {
7979 pinMode (LED_BUILTIN, OUTPUT);
8080 pinMode (SPEAKER, OUTPUT);
8181
82+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
83+ ledcAttach (LED_BUILTIN, 2000 , 8 );
84+ ledcAttach (SPEAKER, 2000 , 8 );
85+ ledcWrite (SPEAKER, 0 );
86+ #else
8287 ledcSetup (0 , 2000 , 8 );
8388 ledcAttachPin (LED_BUILTIN, 0 );
8489
8590 ledcSetup (1 , 2000 , 8 );
8691 ledcAttachPin (SPEAKER, 1 );
8792 ledcWrite (1 , 0 );
93+ #endif
8894}
8995
9096
@@ -94,11 +100,11 @@ void loop() {
94100
95101 /* ******************** sensors */
96102 sensors_event_t humidity, temp, pressure;
97-
103+
98104 tft.setCursor (0 , 0 );
99105 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
100106 dps.getEvents (&temp, &pressure);
101-
107+
102108 tft.print (" DP310: " );
103109 tft.print (temp.temperature , 0 );
104110 tft.print (" C " );
@@ -125,23 +131,23 @@ void loop() {
125131 tft.setCursor (0 , 40 );
126132 tft.setTextColor (ST77XX_YELLOW);
127133 tft.print (" Buttons: " );
128- if (! digitalRead (BUTTON_DOWN)) {
134+ if (! digitalRead (BUTTON_DOWN)) {
129135 tft.setTextColor (ST77XX_GREY);
130136 } else {
131137 Serial.println (" DOWN pressed" );
132138 tft.setTextColor (ST77XX_WHITE);
133139 }
134140 tft.print (" DOWN " );
135141
136- if (! digitalRead (BUTTON_SELECT)) {
142+ if (! digitalRead (BUTTON_SELECT)) {
137143 tft.setTextColor (ST77XX_GREY);
138144 } else {
139145 Serial.println (" SELECT pressed" );
140146 tft.setTextColor (ST77XX_WHITE);
141147 }
142148 tft.print (" SEL " );
143-
144- if (! digitalRead (BUTTON_UP)) {
149+
150+ if (! digitalRead (BUTTON_UP)) {
145151 tft.setTextColor (ST77XX_GREY);
146152 } else {
147153 Serial.println (" UP pressed" );
@@ -151,25 +157,25 @@ void loop() {
151157
152158 /* ************************* CAPACITIVE */
153159 uint16_t touchread;
154-
160+
155161 tft.setCursor (0 , 60 );
156162 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
157163 tft.print (" Captouch 6: " );
158164 touchread = touchRead (6 );
159- if (touchread < 10000 ) {
165+ if (touchread < 10000 ) {
160166 tft.setTextColor (ST77XX_GREY, BG_COLOR);
161167 } else {
162168 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
163169 }
164170 tft.print (touchread);
165171 tft.println (" " );
166172 Serial.printf (" Captouch #6 reading: %d\n " , touchread);
167-
173+
168174 tft.setCursor (0 , 80 );
169175 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
170176 tft.print (" Captouch 7: " );
171177 touchread = touchRead (7 );
172- if (touchread < 20000 ) {
178+ if (touchread < 20000 ) {
173179 tft.setTextColor (ST77XX_GREY, BG_COLOR);
174180 } else {
175181 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
@@ -183,7 +189,7 @@ void loop() {
183189 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
184190 tft.print (" Captouch 8: " );
185191 touchread = touchRead (8 );
186- if (touchread < 20000 ) {
192+ if (touchread < 20000 ) {
187193 tft.setTextColor (ST77XX_GREY, BG_COLOR);
188194 } else {
189195 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
@@ -200,7 +206,7 @@ void loop() {
200206 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
201207 tft.print (" Analog 0: " );
202208 analogread = analogRead (A0);
203- if (analogread < 8000 ) {
209+ if (analogread < 8000 ) {
204210 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
205211 } else {
206212 tft.setTextColor (ST77XX_RED, BG_COLOR);
@@ -214,7 +220,7 @@ void loop() {
214220 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
215221 tft.print (" Analog 1: " );
216222 analogread = analogRead (A1);
217- if (analogread < 8000 ) {
223+ if (analogread < 8000 ) {
218224 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
219225 } else {
220226 tft.setTextColor (ST77XX_RED, BG_COLOR);
@@ -223,12 +229,12 @@ void loop() {
223229 tft.println (" " );
224230 Serial.printf (" Analog A1 reading: %d\n " , analogread);
225231
226-
232+
227233 tft.setCursor (0 , 160 );
228234 tft.setTextColor (ST77XX_YELLOW, BG_COLOR);
229235 tft.print (" Analog 2: " );
230236 analogread = analogRead (A2);
231- if (analogread < 8000 ) {
237+ if (analogread < 8000 ) {
232238 tft.setTextColor (ST77XX_WHITE, BG_COLOR);
233239 } else {
234240 tft.setTextColor (ST77XX_RED, BG_COLOR);
@@ -245,21 +251,25 @@ void loop() {
245251 tft.print (analogread);
246252 tft.println (" " );
247253 Serial.printf (" Light sensor reading: %d\n " , analogread);
248-
254+
249255 /* ************************* Beep! */
250- if (digitalRead (BUTTON_SELECT)) {
256+ if (digitalRead (BUTTON_SELECT)) {
251257 Serial.println (" ** Beep! ***" );
252258 fhtone (SPEAKER, 988.0 , 100.0 ); // tone1 - B5
253259 fhtone (SPEAKER, 1319.0 , 200.0 ); // tone2 - E6
254260 delay (100 );
255261 // fhtone(SPEAKER, 2000.0, 100.0);
256262 }
257-
263+
258264 /* ************************* LEDs */
259265 // pulse red LED
266+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
267+ ledcWrite (LED_BUILTIN, LED_dutycycle);
268+ #else
260269 ledcWrite (0 , LED_dutycycle);
270+ #endif
261271 LED_dutycycle += 32 ;
262-
272+
263273 // rainbow dotstars
264274 for (int i=0 ; i<pixels.numPixels (); i++) { // For each pixel in strip...
265275 int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels ());
@@ -271,9 +281,16 @@ void loop() {
271281
272282
273283void fhtone (uint8_t pin, float frequency, float duration) {
284+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
285+ ledcAttach (SPEAKER, frequency, 8 );
286+ ledcWrite (SPEAKER, 128 );
287+ delay (duration);
288+ ledcWrite (SPEAKER, 0 );
289+ #else
274290 ledcSetup (1 , frequency, 8 );
275291 ledcAttachPin (pin, 1 );
276292 ledcWrite (1 , 128 );
277293 delay (duration);
278294 ledcWrite (1 , 0 );
295+ #endif
279296}
0 commit comments