KNOCK SWITCH WITH SENSOR SW 420

sw420

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/SW420%20KNOCK%20SENSOR.zip

int led = 3;
// the setup routine runs once when you press reset:
void setup() {
pinMode(led, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
int on = 0;
unsigned long last = millis();
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1);
if (sensorValue >10) {
// If it’s been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() – last > 250) {
on = !on;
digitalWrite(led, on ? HIGH : LOW);
}
last = millis();
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s