-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLed_distanta.ino
More file actions
53 lines (45 loc) · 1.06 KB
/
Copy pathLed_distanta.ino
File metadata and controls
53 lines (45 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
int Redpin = 6;
int Greenpin = 10;
int Bluepin = 11;
const int trigPin = 9;
const int echoPin = 8;
long duration;
int distance;
void setup() {
Serial.begin(9600);
pinMode(Redpin, OUTPUT);
pinMode(Greenpin, OUTPUT);
pinMode(Bluepin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(4);
duration = pulseIn(echoPin, HIGH);
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW);
distance = duration * 0.034 / 2;
if (distance >= 20) {
SetColor(100, 190, 255);
}
if (distance >= 63) {
SetColor(180, 255, 180);
} Serial.print("Distance: ");
Serial.println(distance);
if ( distance == 0) {
delayMicroseconds(2);
}
if(distance>=35&&distance<63&&distance>20){
SetColor(255,100,180);
}
if(distance<=10&&distance>0){
SetColor(0,0,0);
}
}
void SetColor (int red, int green, int blue) // the function which sets the color of the RGB LED
{ analogWrite (Redpin, red);
analogWrite (Greenpin, green);
analogWrite (Bluepin, blue);
}