Devices & Components
Arduino Uno Rev3
Breadboard (generic)
Resistor 10k ohm
5 mm LED: Red
Buzzer
Ultrasonic Sensor - HC-SR04 (Generic)
Resistor 220 ohm
5 mm LED: Yellow
Tactile Switch, Top Actuated
USB-A to B Cable
Jumper wires (generic)
5 mm LED: Green
Software & Tools
Arduino IDE
Project description
Code
Cameran
arduino
1#include <pitches.h> 2 3#include <SR04.h> 4SR04 ultraSonic = SR04(11, 9 ); 5 6long dist; 7 8int code[] = {1, 2, 3, 4}; //the desired code is entered in this array, 9//separated by commas 10 11int entered[5]; //create a new empty array for the code entered by 12//the user (has 4 elements) 13 14const int LED1 = 13; 15const int LED2 = 3; 16const int LED3 = 2; 17const int buzzerPin = 8; 18const int button1 = 4; 19const int button2 = 5; 20const int button3 = 6; 21const int button4 = 7; 22const int timerdelay = 250; 23 24const int redLED = 12; 25const int greenLED = 10; 26void checkEntered1(int button); 27 28void setup() { 29 // put your setup code here, to run once: 30 pinMode(LED1, OUTPUT); 31 pinMode(LED2, OUTPUT); 32 pinMode(LED3, OUTPUT); 33 pinMode(buzzerPin, OUTPUT); 34 Serial.begin(9600); 35 digitalWrite(LED1, LOW); 36 digitalWrite(LED2, LOW); 37 digitalWrite(LED3, LOW); 38 delay(1000); 39 Serial.begin(9600); //begin Serial 40 41 pinMode (button1, INPUT); 42 pinMode (button2, INPUT); 43 pinMode (button3, INPUT); 44 pinMode (button4, INPUT); 45 pinMode (greenLED, OUTPUT); 46 pinMode (redLED, OUTPUT); 47 48 digitalWrite(redLED, LOW); 49 digitalWrite(greenLED, LOW); 50 for (int i = 0; i < 4; i++) { //work through numbers 0-3 51 Serial.println(code[i]); //print each digit of the code 52 Serial.println(entered[i]); //print each element of the entered[] 53 54 } 55} 56 57void loop() { 58 // put your main code here, to run repeatedly: 59 dist = ultraSonic.Distance(); 60 Serial.print(dist); 61 Serial.println("cm"); 62 delay(100); 63 if (dist > 100) { 64 digitalWrite(LED1, LOW); 65 digitalWrite(LED2, LOW); 66 digitalWrite(LED3, LOW); 67 } 68 if (dist > 2 && dist <= 25) { 69 digitalWrite(LED1, HIGH); 70 digitalWrite(LED2, LOW); 71 digitalWrite(LED3, LOW); 72 tone(buzzerPin, NOTE_G4, 15000); 73 } 74 else if (dist > 25 && dist <= 75) { 75 digitalWrite(LED1, LOW); 76 digitalWrite(LED2, HIGH); 77 digitalWrite(LED3, LOW); 78 } 79 else if (dist > 75 && dist <= 100) { 80 digitalWrite(LED1, LOW); 81 digitalWrite(LED2, LOW); 82 digitalWrite(LED3, HIGH); 83 } 84 85 if (digitalRead(button1) == HIGH) { //if button1 is pressed 86 checkEntered1(1); //call checkEntered and pass it a 1 87 88 delay(timerdelay);//wait, needed for correct functioning, otherwise 89 //buttons are deemed to be pressed more than once 90 91 } 92 else if (digitalRead(button2) == HIGH) { //if button2 is pressed 93 checkEntered1(2); //call checkEntered1 and pass it a 2 94 95 delay(timerdelay); //wait 96 97 } 98 else if (digitalRead(button3) == HIGH) { //if button3 is pressed 99 checkEntered1(3); //call checkEntered1 and pass it a 3 100 101 delay(timerdelay); //wait 102 103 } 104 else if (digitalRead(button4) == HIGH) { //if button4 is pressed 105 checkEntered1(4); //call checkEntered1 and pass it a 4 106 107 delay(timerdelay); //wait 108 109 } 110} 111 112void checkEntered1(int button) { //check the first element of the entered[] array 113 if (entered[0] != 0) { //if it is not a zero, i.e. it has already been inputted 114 checkEntered2(button); //move on to checkEntered2, passing it "button" 115 } 116 117 else if (entered[0] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet 118 entered[0] = button; //set the first element as the button that has been pressed 119 Serial.print("1: "); Serial.println(entered[0]); //for debugging 120 } 121 122} 123 124void checkEntered2(int button) { //check the second element of the entered[] array 125 126 if (entered[1] != 0) { //if it is not a zero, i.e. it has already been inputted 127 checkEntered3(button); //move on to checkEntered3, passing it "button" 128 } 129 130 else if (entered[1] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet 131 entered[1] = button; //set the second element as the button that has been pressed 132 Serial.print("2: "); Serial.println(entered[1]); //for debugging 133 } 134 135} 136 137void checkEntered3(int button) { //check the third element of the entered[] array 138 if (entered[2] != 0) { //if it is not a zero, i.e. it has already been inputted 139 checkEntered4(button); //move on to checkEntered4, passing it "button" 140 } 141 142 else if (entered[2] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet 143 entered[2] = button; //set the third element as the button that has been pressed 144 Serial.print("3: "); Serial.println(entered[2]); //for debugging 145 } 146 147} 148 149void checkEntered4(int button) { //check the third element of the entered[] array 150 if (entered[3] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet 151 entered[3] = button; //set the final element as the button that has been pressed 152 Serial.print("4: "); Serial.println(entered[3]); //for debugging 153 delay(100); //allow time for processing 154 compareCode(); //call the compareCode function 155 } 156} 157 158 159 160void compareCode() { //checks if the code entered is correct by comparing the code[] array with the entered[] array 161 for (int i = 0; i < 4; i++) { //these three lines are for debugging 162 Serial.println(entered[i]); 163 } 164 if ((entered[0] == code[0]) && (entered[1] == code[1]) && (entered[2] == code[2]) && (entered[3] == code[3])) { //if all the elements of each array are equal 165 digitalWrite(redLED, LOW); // turn the red LED off 166 digitalWrite(greenLED, HIGH); //turn the green LED on 167 delay(1000); //wait for a bit 168 digitalWrite(greenLED, LOW); //turn the green LED off 169 noTone(buzzerPin); 170 171 172 173 for (int i = 0; i < 5; i++) { //this next loop is for debugging 174 entered[i] = 0; 175 176 } 177 178 //loop(); //return to loop() (not really necessary) 179 180 } 181 else { //if you (or the intruder) get the code wrong 182 183 digitalWrite(redLED, HIGH); 184 delay(1000); 185 digitalWrite(redLED, LOW); 186 Serial.println("Red on"); 187 for (int i = 0; i < 5; i++) { //this next loop is for debugging 188 entered[i] = 0; 189 190 } 191 192 } 193 194} 195
Downloadable files
cameran_schematic_CLZwLi4MQr.jpeg
This is the board setup. Keep in mind that the 10k resistors are connected to the buttons, while the 220 resistors are connected to the LEDs and to the GND wire of the Ultrasonic sensor.
cameran_schematic_CLZwLi4MQr.jpeg

cameran_schematic_CLZwLi4MQr.jpeg
This is the board setup. Keep in mind that the 10k resistors are connected to the buttons, while the 220 resistors are connected to the LEDs and to the GND wire of the Ultrasonic sensor.
cameran_schematic_CLZwLi4MQr.jpeg

Comments
Only logged in users can leave comments