Alarm activated by a TILT sensor

Featured Video Play Icon

Sketch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int inPin = 2;   // the number of the input pin
int reading;     // the current reading from the input pin
int GreenLedPin = 13;  // the number of the Green LED output pin
int RedLedPin = 12;    // the number of the Red LED output pin
const int SpeakerPin = 11; // the number of the Speaker/Buzzer pin

void setup(){
  pinMode (inPin, INPUT);
  pinMode (GreenLedPin, OUTPUT);
  pinMode (RedLedPin, OUTPUT);
  Serial.begin(9600);
}

void loop () {
  reading = digitalRead(inPin);
  if (reading == 1) {
    digitalWrite(RedLedPin, HIGH);   // if tilted, turn the red LED ON
    digitalWrite(GreenLedPin, LOW);
    tone(SpeakerPin, 494, 500);      // if tilted, turn the Speaker ON
   delay(500);
   
  } else {
    digitalWrite(RedLedPin, LOW);
    digitalWrite(GreenLedPin, HIGH);    // if not tilted, turn the green LED ON
  }
  Serial.println(reading); // not really needed

  delay(200);   // pause 200 milliseconds between readings
}

Wire Diagram

Leave a Reply

Your email address will not be published. Required fields are marked *