Components and supplies
OR gate
Photo resistor
Pushbutton switch 12mm
LED (generic)
Project description
Code
Light Sensor Alarm
arduino
1const int buttonRed = 8; 2const int buttonBlue = 9; 3const int buttonWhite = 10; 4const int buttonGreen = 11; 5 6const int ledRed = 6; 7const int ledGreen = 7; 8 9const int buzzerpin = 13; 10const int LEDpin = 5; 11 12const int displayButton = 12; 13const int photoSensorState = 4; 14 15int password[4] = {1, 2, 3, 4}; 16volatile int entered[4] = {0, 0, 0, 0}; 17 18volatile int state = LOW; 19volatile int pushButtonCounter = 0; 20bool boxNeverOpen = 1; 21bool alarmDisabled = 0; 22unsigned long correctPasswordEntered = 0; 23 24unsigned int hack = 0; 25 26volatile int eventNumber = 0; 27 28struct timerInfo { 29 String action; 30 unsigned int timeOfEvent; 31}; 32 33timerInfo timerInfoArray[20]; 34 35unsigned int timer = 0; 36 37void setup() { 38 Serial.begin(9600); 39 pinMode (buttonWhite, INPUT); 40 pinMode (buttonGreen, INPUT); 41 pinMode (buttonRed, INPUT); 42 pinMode (buttonBlue, INPUT); 43 44 pinMode (ledRed, OUTPUT); 45 pinMode (ledGreen, OUTPUT); 46 47 pinMode (buzzerpin, OUTPUT); 48 pinMode (LEDpin, OUTPUT); 49 50 pinMode (displayButton, INPUT); 51 pinMode (photoSensorState, INPUT); 52 53 attachInterrupt (digitalPinToInterrupt(2), buttonPressed, RISING); 54 attachInterrupt (digitalPinToInterrupt(3), photoResistor, CHANGE); 55 56 cli(); 57 TCCR1A = 0; // clears register 58 TCCR1B = 0; // clears register 59 TIMSK1 |= (1 << TOIE1); //enables overflow interrupt 60 TCCR1B |= _BV(CS12); //prescales to 16MHz/256 = 62500 Hz. 61 sei(); 62 63} 64 65ISR(TIMER1_OVF_vect) // timer 1 can count to 65535 and the clock frequency is set to 62500hz or 0.000016 seconds per timer increment. 0.000016 * 65535 = 1.04856 seconds 66{ 67timer++; // timer increments every 1.04856 seconds 68} 69 70long timerr(){ 71 unsigned long timerValue = 0; 72 timerValue = ((timer * 65535) + TCNT1) / 62500; 73 return (timerValue); 74} 75 76void loop() { 77 if ((state == HIGH) && (((millis() - correctPasswordEntered) > 10000) || (correctPasswordEntered == 0))) { 78 detachInterrupt(digitalPinToInterrupt(2)); //so other lights and buttons cannot be pressed thus risking change of state 79 digitalWrite(LEDpin, state); 80 digitalWrite(buzzerpin, state); 81 82 tone(buzzerpin, 100); // Send 1KHz sound signal... 83 delay(1000); // ...for 1 sec 84 noTone(buzzerpin); // Stop sound... 85 delay(1000); // ...for 1sec 86 87 if ((digitalRead(displayButton)) == HIGH) { //hold down button due to delays 88 displayTimedEvents(); //polling used for display events because it should override anything and interrupts were detached 89 } 90 } 91 92 if (pushButtonCounter == 4) { 93 bool check = compareArrays(); 94 if (check == 1) { 95 correctPasswordEntered = timerr(); 96 timerInfoArray[eventNumber].action = "Correct Password Entered - Alarm Disabled"; 97 timerInfoArray[eventNumber].timeOfEvent = correctPasswordEntered; 98 eventNumber ++; 99 while ((timerr() - correctPasswordEntered) < 10) { 100 detachInterrupt(digitalPinToInterrupt(3)); 101 digitalWrite (ledGreen, HIGH); // turns green LED on for a 10 second time period after correct password is entered 102 } 103 digitalWrite (ledGreen, LOW); // green LED turns off after the 5 second safe zone 104 timerInfoArray[eventNumber].action = "Alarm Enabled"; 105 timerInfoArray[eventNumber].timeOfEvent = timerr(); 106 eventNumber ++; 107 hack = millis(); 108 attachInterrupt (digitalPinToInterrupt(3), photoResistor, CHANGE); // Serial.print("state after attach interrupt: "); 109 if (digitalRead(photoSensorState) == HIGH) { //if the box if left open after safe zone expires, alarm will sound 110 state = HIGH; 111 } 112 //timerInfoArray[eventNumber-1].action = "err"; 113 //state = LOW; // hack for button 114 // boxNeverOpen = 1; 115 for (int i = 0; i < 4; i ++) { 116 (entered[i] = 0); //resets password after correct entry 117 } 118 } 119 else if (check == 0) { 120 timerInfoArray[eventNumber].action = "Wrong Password Entered"; 121 timerInfoArray[eventNumber].timeOfEvent = timerr(); 122 eventNumber ++; 123 wrongPassword(); 124 } 125 pushButtonCounter = 0; 126 } 127} 128 129void buttonPressed () { 130 static unsigned long last_interrupt_time = 0; // changes to interrupt time at end of ISR. Thus if it glitches and triggers multiple times successively, it won't go into following loop because that glitch happens in less than 200ms 131 unsigned long interrupt_time = millis(); 132 if (interrupt_time - last_interrupt_time > 200) { 133 if ((digitalRead(buttonRed)) == HIGH) { 134 entered[pushButtonCounter] = 1; 135 pushButtonCounter ++; 136 137 timerInfoArray[eventNumber].action = "Red Button Pressed"; 138 timerInfoArray[eventNumber].timeOfEvent = timerr(); 139 eventNumber ++; 140 Serial.println("red"); 141 } 142 143 if ((digitalRead(buttonBlue)) == HIGH) { 144 entered[pushButtonCounter] = 2; 145 pushButtonCounter ++; 146 147 timerInfoArray[eventNumber].action = "Blue Button Pressed"; 148 timerInfoArray[eventNumber].timeOfEvent = timerr(); 149 eventNumber ++; 150 //currentState(); 151 Serial.println("blue"); 152 } 153 154 if ((digitalRead(buttonWhite)) == HIGH) { 155 entered[pushButtonCounter] = 3; 156 pushButtonCounter ++; 157 158 timerInfoArray[eventNumber].action = "White Button Pressed"; 159 timerInfoArray[eventNumber].timeOfEvent = timerr(); 160 eventNumber ++; 161 //currentState(); 162 Serial.println("white"); 163 } 164 165 if ((digitalRead(buttonGreen)) == HIGH) { 166 entered[pushButtonCounter] = 4; 167 pushButtonCounter ++; 168 169 timerInfoArray[eventNumber].action = "Green Button Pressed"; 170 timerInfoArray[eventNumber].timeOfEvent = timerr(); 171 eventNumber ++; 172 //currentState(); 173 Serial.println("green"); 174 } 175 176 if ((digitalRead(displayButton)) == HIGH) { 177 displayTimedEvents(); 178 while (1 == 1) {}; 179 } 180 last_interrupt_time = interrupt_time; 181 } 182} 183 184void photoResistor() { 185 static unsigned long last_interrupt_time = 0; 186 unsigned long interrupt_time = millis(); 187 if ((interrupt_time - last_interrupt_time > 200) && ((millis() - hack) > 100)) { // && ((millis() - hack) > 100)) 188 if (boxNeverOpen == 1) { // causes the alarm to never stop buzzing if you open it WITHOUT entering the correct password 189 state = HIGH; 190 boxNeverOpen = 0; 191 192 timerInfoArray[eventNumber].action = "Alarm triggered"; 193 timerInfoArray[eventNumber].timeOfEvent = timerr(); 194 eventNumber ++; 195 } 196 } 197 last_interrupt_time = interrupt_time; 198} 199 200bool compareArrays () { 201 for (int i = 0; i < 4; i ++) { 202 if (password[i] != entered[i]) { 203 return false; 204 } 205 } 206 return true; 207} 208 209void currentState() { 210 Serial.print("the button has been pushed this many times: " ); 211 Serial.println(pushButtonCounter); 212 213 Serial.print("the password so far is: " ); 214 for (int i = 0; i < 4; i ++) { 215 Serial.print(entered[i]); 216 } 217 Serial.println(); 218} 219 220void wrongPassword() { 221 digitalWrite (ledRed, HIGH); 222 delay (500); 223 digitalWrite (ledRed, LOW); 224 delay (500); 225 digitalWrite (ledRed, HIGH); 226 delay (500); 227 digitalWrite (ledRed, LOW); 228 for (int i = 0; i < 4; i ++) { 229 (entered[i] = 0); 230 } 231} 232 233void displayTimedEvents() { 234 for (int k = 0; (k < 20); k++) { 235 if (timerInfoArray[k].action == "err") { 236 Serial.print(k + 1); 237 Serial.print(" "); 238 Serial.println("0"); 239 } 240 else { 241 Serial.print(k + 1); 242 Serial.print(" "); 243 Serial.print(timerInfoArray[k].action); 244 Serial.print(" "); 245 Serial.println(timerInfoArray[k].timeOfEvent); 246 } 247 } 248} 249 250 251
Comments
Only logged in users can leave comments
shfernan
0 Followers
•0 Projects
0