-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiFeel_Arduino.ino
172 lines (131 loc) · 3.7 KB
/
iFeel_Arduino.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//a project for iFeel sensing
//Mainatance: [email protected]
//-----------------------------------
int S0 = 4;//pinB
int S1 = 5;//pinA
int S2 = 6;//pinE
int S3 = 7;//pinF
int taosOutPin = 8;//pinC
int LED = 13;//pinD
int grey=0;
void setup() {
TCS3200setup();
Serial.begin(115200);
Serial.print("\n\n\nready\n\n\n");
delay(100);
}
// primary loop takes color readings from all four channels and displays the raw values once per second. What you might wish to do with those values is up to you...
void loop() {
detectColor(taosOutPin);
Serial.print("\n\n\n");
delay(200);
vibr(grey);
}
int detectColor(int taosOutPin){
float white = colorRead(taosOutPin,0,1);
float red = colorRead(taosOutPin,1,1);
float blue = colorRead(taosOutPin,2,1);
float green = colorRead(taosOutPin,3,1);
Serial.print("white ");
Serial.println(white);
Serial.print("red ");
Serial.println(red);
Serial.print("blue ");
Serial.println(blue);
Serial.print("green ");
Serial.println(green);
//analogWrite(12, (white+red+blue+green)/4);
grey=0.2126*red+0.7152*green+0.0722*blue;
}
float colorRead(int taosOutPin, int color, boolean LEDstate){
taosMode(1);
//setting for a delay to let the sensor sit for a moment before taking a reading.
int sensorDelay = 100;
//set the S2 and S3 pins to select the color to be sensed
if(color == 0){//white
digitalWrite(S3, LOW); //S3
digitalWrite(S2, HIGH); //S2
// Serial.print(" w");
}
else if(color == 1){//red
digitalWrite(S3, LOW); //S3
digitalWrite(S2, LOW); //S2
// Serial.print(" r");
}
else if(color == 2){//blue
digitalWrite(S3, HIGH); //S3
digitalWrite(S2, LOW); //S2
// Serial.print(" b");
}
else if(color == 3){//green
digitalWrite(S3, HIGH); //S3
digitalWrite(S2, HIGH); //S2
// Serial.print(" g");
}
// create a var where the pulse reading from sensor will go
float readPulse;
// turn LEDs on or off, as directed by the LEDstate var
if(LEDstate == 0){
digitalWrite(LED, LOW);
}
if(LEDstate == 1){
digitalWrite(LED, HIGH);
}
// wait a bit for LEDs to actually turn on, as directed by sensorDelay var
delay(sensorDelay);
// now take a measurement from the sensor, timing a low pulse on the sensor's "out" pin
readPulse = pulseIn(taosOutPin, LOW, 80000);
//if the pulseIn times out, it returns 0 and that throws off numbers. just cap it at 80k if it happens
if(readPulse < .1){
readPulse = 80000;
}
//turn off color sensor and LEDs to save power
taosMode(0);
// return the pulse value back to whatever called for it...
return readPulse;
}
void vibr(int delay_time){
analogWrite(12,512);
delay(50);
analogWrite(12,0);
delay(delay_time/1000);
return;
}
void taosMode(int mode){
if(mode == 0){
//power OFF mode- LED off and both channels "low"
digitalWrite(LED, LOW);
digitalWrite(S0, LOW); //S0
digitalWrite(S1, LOW); //S1
// Serial.println("mOFFm");
}else if(mode == 1){
//this will put in 1:1, highest sensitivity
digitalWrite(S0, HIGH); //S0
digitalWrite(S1, HIGH); //S1
// Serial.println("m1:1m");
}else if(mode == 2){
//this will put in 1:5
digitalWrite(S0, HIGH); //S0
digitalWrite(S1, LOW); //S1
//Serial.println("m1:5m");
}else if(mode == 3){
//this will put in 1:50
digitalWrite(S0, LOW); //S0
digitalWrite(S1, HIGH); //S1
//Serial.println("m1:50m");
}
return;
}
void TCS3200setup(){
//initialize pins
pinMode(LED,OUTPUT); //LED pinD
//color mode selection
pinMode(S2,OUTPUT); //S2 pinE
pinMode(S3,OUTPUT); //s3 pinF
//color response pin (only actual input from taos)
pinMode(taosOutPin, INPUT); //taosOutPin pinC
//communication freq (sensitivity) selection
pinMode(S0,OUTPUT); //S0 pinB
pinMode(S1,OUTPUT); //S1 pinA
return;
}