Skip to content

Commit 083127b

Browse files
committed
Sketch modified to allow for either auto-scaling of accelerometer response, or use of default values if no action taken during calibration period. Deleted the similar sketch that only allowed for default values.
1 parent fbdbbcc commit 083127b

File tree

1 file changed

+54
-95
lines changed

1 file changed

+54
-95
lines changed
+54-95
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Vernier3DA (v 2014.12)
2+
Vernier3DA (v 2017.08)
33
This project assumes that you have a Vernier 3D Accelerometer connected to
44
three BTA connectors. It also assumes that you have an RGB LED connected to
55
PWM outputs 3, 5, and 6.
@@ -12,17 +12,15 @@ The brightness of the blue LED is controlled by the z axis acceleration.
1212
1313
During the first 15 seconds of this program you should rotate the
1414
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
1617
and end of this "calibration".
1718
After calibration, each color should progress through a range of "off" to
1819
full power based on the orientation of the accelerometer. Sitting flat on a
1920
surface (z-axis up) is blue, flat on its side (y-axis up) is green, and cord
2021
facing down is red.
2122
Note that either individual LEDs or a RGB LED may be used.
2223
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-
2624
See www.vernier.com/arduino for more information.
2725
*/
2826
const int RedPin = 3;
@@ -34,30 +32,28 @@ const int sensorPinGreen = A2;
3432
const int sensorPinBlue = A4;
3533

3634
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;
4541

46-
int analogValue = 0;
42+
int analogValueR = 0;
43+
int analogValueG = 0;
44+
int analogValueB = 0;
4745
int time;
4846
int analogOut;
49-
int flash;
5047

5148
void setup()
5249
{
5350
Serial.begin(9600);
54-
Serial.println();
51+
5552
}
5653
void loop()
5754
{
58-
5955
while (time < 1000){ // LEDs to flash at start of calibration
60-
time = millis();
56+
time = millis();
6157
analogWrite(RedPin, 255);
6258
analogWrite(GreenPin, 255);
6359
analogWrite(BluePin, 255);
@@ -66,33 +62,27 @@ void loop()
6662
analogWrite(GreenPin, 0);
6763
analogWrite(BluePin, 0);
6864
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
7972
//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
8676
//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);
9686
analogWrite(RedPin, 255);
9787
analogWrite(GreenPin, 255);
9888
analogWrite(BluePin, 255);
@@ -102,12 +92,11 @@ void loop()
10292
analogWrite(BluePin, 0);}
10393

10494
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
111100
Serial.print("minReadingRed = " );
112101
Serial.print(minReadingRed, DEC);
113102
Serial.print(" maxReadingRed = " );
@@ -125,67 +114,37 @@ void loop()
125114
// Below is scrolling printout
126115

127116
//Red
128-
129-
analogValue = analogRead(sensorPinRed);
130-
analogValue = constrain(analogValue, minReadingRed, maxReadingRed);
117+
analogValueR = analogRead(sensorPinRed);
118+
analogValueR = constrain(analogValueR, minReadingRed, maxReadingRed);
131119
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);
135122
analogOut = constrain(analogOut, 0, 255);
136123
Serial.print(" scaled to " );
137-
Serial.print(analogOut, DEC);
124+
Serial.println(analogOut, DEC);
138125
analogWrite(RedPin, analogOut);
139126

140127
//Green
141-
142-
analogValue = analogRead(sensorPinGreen);
143-
analogValue = constrain(analogValue, minReadingGreen, maxReadingGreen);
128+
analogValueG = analogRead(sensorPinGreen);
129+
analogValueG = constrain(analogValueG, minReadingGreen, maxReadingGreen);
144130
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);
148133
analogOut = constrain(analogOut, 0, 255);
149134
Serial.print(" scaled to " );
150-
Serial.print(analogOut, DEC);
135+
Serial.println(analogOut, DEC);
151136
analogWrite(GreenPin, analogOut);
152137

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);
157141
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);
161144
analogOut = constrain(analogOut, 0, 255);
162145
Serial.print(" scaled to " );
163146
Serial.println(analogOut, DEC);
164147
analogWrite(BluePin, analogOut);
165148

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);
191150
}

0 commit comments

Comments
 (0)