File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ // This uses a RGB Common Cathode LED, and an HC_SR04 ultrasonic sensor
2
+ // The LED lights up in different colours, according to the distance of an object from the sensor
3
+
4
+ #include < NewPing.h>
5
+ NewPing sonar (12 , 13 , 200 );
6
+
7
+ void setup (){
8
+ Serial.begin (9600 );
9
+ pinMode (11 , OUTPUT); // red
10
+ pinMode (10 , OUTPUT); // green
11
+ pinMode (9 , OUTPUT); // blue
12
+ }
13
+
14
+ void loop (){
15
+ int distance = sonar.ping_cm ();
16
+ Serial.println (distance);
17
+
18
+ // if distance is up to 5 cm, the LED becomes red
19
+ if (distance <=5 && distance >0 ){
20
+ digitalWrite (11 , HIGH);
21
+ digitalWrite (10 , LOW);
22
+ digitalWrite (9 , LOW);
23
+ }
24
+
25
+ // if distance is between 5 and 10 cm, the LED becomes blue
26
+ else if (distance > 5 && distance <= 10 ){
27
+ digitalWrite (11 , LOW);
28
+ digitalWrite (10 , LOW);
29
+ digitalWrite (9 , HIGH);
30
+ }
31
+
32
+ // if distance if bigger than 10 cm, the LED becomes green
33
+ else if (distance >10 ){
34
+ digitalWrite (11 , LOW);
35
+ digitalWrite (10 , HIGH);
36
+ digitalWrite (9 , LOW);
37
+ }
38
+ else {
39
+ digitalWrite (11 , LOW);
40
+ digitalWrite (10 , LOW);
41
+ digitalWrite (9 , LOW);
42
+ }
43
+ delay (1000 );
44
+ }
You can’t perform that action at this time.
0 commit comments