#include "DHTesp.h"
const int DHT_PIN = 15;
const int LED_PIN = 4;
const int BUZZER_PIN = 23;
DHTesp dhtSensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LED_PIN,OUTPUT);
pinMode(BUZZER_PIN,OUTPUT);
digitalWrite(LED_PIN,HIGH);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
/* tone(BUZZER_PIN, 262, 250);
tone(BUZZER_PIN, 293, 250);
tone(BUZZER_PIN, 329, 250);
tone(BUZZER_PIN, 392, 250);
tone(BUZZER_PIN, 349, 250);
tone(BUZZER_PIN, 440, 250);
tone(BUZZER_PIN, 493, 250);
tone(BUZZER_PIN, 523, 250);
*/
/* int TONS_SIZE = 8;
int tons[TONS_SIZE]={262,293,329,392,349,440,493,523};
int ton_seconds[TONS_SIZE] = {500,250,250,500,250,250,250, 500};
for(int i=0;i<TONS_SIZE;i++)
tone(BUZZER_PIN, tons[i], ton_seconds[i]);
*/
int tons[2][8]={{262,293,329,392,349,440,493,523},{250,250,250,250,250,250,250, 250}};
for(int i=0;i<1;i++)
for(int j=0;j<8;j++)
tone(BUZZER_PIN, tons[i][j], tons[i+1][j]);
/*另一種 寫法
tone(BUZZER_PIN,262)
delay(250);
noTone(BUZZER_PIN);*/
}
// 2-D Array(2x8)
/*int TONS_SIZE = 8;
int tons_2d[2][TONS_SIZE] = {
{262,293,329,349,392,440,493,523}
{500,250,250,500,250,250,250,500}
};
for(int i=0;i<TONS_SIZE ;i++)
tone(BUZZER_PIN, tons_2d[0][i], tons_2d[1][i]);
}
*/
// 2-D Array(2x8)
/*int TONS_SIZE = 8;
int tons_2d[TONS_SIZE][2] = {
{262,500},
{293,250},
{329,250},
{349,500},
{392,250},
{400,250},
{493,250},
{523,500}
};
for(int i=0;i<TONS_SIZE ;i++)
for(int j=0; j<1;j++)
tone(BUZZER_PIN, tons_2d[i][0], tons_2d[i][0]);
}
*/
void loop(){
printTempAndHumidity(); // call
delay(2000);
}
void printTempAndHumidity() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}