hey, im trying to get the button when pushed shortly to make the button count 1 and a 2 if held.
#include "pitches.h"
int buzzer = 11;
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;
unsigned long currentMillis; // Variabele to store the number of milleseconds since the Arduino has started
int buttonStatePrevious = HIGH; // previousstate of the switch
static const int buttonPin =7; // switch pin
unsigned long minButtonLongPressDuration = 3000; // Time we wait before we see the press as a long press
unsigned long buttonLongPressMillis; // Time in ms when we the button was pressed
bool buttonStateLongPress = false; // True if it is a long press
const int intervalButton = 50; // Time between two readings of the button state
unsigned long previousButtonMillis; // Timestamp of the latest reading
unsigned long buttonPressDuration; // Time the button is pressed in ms
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);
currentMillis = millis(); // store the current time
readButtonState();
}
//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);
}
}
// Function for reading the button state
void readButtonState() {
// If the difference in time between the previous reading is larger than intervalButton
if(currentMillis - previousButtonMillis > intervalButton) {
// Read the digital value of the button (LOW/HIGH)
int buttonState = digitalRead(buttonPin);
// If the button has been pushed AND
// If the button wasn't pressed before AND
// IF there was not already a measurement running to determine how long the button has been pressed
if (buttonState == LOW && buttonStatePrevious == HIGH && !buttonStateLongPress) {
buttonLongPressMillis = currentMillis;
buttonStatePrevious = LOW;
Serial.println("Button pressed");
}
// Calculate how long the button has been pressed
buttonPressDuration = currentMillis - buttonLongPressMillis;
// If the button is pressed AND
// If there is no measurement running to determine how long the button is pressed AND
// If the time the button has been pressed is larger or equal to the time needed for a long press
if (buttonState == LOW && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
buttonStateLongPress = true;
Serial.println("Button long pressed");
}
// If the button is released AND
// If the button was pressed before
if (buttonState == HIGH && buttonStatePrevious == LOW) {
buttonStatePrevious = HIGH;
buttonStateLongPress = false;
Serial.println("Button released");
// If there is no measurement running to determine how long the button was pressed AND
// If the time the button has been pressed is smaller than the minimal time needed for a long press
if (buttonPressDuration < minButtonLongPressDuration) {
Serial.println("Button pressed shortly");
}
}
// store the current timestamp in previousButtonMillis
previousButtonMillis = currentMillis;
}
}
void loop()
{
if (btncount <1 && currentMillis <= 2867 )
{
Serial.print("push 2 statr; ");
Serial.println("off");
delay(1000);
Serial.println("rotate to set temp (0 to 34)");
delay(1000);
btncount = 0;
delay(1000);
Serial.println(count);
if (buttonPressDuration < minButtonLongPressDuration) {
Serial.println("Button pressed shortly");
}
}
if (digitalRead( btn ) == LOW) //check if button is pressed
{
++ btncount; // if pressed add one to variable btncount
Serial.println("on");
delay(1000);
