/* Arduino Tutorial: How to use a magnetic contact switch Dev: Michalis Vasilakis // www.ardumotive.com // Date: 4/8/2016 */ const int buzzer = 3; const int sensor = 4; const int ledPin = 13; int state; // 0 close - 1 open wwitch void setup() { pinMode(sensor, INPUT_PULLUP); pinMode(buzzer, OUTPUT); pinMode(ledPin, OUTPUT); } void loop() { state = digitalRead(sensor); if (state == HIGH){ tone(buzzer, 555); digitalWrite(ledPin,HIGH ); } else{ noTone(buzzer); digitalWrite(ledPin,LOW ); } delay(200); }