glennedi
Published © GPL3+

Tilt switch alarm

Keep people off your stuff!

BeginnerFull instructions provided656
Tilt switch alarm

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Arduino UNO development shield
×1
5 Volt Red LED
The kind with the built in resistor
×1
5 Volt Yellow LED
The kind with the built in resistor
×1
Piezo sounder element (small)
×1
Non mercury tilt switch (PCB mount)
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Tilt alarm circuit

Tilt alarm board photo

Note that the wires for the piezo sounder are fed from the top of the board to the underneath where they are soldered to the sounders pins

Tilt alarm board diagram

Code

Tilt alarm software

C/C++
// Tilt switch alarm using Arduino UNO and shield November 2021

/*
 * Connections: 
 * 
 * 5 Volt Red LED Anode to d8 Cathode to Ground
 * 
 * 5 Volt Yellow LED Anode to d9 Cathode to Ground
 * 
 * Tilt switch 1 - 2* contacts to d10 2* contacts to Ground
 * 
 * Tilt switch 2 - 2* contacts to d10 2* contacts to Ground
 * 
 * Piezo element 1* contact to d3 other contact to Ground
 * 
 * Source for timer interrupt calculations:
 * 
 * AVR Timer CTC Interrupts Calculator
 * v. 8
 * http://www.arduinoslovakia.eu/application/timer-calculator
 * Microcontroller: ATmega328P
 * Created: 2021-12-14T01:44:28.542Z
 *
*/

#define button_1 10
#define button_2 11

#define yellow_led 9
#define red_led 8
#define piezo_sounder 3 

#define my_test // test mode shortens piezo_sounder time
             
const int number_of_buttons =2;

//piezo_sounder time in milliseconds
#ifdef my_test
const unsigned long the_alarm_time=500;
#else
const unsigned long the_alarm_time=10000;
#endif

const int piezo_sounder_frequency =4000;//set at resonant frequency of piezo sounder for maximum volume

//required to debounce switch
#define MAX_CHECKS 10

uint8_t State[MAX_CHECKS]={0};
uint8_t Index=0;

volatile bool sensor_changed=false;//accessed by isr and main loop code


// the setup function runs once when you press reset or power the board
void setup() {

//for switches
  pinMode(button_1,INPUT_PULLUP);
  pinMode(button_2,INPUT_PULLUP);

//for leds
  pinMode(yellow_led,OUTPUT);
  pinMode(red_led,OUTPUT);
  pinMode(piezo_sounder,OUTPUT);

// initialize timer1

  setupTimer1();
  
}

// the loop function runs over and over again forever
void loop() {

int count=0;

//alarm set delay
//flash yellow led 4 times then red led once
// 0 1  2 3  4  5 6  7 8 9
//[y][][y][][y][][y][][r][] Exit loop
  do
{


    switch(count)
    {
      case 1:digitalWrite(yellow_led,HIGH);break;
      case 2:digitalWrite(yellow_led,LOW);break;
      case 3:digitalWrite(yellow_led,HIGH);break;
      case 4:digitalWrite(yellow_led,LOW);break;
      case 5:digitalWrite(yellow_led,HIGH);break;
      case 6:digitalWrite(yellow_led,LOW);break;
      case 7:digitalWrite(yellow_led,HIGH);break;
      case 8:digitalWrite(yellow_led,LOW);break;
      
      case 9:digitalWrite(red_led,1);break;
      }

      delay(1000);
    
      count++;
    
      if(sensor_changed){count=0;sensor_changed=false;digitalWrite(red_led,LOW);}
    
} while (count<10);

digitalWrite(red_led,LOW);


//wait for sensor change trigger
while(!sensor_changed){}
sensor_changed=false;

//countdown to allow alarm to be disabled
count=0;
  do
{
    // statement block
    digitalWrite(red_led, digitalRead(red_led) ^ 1);   // toggle LED pin
    delay(1000);
    count++;
} while (count<9);
    digitalWrite(red_led, digitalRead(red_led) ^ 1);   // toggle LED pin

//sound alarm once for the_alarm_time

bool alarm_done=false;

do{
  if(!alarm_done){//sound alarm first time through

    alarm_done=true;
    tone(piezo_sounder, piezo_sounder_frequency ,the_alarm_time);// pin, frequency, duration
    
  }
  else {//subsequently light led to alert user that alarm has been tripped

    digitalWrite(red_led,HIGH);   // turn on red_led
    delay(5000);
    digitalWrite(red_led,LOW);   // turn off red_led

  }
  

//wait for sensor change trigger
sensor_changed=false; 
while(!sensor_changed){}
sensor_changed=false;

}while(1);//infinite loop


}

//My_functions

void setupTimer1() {
  noInterrupts();
  // Clear registers
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;

  // 100 Hz (16000000/((624+1)*256))
  OCR1A = 624;
  // CTC
  TCCR1B |= (1 << WGM12);
  // Prescaler 256
  TCCR1B |= (1 << CS12);
  // Output Compare Match A Interrupt Enable
  TIMSK1 |= (1 << OCIE1A);
  interrupts();
}

ISR(TIMER1_COMPA_vect)  // timer compare interrupt service routine
{

//read buttons
  uint8_t temp=0x00;
  temp|=!digitalRead(button_1)<<0;
  temp|=!digitalRead(button_2)<<1;
          
//debounce
static uint8_t Debounced_State=0;
static uint8_t Previous_Debounced_State=0;
Previous_Debounced_State=Debounced_State;
   
  uint8_t i,j;
  State[Index]= temp;
  ++Index;
  j=0xFF;
  for (i=0;i<MAX_CHECKS;i++){j=j&State[i];}
  Debounced_State=j;
  if(Index>=MAX_CHECKS){Index=0;} 

  if (Debounced_State!=Previous_Debounced_State){sensor_changed=true;}
    
}
//-----------end------------  

Credits

glennedi
5 projects • 23 followers
Contact

Comments

Please log in or sign up to comment.