Skip to content

Instantly share code, notes, and snippets.

@arduinothai
Last active July 17, 2023 07:03
Show Gist options
  • Save arduinothai/aadf74097251d8dc21972fd6e5c93023 to your computer and use it in GitHub Desktop.
Save arduinothai/aadf74097251d8dc21972fd6e5c93023 to your computer and use it in GitHub Desktop.
#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;
}
@NewNattakan
Copy link

ตัว code อันนี้พอมีอธิบายที่มาคร่าวๆหน่อยมั้ยคะ ว่ามันมาจากไหนบ้างเหมือนอธิบาย code ว่าทำไมเราถึงเขียนแล้วมันออกมาแบบนี้อ่ะค่ะ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment