Last active
July 17, 2023 07:03
-
-
Save arduinothai/aadf74097251d8dc21972fd6e5c93023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define signalToRelayPin 8 | |
#define sensorPin 7 | |
int lastSoundValue; | |
int soundValue; | |
long lastNoiseTime = 0; | |
long currentNoiseTime = 0; | |
long lastLightChange = 0; | |
int relayStatus = HIGH; | |
void setup() { | |
pinMode(sensorPin, INPUT); | |
pinMode(signalToRelayPin, OUTPUT); | |
digitalWrite(signalToRelayPin, HIGH); | |
} | |
void loop() { | |
soundValue = digitalRead(sensorPin); | |
currentNoiseTime = millis(); | |
if (soundValue == 1) { | |
if ( | |
(currentNoiseTime > lastNoiseTime + 200)&& | |
(lastSoundValue == 0)&& | |
(currentNoiseTime < lastNoiseTime + 800)&& | |
(currentNoiseTime > lastLightChange + 1000) | |
) { | |
relayStatus = !relayStatus; | |
digitalWrite(signalToRelayPin, relayStatus); | |
lastLightChange = currentNoiseTime; | |
} | |
lastNoiseTime = currentNoiseTime; | |
} | |
lastSoundValue = soundValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ตัว code อันนี้พอมีอธิบายที่มาคร่าวๆหน่อยมั้ยคะ ว่ามันมาจากไหนบ้างเหมือนอธิบาย code ว่าทำไมเราถึงเขียนแล้วมันออกมาแบบนี้อ่ะค่ะ