-
Notifications
You must be signed in to change notification settings - Fork 0
/
trafficlight.ino
70 lines (65 loc) · 1.22 KB
/
trafficlight.ino
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "SR04.h"
#include <Servo.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
#define BLUE 3
#define GREEN 5
#define RED 6
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
Servo myservo;
long a;
int redValue;
int greenValue;
int blueValue;
int pos = 0;
int red = 1;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
myservo.attach(9);
}
void loop() {
a = sr04.Distance();
if (a < 10)
{
redValue = 0;
greenValue = 255;
blueValue = 0;
pos = 65;
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
myservo.write(pos);
delay(5000);
red = 1;
}
else
{
if (red == 1)
{
redValue = 255;
greenValue = 140;
blueValue = 0;
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(2000);
}
redValue = 255;
greenValue = 0;
blueValue = 0;
pos = 10;
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
myservo.write(pos);
delay(3000);
red = 0;
}
delay(100);
}