1
1
/*
2
- Vernier3DA (v 2014.12 )
2
+ Vernier3DA (v 2017.08 )
3
3
This project assumes that you have a Vernier 3D Accelerometer connected to
4
4
three BTA connectors. It also assumes that you have an RGB LED connected to
5
5
PWM outputs 3, 5, and 6.
@@ -12,17 +12,15 @@ The brightness of the blue LED is controlled by the z axis acceleration.
12
12
13
13
During the first 15 seconds of this program you should rotate the
14
14
accelerometer through all 3 axes. This will calibrate the signal for
15
- you specific accelerometer. All LEDs will flash for 1 second at the beginning
15
+ you specific accelerometer. If you do not rotate the sensor it will
16
+ default to set values. All LEDs will flash for 1 second at the beginning
16
17
and end of this "calibration".
17
18
After calibration, each color should progress through a range of "off" to
18
19
full power based on the orientation of the accelerometer. Sitting flat on a
19
20
surface (z-axis up) is blue, flat on its side (y-axis up) is green, and cord
20
21
facing down is red.
21
22
Note that either individual LEDs or a RGB LED may be used.
22
23
23
- In order to experiment with 2 colors one of the LEDs must be disconnected from
24
- the digital output (Redpin, Greenpin, or Bluepin).
25
-
26
24
See www.vernier.com/arduino for more information.
27
25
*/
28
26
const int RedPin = 3 ;
@@ -34,30 +32,28 @@ const int sensorPinGreen = A2;
34
32
const int sensorPinBlue = A4;
35
33
36
34
int var = 1 ;
37
- int minReadingRed = 750 ; // High value to force the minimum
38
- int maxReadingRed = 0 ; // Low value to force the maximum
39
-
40
- int minReadingGreen = 750 ;
41
- int maxReadingGreen = 0 ;
42
-
43
- int minReadingBlue = 750 ;
44
- int maxReadingBlue = 0 ;
35
+ int minReadingRed = 450 ; // Default value if no calibration
36
+ int maxReadingRed = 600 ; // Default value if no calibration
37
+ int minReadingGreen = 450 ;
38
+ int maxReadingGreen = 600 ;
39
+ int minReadingBlue = 500 ;
40
+ int maxReadingBlue = 830 ;
45
41
46
- int analogValue = 0 ;
42
+ int analogValueR = 0 ;
43
+ int analogValueG = 0 ;
44
+ int analogValueB = 0 ;
47
45
int time;
48
46
int analogOut;
49
- int flash;
50
47
51
48
void setup ()
52
49
{
53
50
Serial.begin (9600 );
54
- Serial. println ();
51
+
55
52
}
56
53
void loop ()
57
54
{
58
-
59
55
while (time < 1000 ){ // LEDs to flash at start of calibration
60
- time = millis ();
56
+ time = millis ();
61
57
analogWrite (RedPin, 255 );
62
58
analogWrite (GreenPin, 255 );
63
59
analogWrite (BluePin, 255 );
@@ -66,33 +62,27 @@ void loop()
66
62
analogWrite (GreenPin, 0 );
67
63
analogWrite (BluePin, 0 );
68
64
delay (10 );}
69
-
70
- while (time >1000 && time < 16000 ){ // Time to calibrate each sensor to it's particular range of values.
71
- time = millis ();
72
- // Red
73
- analogValue = analogRead (sensorPinRed);
74
- maxReadingRed = max (analogValue, maxReadingRed);
75
- minReadingRed = min (analogValue, minReadingRed);// This is the absolute min. We'll want midpoint
76
- Serial.print (" Analog Red " );
77
- Serial.print (analogValue, DEC);
78
- delay (20 );
65
+ while (time >1000 && time < 16000 ){
66
+ time = millis ();
67
+ // Time to calibrate each sensor to it's particular range of values.
68
+ // Red
69
+ analogValueR = analogRead (sensorPinRed);
70
+ maxReadingRed = max (analogValueR, maxReadingRed);
71
+ minReadingRed = min (analogValueR, minReadingRed);// This is the absolute min. We'll adjust this up
79
72
// Green
80
- analogValue = analogRead (sensorPinGreen);
81
- maxReadingGreen = max (analogValue, maxReadingGreen);
82
- minReadingGreen = min (analogValue, minReadingGreen);// This is the absolute min. We'll want midpoint
83
- Serial.print (" Analog Green " );
84
- Serial.print (analogValue, DEC);
85
- delay (20 );
73
+ analogValueG = analogRead (sensorPinGreen);
74
+ maxReadingGreen = max (analogValueG, maxReadingGreen);
75
+ minReadingGreen = min (analogValueG, minReadingGreen);// This is the absolute min. We'll adjust this up
86
76
// Blue
87
- analogValue = analogRead (sensorPinBlue);
88
- maxReadingBlue = max (analogValue , maxReadingBlue);
89
- minReadingBlue = min (analogValue , minReadingBlue);// This is the absolute min. We'll want midpoint
90
- Serial. print ( " Analog Blue " );
91
- Serial. println (analogValue, DEC);
92
- delay ( 20 );
93
- }
94
- while ( time > 16000 && time < 17000 ){ // LEDs to flash at finish of calibration
95
- time = millis ( );
77
+ analogValueB = analogRead (sensorPinBlue);
78
+ maxReadingBlue = max (analogValueB , maxReadingBlue);
79
+ minReadingBlue = min (analogValueB , minReadingBlue);// This is the absolute min. We'll adjust this up
80
+ }
81
+ while ( time > 16000 && time < 17000 ){ // LEDs to flash at finish of calibration
82
+ time = millis ( );
83
+ Serial. print (minReadingRed);
84
+ Serial. print ( " /t " );
85
+ Serial. println (maxReadingRed );
96
86
analogWrite (RedPin, 255 );
97
87
analogWrite (GreenPin, 255 );
98
88
analogWrite (BluePin, 255 );
@@ -102,12 +92,11 @@ void loop()
102
92
analogWrite (BluePin, 0 );}
103
93
104
94
while (var == 1 ){
105
-
106
- minReadingRed = minReadingRed+0.5 *(maxReadingRed - minReadingRed); // Sets midpoint as minimum
107
- minReadingGreen = minReadingGreen+0.5 *(maxReadingGreen - minReadingGreen);
108
- minReadingBlue = minReadingBlue+0.5 *(maxReadingBlue - minReadingBlue);
109
-
110
- Serial.println ();
95
+ minReadingRed = minReadingRed+0.25 *(maxReadingRed - minReadingRed); // Adjusts minimum value (experiment with factor)
96
+ minReadingGreen = minReadingGreen+0.25 *(maxReadingGreen - minReadingGreen);
97
+ minReadingBlue = minReadingBlue+0.25 *(maxReadingBlue - minReadingBlue);
98
+
99
+ Serial.println ();// prints range of each axis
111
100
Serial.print (" minReadingRed = " );
112
101
Serial.print (minReadingRed, DEC);
113
102
Serial.print (" maxReadingRed = " );
@@ -125,67 +114,37 @@ void loop()
125
114
// Below is scrolling printout
126
115
127
116
// Red
128
-
129
- analogValue = analogRead (sensorPinRed);
130
- analogValue = constrain (analogValue, minReadingRed, maxReadingRed);
117
+ analogValueR = analogRead (sensorPinRed);
118
+ analogValueR = constrain (analogValueR, minReadingRed, maxReadingRed);
131
119
Serial.print (" analogValue Red = " );
132
- Serial.print (analogValue);
133
-
134
- analogOut = map (analogValue, minReadingRed, maxReadingRed, 0 , 255 );
120
+ Serial.println (analogValueR);
121
+ analogOut = map (analogValueR, minReadingRed, maxReadingRed, 0 , 255 );
135
122
analogOut = constrain (analogOut, 0 , 255 );
136
123
Serial.print (" scaled to " );
137
- Serial.print (analogOut, DEC);
124
+ Serial.println (analogOut, DEC);
138
125
analogWrite (RedPin, analogOut);
139
126
140
127
// Green
141
-
142
- analogValue = analogRead (sensorPinGreen);
143
- analogValue = constrain (analogValue, minReadingGreen, maxReadingGreen);
128
+ analogValueG = analogRead (sensorPinGreen);
129
+ analogValueG = constrain (analogValueG, minReadingGreen, maxReadingGreen);
144
130
Serial.print (" Green = " );
145
- Serial.print (analogValue);
146
-
147
- analogOut = map (analogValue, minReadingGreen, maxReadingGreen, 0 , 255 );
131
+ Serial.println (analogValueG);
132
+ analogOut = map (analogValueG, minReadingGreen, maxReadingGreen, 0 , 255 );
148
133
analogOut = constrain (analogOut, 0 , 255 );
149
134
Serial.print (" scaled to " );
150
- Serial.print (analogOut, DEC);
135
+ Serial.println (analogOut, DEC);
151
136
analogWrite (GreenPin, analogOut);
152
137
153
- // Blue
154
-
155
- analogValue = analogRead (sensorPinBlue);
156
- analogValue = constrain (analogValue, minReadingBlue, maxReadingBlue);
138
+ // Blue
139
+ analogValueB = analogRead (sensorPinBlue);
140
+ analogValueB = constrain (analogValueB, minReadingBlue, maxReadingBlue);
157
141
Serial.print (" Blue = " );
158
- Serial.print (analogValue);
159
-
160
- analogOut = map (analogValue, minReadingBlue, maxReadingBlue, 0 , 255 );
142
+ Serial.println (analogValueB);
143
+ analogOut = map (analogValueB, minReadingBlue, maxReadingBlue, 0 , 255 );
161
144
analogOut = constrain (analogOut, 0 , 255 );
162
145
Serial.print (" scaled to " );
163
146
Serial.println (analogOut, DEC);
164
147
analogWrite (BluePin, analogOut);
165
148
166
- delay (25 );
167
-
168
- /* TEST AREA ABOVE!! Don't delete below just yet!
169
- //Red
170
- analogValue = analogRead(sensorPinRed);
171
- analogOut = map(analogValue, minReadingRed, maxReadingRed, 0, 255);
172
- Serial.print("Red = " );
173
- Serial.print(analogValue, DEC);
174
- analogWrite(RedPin, analogOut);
175
-
176
- //Green
177
- analogValue = analogRead(sensorPinGreen);
178
- analogOut = map(analogValue, minReadingGreen, maxReadingGreen, 0, 255);
179
- Serial.print("Green = " );
180
- Serial.print(analogValue, DEC);
181
- analogWrite(GreenPin, analogOut);
182
-
183
- //Blue
184
- analogValue = analogRead(sensorPinBlue);
185
- analogOut = map(analogValue, minReadingBlue, maxReadingBlue, 0, 255);
186
- Serial.print("Blue = " );
187
- Serial.println(analogValue, DEC);
188
- analogWrite(BluePin, analogOut);
189
-
190
- delay(25); */
149
+ delay (500 );
191
150
}
0 commit comments