-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPerfect.ino
92 lines (67 loc) · 2.56 KB
/
Perfect.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//We need to add on requests inside a void loop (like an if statement)
//We also need to finish construction of our project!
#include <Servo.h>
#include <SharpIR.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
#define ir A0
#define model 20150
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// (working distance range according to the datasheets)
SharpIR sharp(ir, model);
int pos = 0; // variable to store the servo position
int rled = 13;
int yled = 12;
int gled = 11;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(rled, OUTPUT);
pinMode(yled, OUTPUT);
pinMode(gled, OUTPUT);
}
// put your setup code here, to run once:
Serial.begin(9600);
// the loop routine runs over and over again forever:
void loop()
for (pos = 0; pos <= 180; pos += 10) { // goes from 0 degrees to 180 degrees
// in steps of 10 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
unsigned long pepe1=millis(); // takes the time before the loop on the library begins
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
// if(dis>0<10)
// {//Code for Red Light HERE
// }
// if(dis>10)
{//Stay Green for Road Traffic
// }
{
digitalWrite(gled, HIGH); // turn the LED on (HIGH is the voltage level)
delay(4000);
// wait for a second
digitalWrite(gled, LOW); // turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
digitalWrite(yled, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(yled, LOW); // turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
digitalWrite(rled, HIGH); // turn the LED on (HIGH is the voltage level)
delay(4000);
// wait for a second
digitalWrite(rled, LOW); // turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
}