1#include <pitches.h>
2
3#include <SR04.h>
4SR04 ultraSonic = SR04(11, 9 );
5
6long dist;
7
8int code[] = {1, 2, 3, 4};
9
10
11int entered[5];
12
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
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);
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++) {
51 Serial.println(code[i]);
52 Serial.println(entered[i]);
53
54 }
55}
56
57void loop() {
58
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) {
86 checkEntered1(1);
87
88 delay(timerdelay);
89
90
91 }
92 else if (digitalRead(button2) == HIGH) {
93 checkEntered1(2);
94
95 delay(timerdelay);
96
97 }
98 else if (digitalRead(button3) == HIGH) {
99 checkEntered1(3);
100
101 delay(timerdelay);
102
103 }
104 else if (digitalRead(button4) == HIGH) {
105 checkEntered1(4);
106
107 delay(timerdelay);
108
109 }
110}
111
112void checkEntered1(int button) {
113 if (entered[0] != 0) {
114 checkEntered2(button);
115 }
116
117 else if (entered[0] == 0) {
118 entered[0] = button;
119 Serial.print("1: "); Serial.println(entered[0]);
120 }
121
122}
123
124void checkEntered2(int button) {
125
126 if (entered[1] != 0) {
127 checkEntered3(button);
128 }
129
130 else if (entered[1] == 0) {
131 entered[1] = button;
132 Serial.print("2: "); Serial.println(entered[1]);
133 }
134
135}
136
137void checkEntered3(int button) {
138 if (entered[2] != 0) {
139 checkEntered4(button);
140 }
141
142 else if (entered[2] == 0) {
143 entered[2] = button;
144 Serial.print("3: "); Serial.println(entered[2]);
145 }
146
147}
148
149void checkEntered4(int button) {
150 if (entered[3] == 0) {
151 entered[3] = button;
152 Serial.print("4: "); Serial.println(entered[3]);
153 delay(100);
154 compareCode();
155 }
156}
157
158
159
160void compareCode() {
161 for (int i = 0; i < 4; i++) {
162 Serial.println(entered[i]);
163 }
164 if ((entered[0] == code[0]) && (entered[1] == code[1]) && (entered[2] == code[2]) && (entered[3] == code[3])) {
165 digitalWrite(redLED, LOW);
166 digitalWrite(greenLED, HIGH);
167 delay(1000);
168 digitalWrite(greenLED, LOW);
169 noTone(buzzerPin);
170
171
172
173 for (int i = 0; i < 5; i++) {
174 entered[i] = 0;
175
176 }
177
178
179
180 }
181 else {
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++) {
188 entered[i] = 0;
189
190 }
191
192 }
193
194}
195
Anonymous user
2 years ago
I keep getting an error message on line 1. How did you get the pitches.h library? I looked it up on the Library Manager and could not find it.