full code here
#include "pitches.h"
int melody[] = {
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_G4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_B4,
NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_C5,
NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_A4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
int noteDurations[] = {
8, 4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 8, 8, 4, 8, 8, 4, 8, 8, 2,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 6, 16,
4, 6, 16, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
4, 6, 16, 4, 6, 16, 4, 8, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
4, 6, 16, 4, 4, 2, 4, 4, 1
};
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22
//Define the pin connection
int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
int SW = 4;//SW->D4
const int interrupt0 = 0;// Interrupt 0 在 pin 2 上
int count = 0;//Define the count
int lastCLK = 0;//CLK initial value
static const int DHT_SENSOR_PIN = 8;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
int btncount;
int btn =7; // switch pin
int relayPin = 10;
int buzzer = 12;
void setup()
{
Serial.begin(9600);
pinMode(SW, INPUT);
digitalWrite(SW, HIGH);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
attachInterrupt(interrupt0, ClockChanged, CHANGE);//Set the interrupt 0 handler, trigger level change
pinMode(buzzer, OUTPUT);
noTone(buzzer);
pinMode(relayPin, OUTPUT);
}
//The interrupt handlers
void ClockChanged()
{
int clkValue = digitalRead(CLK);//Read the CLK pin level
int dtValue = digitalRead(DT);//Read the DT pin level
if (lastCLK != clkValue)
{
lastCLK = clkValue;
count += (clkValue != dtValue ? 1 : -1);//CLK and inconsistent DT + 1, otherwise - 1
Serial.print("count:");
Serial.println(count);
delay(200);
}
}
void loop()
{
// Check if button has been pressed
if ( digitalRead( btn ) == LOW )
{
const int button_threshold_ms = 2000;
int button_press_time_ms = 0;
int button_release_time_ms = 0;
int button_hold_time_ms = 0;
// Store millisecond count when button was pressed
button_press_time_ms = millis();
// Wait while button is pressed
while ( digitalRead( btn ) == LOW ) {}
// Store millisecond count when button was released
button_release_time_ms = millis();
// Calculate button hold time
button_hold_time_ms = button_release_time_ms - button_press_time_ms;
// Determine if button press was longer than threshold
if ( button_hold_time_ms >= button_threshold_ms )
{
Serial.println("Long press detected: ");
Serial.println(button_hold_time_ms);
Serial.println("ms");
btncount = 2;
Serial.println(btncount);
}
// Execute long press code here or set flag
if ( btncount < 2){
return;
// Wait while button is pressed
while ( btncount > 1 ){}
// Execute once button has been released
for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ )
{
// Calculate note duration and update
int noteDuration = 2000 / noteDurations[thisNote];
tone( buzzer, melody[thisNote], noteDuration );
// Calculate and execute note delay
int pauseBetweenNotes = noteDuration * 1.30;
delay( pauseBetweenNotes );
// Check for a button press
if ( digitalRead( btn ) == LOW )
--btncount;
{
// Wait while button is pressed
while ( btncount < 2 ){}
// Break from loop once button is released
break;
}
}
}
}
else
{
Serial.println("Short press detected: ");
Serial.println(button_hold_time_ms);
Serial.println("ms");
btncount = 1;
Serial.println(btncount);
}
// Execute short press code here or set flag
while(btncount == 1)
{
delay(200);
float temperature;
float humidity;
if(dht_sensor.measure(&temperature, &humidity))
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
Serial.println(count);
// max and min encoder settings resets 2 0
while( count <= -1 )
{
Serial.println("setting not allowed");
delay(1000);
Serial.println(count);
delay(200);
count = 0;
delay(1000);
}
// all is reading temp and adjusting the relay accoridingly
while (temperature <= count)
{
digitalWrite(relayPin , LOW);
delay(1000);
Serial.println("relay off above set temperature");
Serial.println(count);
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
while( count >= 34 )
{
Serial.println("setting not allowed");
delay(1000);
Serial.println(count);
delay(200);
count = 0;
delay(1000);
}
if (digitalRead( btn ) == LOW)
{
// -- btncount;
digitalWrite(relayPin, LOW);
delay(200);
return;
}
while (temperature >= count)
{
digitalWrite(relayPin, HIGH); // turn the relay off by making the voltage LOW
delay(1000);
Serial.println(count);
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
Serial.println("relay on, below set temperature");
if (digitalRead( btn ) == LOW)
{
// -- btncount;
Serial.println("off");
digitalWrite(relayPin, LOW);
delay(200);
return;
}
}
}
}
}
// encoder 0
if (!digitalRead(SW) && count != 0) //Read the button press and the count value to 0 when the counter reset
{
count = 0;
Serial.print("count:");
Serial.println(count);
}
}