Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit af8ea3d

Browse files
agdlsandeepmistry
authored andcommitted
Implemented average and double measurement for resistance calculation
1 parent 4d2fe98 commit af8ea3d

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

examples/PhysicsLabsFirmware/PhysicsLabsFirmware.ino

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,39 @@ BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("500
3939
BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("5002"), BLENotify, 3 * sizeof(float));
4040
BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("5003"), BLENotify, 3 * sizeof(float));
4141

42-
const int LED_PIN = 0;
43-
const int INPUT1_PIN = A3;
44-
const int INPUT2_PIN = A1;
45-
const int INPUT3_PIN = A0;
46-
const int OUTPUT1_PIN = 5;
47-
const int OUTPUT2_PIN = 1;
48-
const int RESISTANCE_PIN = A2;
49-
const int RESISTANCE_AUX = 13;
42+
const int LED_PIN = 0;
43+
const int INPUT1_PIN = A3;
44+
const int INPUT2_PIN = A1;
45+
const int INPUT3_PIN = A0;
46+
const int OUTPUT1_PIN = 5;
47+
const int OUTPUT2_PIN = 1;
48+
const int RESISTANCE_PIN = A2;
49+
const int RESISTANCE_AUX_PIN = 13;
5050

5151
String name;
5252
unsigned long lastNotify = 0;
5353

54+
#define RESISTOR_AUX_LOW 47000.0
55+
#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm
56+
57+
//#define DEBUG //uncomment to debug the code :)
58+
5459
void setup() {
5560
Serial.begin(9600);
61+
#ifdef DEBUG
62+
while (!Serial);
63+
Serial.println("Started");
64+
#endif
5665

5766
pinMode(LED_PIN, OUTPUT);
5867
pinMode(INPUT1_PIN, INPUT);
5968
pinMode(INPUT2_PIN, INPUT);
6069
pinMode(INPUT3_PIN, INPUT);
6170
pinMode(OUTPUT1_PIN, OUTPUT);
6271
pinMode(OUTPUT2_PIN, OUTPUT);
63-
pinMode(RESISTANCE_AUX, OUTPUT);
64-
65-
digitalWrite(RESISTANCE_AUX, LOW);
72+
pinMode(RESISTANCE_AUX_PIN, OUTPUT);
73+
74+
digitalWrite(RESISTANCE_AUX_PIN, LOW);
6675

6776
if (!INA226.begin(0x45)) {
6877
Serial.println("Failled to initialized INA226!");
@@ -135,7 +144,7 @@ void loop() {
135144

136145
if (abs((long)now - (long)lastNotify) >= 100) {
137146
lastNotify = now;
138-
147+
139148
// every 100ms update subscribed characteristics
140149
updateSubscribedCharacteristics();
141150
}
@@ -170,11 +179,63 @@ void updateSubscribedCharacteristics() {
170179
}
171180

172181
if (resistanceCharacteristic.subscribed()) {
173-
float Vout = (analogRead(RESISTANCE_PIN) * 3.30) / 1023.0;
174-
float resistance = 4.7e4 * ((3.3 / Vout) - 1);
182+
float Vout = 0;
183+
float resistanceAuxLow = INFINITY;
184+
float resistanceAuxHigh = INFINITY;
185+
float resistanceAvg = INFINITY; //open circuit as default
186+
187+
digitalWrite(RESISTANCE_AUX_PIN, LOW);
188+
Vout = getVoutAverage();
189+
if (Vout >= 0.1) {
190+
resistanceAuxLow = RESISTOR_AUX_LOW * ((3.3 / Vout) - 1);
191+
}
192+
193+
digitalWrite(RESISTANCE_AUX_PIN, HIGH);
194+
Vout = getVoutAverage();
195+
if (Vout >= 0.1) {
196+
resistanceAuxHigh = RESISTOR_AUX_HIGH * ((3.3 / Vout) - 1);
197+
}
198+
199+
#ifdef DEBUG
200+
Serial.print("Resistance (HIGH): ");
201+
Serial.print(resistanceAuxHigh);
202+
Serial.println(" Ohm");
203+
204+
Serial.print("Resistance (LOW): ");
205+
Serial.print(resistanceAuxLow);
206+
Serial.println(" Ohm");
207+
#endif
208+
209+
if ((resistanceAuxHigh != INFINITY) && (resistanceAuxLow != INFINITY)) {
210+
resistanceAvg = (resistanceAuxHigh + resistanceAuxLow) / 2;
211+
} else if ((resistanceAuxHigh != INFINITY) && (resistanceAuxLow == INFINITY)) {
212+
resistanceAvg = resistanceAuxHigh;
213+
} else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) {
214+
resistanceAvg = resistanceAuxLow;
215+
}
216+
217+
#ifdef DEBUG
218+
Serial.print("Resistance (AVG): ");
219+
Serial.print(resistanceAvg);
220+
Serial.println(" Ohm");
221+
#endif
222+
resistanceCharacteristic.writeValue(resistanceAvg);
223+
}
224+
}
175225

176-
resistanceCharacteristic.writeValue(resistance);
226+
float getVoutAverage() {
227+
float Vout = 0;
228+
for (int i = 0; i < 30; i++) {
229+
Vout += (analogRead(RESISTANCE_PIN) * 3.30) / 1023.0;
177230
}
231+
Vout /= 30;
232+
233+
#ifdef DEBUG
234+
Serial.print("Vout: ");
235+
Serial.print(Vout);
236+
Serial.println("V");
237+
#endif
238+
return Vout;
178239
}
179240

180241
int analogReadAverage(int pin, int numberOfSamples) {

0 commit comments

Comments
 (0)