Devices & Components
Arduino Uno Rev3
5 Volt Red LED
Piezo sounder element (small)
Non mercury tilt switch (PCB mount)
5 Volt Yellow LED
Software & Tools
Arduino IDE
Project description
Code
Tilt alarm software
c_cpp
1 2// Tilt switch alarm using Arduino UNO and shield November 2021 3 4/* 5 * Connections: 6 * 7 * 5 Volt Red LED Anode to d8 Cathode to Ground 8 * 9 * 5 Volt Yellow LED Anode to d9 Cathode to Ground 10 * 11 * Tilt switch 1 - 2* contacts to d10 2* contacts to Ground 12 * 13 * Tilt switch 2 - 2* contacts to d10 2* contacts to Ground 14 * 15 * Piezo element 1* contact to d3 other contact to Ground 16 * 17 * Source for timer interrupt calculations: 18 * 19 * AVR Timer CTC Interrupts Calculator 20 * v. 8 21 * http://www.arduinoslovakia.eu/application/timer-calculator 22 * Microcontroller: ATmega328P 23 * Created: 2021-12-14T01:44:28.542Z 24 * 25*/ 26 27#define button_1 10 28#define button_2 11 29 30#define yellow_led 9 31#define red_led 8 32#define piezo_sounder 3 33 34#define my_test // test mode shortens piezo_sounder time 35 36const int number_of_buttons =2; 37 38//piezo_sounder time in milliseconds 39#ifdef my_test 40const unsigned long the_alarm_time=500; 41#else 42const unsigned long the_alarm_time=10000; 43#endif 44 45const int piezo_sounder_frequency =4000;//set at resonant frequency of piezo sounder for maximum volume 46 47//required to debounce switch 48#define MAX_CHECKS 10 49 50uint8_t State[MAX_CHECKS]={0}; 51uint8_t Index=0; 52 53volatile bool sensor_changed=false;//accessed by isr and main loop code 54 55 56// the setup function runs once when you press reset or power the board 57void setup() { 58 59//for switches 60 pinMode(button_1,INPUT_PULLUP); 61 pinMode(button_2,INPUT_PULLUP); 62 63//for leds 64 pinMode(yellow_led,OUTPUT); 65 pinMode(red_led,OUTPUT); 66 pinMode(piezo_sounder,OUTPUT); 67 68// initialize timer1 69 70 setupTimer1(); 71 72} 73 74// the loop function runs over and over again forever 75void loop() { 76 77int count=0; 78 79//alarm set delay 80//flash yellow led 4 times then red led once 81// 0 1 2 3 4 5 6 7 8 9 82//[y][][y][][y][][y][][r][] Exit loop 83 do 84{ 85 86 87 switch(count) 88 { 89 case 1:digitalWrite(yellow_led,HIGH);break; 90 case 2:digitalWrite(yellow_led,LOW);break; 91 case 3:digitalWrite(yellow_led,HIGH);break; 92 case 4:digitalWrite(yellow_led,LOW);break; 93 case 5:digitalWrite(yellow_led,HIGH);break; 94 case 6:digitalWrite(yellow_led,LOW);break; 95 case 7:digitalWrite(yellow_led,HIGH);break; 96 case 8:digitalWrite(yellow_led,LOW);break; 97 98 case 9:digitalWrite(red_led,1);break; 99 } 100 101 delay(1000); 102 103 count++; 104 105 if(sensor_changed){count=0;sensor_changed=false;digitalWrite(red_led,LOW);} 106 107} while (count<10); 108 109digitalWrite(red_led,LOW); 110 111 112//wait for sensor change trigger 113while(!sensor_changed){} 114sensor_changed=false; 115 116//countdown to allow alarm to be disabled 117count=0; 118 do 119{ 120 // statement block 121 digitalWrite(red_led, digitalRead(red_led) ^ 1); // toggle LED pin 122 delay(1000); 123 count++; 124} while (count<9); 125 digitalWrite(red_led, digitalRead(red_led) ^ 1); // toggle LED pin 126 127//sound alarm once for the_alarm_time 128 129bool alarm_done=false; 130 131do{ 132 if(!alarm_done){//sound alarm first time through 133 134 alarm_done=true; 135 tone(piezo_sounder, piezo_sounder_frequency ,the_alarm_time);// pin, frequency, duration 136 137 } 138 else {//subsequently light led to alert user that alarm has been tripped 139 140 digitalWrite(red_led,HIGH); // turn on red_led 141 delay(5000); 142 digitalWrite(red_led,LOW); // turn off red_led 143 144 } 145 146 147//wait for sensor change trigger 148sensor_changed=false; 149while(!sensor_changed){} 150sensor_changed=false; 151 152}while(1);//infinite loop 153 154 155} 156 157//My_functions 158 159void setupTimer1() { 160 noInterrupts(); 161 // Clear registers 162 TCCR1A = 0; 163 TCCR1B = 0; 164 TCNT1 = 0; 165 166 // 100 Hz (16000000/((624+1)*256)) 167 OCR1A = 624; 168 // CTC 169 TCCR1B |= (1 << WGM12); 170 // Prescaler 256 171 TCCR1B |= (1 << CS12); 172 // Output Compare Match A Interrupt Enable 173 TIMSK1 |= (1 << OCIE1A); 174 interrupts(); 175} 176 177ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 178{ 179 180//read buttons 181 uint8_t temp=0x00; 182 temp|=!digitalRead(button_1)<<0; 183 temp|=!digitalRead(button_2)<<1; 184 185//debounce 186static uint8_t Debounced_State=0; 187static uint8_t Previous_Debounced_State=0; 188Previous_Debounced_State=Debounced_State; 189 190 uint8_t i,j; 191 State[Index]= temp; 192 ++Index; 193 j=0xFF; 194 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 195 Debounced_State=j; 196 if(Index>=MAX_CHECKS){Index=0;} 197 198 if (Debounced_State!=Previous_Debounced_State){sensor_changed=true;} 199 200} 201//-----------end------------ 202
Downloadable files
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 photo

Tilt alarm board diagram
Tilt alarm board diagram

Tilt alarm circuit
Tilt alarm circuit

Tilt alarm circuit
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 photo

Tilt alarm board diagram
Tilt alarm board diagram

Comments
Only logged in users can leave comments