-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_sep03a.ino
158 lines (133 loc) · 3.47 KB
/
sketch_sep03a.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <Servo.h>
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
Servo topServo;
Servo bottomServo;
int frequency = 0;
int color=0;
int top_servo_start = 90;
int top_servo_sensor = 26;
int top_servo_end = 0 ;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
topServo.attach(7);
bottomServo.attach(8);
Serial.begin(9600);
}
void loop() {
topServo.write(top_servo_start);
delay(500);
for(int i = top_servo_start; i > top_servo_sensor; i--) {
topServo.write(i);
delay(2);
}
delay(500);
color = readColor();
Serial.print("color");
Serial.println(color);
delay(10);
switch (color) {
case 1:
bottomServo.write(10);
break;
case 2:
bottomServo.write(35);
break;
case 3:
bottomServo.write(65);
break;
case 4:
bottomServo.write(95);
break;
case 0:
bottomServo.write(125);
break;
}
delay(300);
for(int i = top_servo_sensor; i >top_servo_end ; i--) {
topServo.write(i);
delay(2);
}
delay(200);
for(int i = top_servo_end; i < top_servo_start; i++) {
topServo.write(i);
delay(2);
}
color=0;
}
// Custom Function - readColor()
int readColor() {
// Setting red filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int R = frequency;
// Printing the value on the serial monitor
// Serial.print("R= ");//printing name
// Serial.print(frequency);//printing RED color frequency
// Serial.print(" ");
R = map(frequency,0, 255,0,100000);
Serial.print("R_remapped= ");//printing name
Serial.print(R);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Green filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int G = frequency;
// Printing the value on the serial monitor
// Serial.print("G= ");//printing name
// Serial.print(frequency);//printing RED color frequency
// Serial.print(" ");
G = map(frequency,0, 255,0,100000);
Serial.print("G_remapped= ");//printing name
Serial.print(G);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int B = frequency;
// Printing the value on the serial monitor
// Serial.print("B= ");//printing name
// Serial.print(frequency);//printing RED color frequency
// Serial.println(" ");
B = map(frequency,0, 255,0,100000);
Serial.print("B_remapped= ");//printing name
Serial.print(B);//printing RED color frequency
Serial.println(" ");
delay(50);
int color = 0;
if(R<11800 & R>11300 & G<17700 & G>16800){
color = 1; // Red
return color;
}
if(R<10000 & R>8000 & G<11800 & G>11300){
color = 2; // yellow
return color;
}
if(R<15000 & R>13320 & G<1670 & G>1445){
color = 3; // green
return color;
}
if(R<12600 & R>11850 & G<1750 & G>1450){
color = 4; // pink
return color;
}
return color;
}