1
+
1
2
/*
2
3
Name : LR_meter_ardunio
3
4
Title : Resistance and Inductance meter. Ardunio ucontroller based.
10
11
*/
11
12
12
13
// ****************** Libraries ********************
13
- #include < Wire.h>
14
14
#include < Adafruit_SSD1306.h> // OLED 1.1.2
15
15
16
16
// ***************** GLOBALS ********************
@@ -31,10 +31,8 @@ int lastButtonModeState = HIGH; // the previous reading from the input pin
31
31
// the following variable are unsigned longs because the time, measured in
32
32
// milliseconds, will quickly become a bigger number than can be stored in an int.
33
33
unsigned long lastDebounceModeTime = 0 ; // the last time the output pin was toggled
34
-
35
34
unsigned long debounceDelay = 50 ; // the debounce time;
36
35
37
-
38
36
// Var to hold menu mode
39
37
uint8_t mode = 0 ;
40
38
@@ -58,12 +56,31 @@ const int PulseInPin = 12; //digital pin to read in pulse
58
56
#define OLED_RESET 4
59
57
Adafruit_SSD1306 display (OLED_RESET);
60
58
59
+ // **************** FUNCTION PROTOTYPES ***********
60
+ void DisplayInit ();
61
+ void Serialinit ();
62
+ void GPIOinit ();
63
+ void OLEDready ();
64
+ void DisplayHelpMsg ();
65
+ void ResScaleOne ();
66
+ void ResScaleTwo ();
67
+ void ResScaleThree ();
68
+ void ResScaleFour ();
69
+ void Ltest ();
70
+ void AnalogDisplay ();
71
+ float calcResult (float R1, int multi_factor);
72
+ void PrintResult (String unit, float R2);
73
+ void printMenuMsg ();
74
+ void TestRun ();
75
+ void ReadPushButtonMode ();
76
+ void ReadPushButtonTest ();
77
+
61
78
// *************** SETUP *************
62
79
void setup ()
63
80
{
64
81
Serialinit ();
65
82
GPIOinit ();
66
- Display_init ();
83
+ DisplayInit ();
67
84
}
68
85
69
86
// ******************* MAIN LOOP *****************
@@ -76,13 +93,28 @@ void loop()
76
93
77
94
// ********************* Functions *************************
78
95
79
- // Function to init OLED and display OLED welcome message
80
- void Display_init ()
96
+ // Function to display help message related to PCB socket layout
97
+ void DisplayHelpMsg ()
81
98
{
99
+ display.clearDisplay ();
100
+ display.setCursor (0 , 0 );
101
+ display.setTextSize (1 );
102
+ display.setTextColor (WHITE);
103
+ display.print (" Connector pinout" );
104
+ display.setCursor (0 , 12 );
105
+ display.print (" Row1 : R1 RRRR" );
106
+ display.setCursor (0 , 22 );
107
+ display.print (" Row2 : A G V LL" );
108
+ display.display ();
109
+ delay (5000 );
110
+ OLEDready ();
111
+ }
82
112
113
+ // Function to init OLED and display OLED welcome message
114
+ void DisplayInit ()
115
+ {
83
116
display.begin (SSD1306_SWITCHCAPVCC, 0x3C );
84
117
display.clearDisplay ();
85
-
86
118
display.setCursor (0 , 0 );
87
119
display.setTextSize (2 );
88
120
display.setTextColor (WHITE);
@@ -102,16 +134,14 @@ void OLEDready()
102
134
display.setCursor (0 , 0 );
103
135
display.setTextSize (2 );
104
136
display.setTextColor (WHITE);
105
- display.print (" LR Meter " );
137
+ display.print (" MODE " );
106
138
display.setCursor (0 , 15 );
107
- display.print (" Ready" );
139
+ display.print (" Ready TEST " );
108
140
display.display ();
109
141
Serial.println (" LR Meter Ready" );
110
142
digitalWrite (LED_BUILTIN, HIGH);
111
143
mode = 0 ;
112
144
delay (50 );
113
-
114
-
115
145
}
116
146
117
147
// Function to setup serial called from setup
@@ -147,11 +177,10 @@ void GPIOinit()
147
177
// Inductance test pins
148
178
pinMode (PulseInPin, INPUT);
149
179
pinMode (OutLtestPin, OUTPUT);
150
-
151
180
}
152
181
153
- // Function scale_one :resistor 0 to 2k range test.
154
- void scale_one ()
182
+ // Function ResScaleOne :resistor 0 to 2k range test.
183
+ void ResScaleOne ()
155
184
{
156
185
digitalWrite (apply_voltage, HIGH);
157
186
pinMode (Res2K, OUTPUT);
@@ -161,20 +190,23 @@ void scale_one()
161
190
digitalWrite (Res2K, LOW);
162
191
float R2 = 0 ;
163
192
float R1 = 2.005 ; // Set this value to the value of the used resistor in K ohms
164
- R2 = calc_Res (R1, 1000 );
193
+ R2 = calcResult (R1, 1000 );
165
194
if (R2 > (R1 * 1000 ))
166
195
{
167
- mode = 8 ; // increase scale
196
+ Serial.println (" Increasing Scale" );
197
+ mode = 2 ; // increase scale
168
198
printMenuMsg ();
199
+ // ResetResRange();
200
+ ResScaleTwo ();
169
201
}
170
202
if (R2 < (R1 * 1000 ))
171
203
{
172
- print_Res (" ohms" , R2);
204
+ PrintResult (" ohms" , R2);
173
205
}
174
206
}
175
207
176
- // Function scale_two : resistor 2K to 20k range test.
177
- void scale_two ()
208
+ // Function ResScaleTwo : resistor 2K to 20k range test.
209
+ void ResScaleTwo ()
178
210
{
179
211
digitalWrite (apply_voltage, HIGH);
180
212
pinMode (Res2K, INPUT);
@@ -184,20 +216,23 @@ void scale_two()
184
216
digitalWrite (Res20K, LOW);
185
217
float R2 = 0 ;
186
218
float R1 = 18.3 ; // Set this value to the value of the used resistor in K ohms
187
- R2 = calc_Res (20.03 , 1 );
219
+ R2 = calcResult (20.03 , 1 );
188
220
if (R2 > R1)
189
221
{
190
- mode = 8 ; // increase scale
222
+ Serial.println (" Increasing Scale" );
223
+ mode = 3 ; // increase scale
191
224
printMenuMsg ();
225
+ // ResetResRange();
226
+ ResScaleThree ();
192
227
}
193
228
if (R2 < R1)
194
229
{
195
- print_Res (" k ohms" , R2);
230
+ PrintResult (" k ohms" , R2);
196
231
}
197
232
}
198
233
199
- // Function scale_three : resistor test 20k to 200k range test.
200
- void scale_three ()
234
+ // Function ResScaleThree : resistor test 20k to 200k range test.
235
+ void ResScaleThree ()
201
236
{
202
237
digitalWrite (apply_voltage, HIGH);
203
238
pinMode (Res2K, INPUT);
@@ -207,60 +242,83 @@ void scale_three()
207
242
digitalWrite (Res200K, LOW);
208
243
float R2 = 0 ;
209
244
float R1 = 218 ; // Set this value to the value of the used resistor in K ohms
210
- R2 = calc_Res (R1, 1 );
245
+ R2 = calcResult (R1, 1 );
211
246
if (R2 > R1)
212
247
{
213
- mode = 8 ; // increase scale
248
+ Serial.println (" Increasing Scale" );
249
+ mode = 4 ; // increase scale
214
250
printMenuMsg ();
251
+ // ResetResRange();
252
+ ResScaleFour ();
215
253
}
216
254
if (R2 < R1)
217
255
{
218
- print_Res (" k ohms" , R2);
256
+ PrintResult (" k ohms" , R2);
219
257
}
220
258
}
221
- // Function scale_four :UP pressed on joystick carry out 200k to 1M range test.
222
- void scale_four ()
259
+ // Function ResScaleFour : resistence test 200k to 1M range test.
260
+ void ResScaleFour ()
223
261
{
224
262
digitalWrite (apply_voltage, HIGH);
225
263
pinMode (Res2K, INPUT);
226
264
pinMode (Res20K, INPUT);
227
265
pinMode (Res200K, INPUT);
228
266
pinMode (Res1M, OUTPUT);
229
- digitalWrite (Res200K , LOW);
267
+ digitalWrite (Res1M , LOW);
230
268
float R2 = 0 ;
231
- float R1 = 1.006 ;// Set first value to the value of the used resistor in M ohms
232
- R2 = calc_Res (R1, 1 );
233
- if (R2 > 2 )
269
+ float R1 = 1006 ;// Set first value to the value of the used resistor in Kohms
270
+ R2 = calcResult (R1, 1 );
271
+ if (R2 > 2000 )
234
272
{
235
- mode = 8 ; // increase scale, impossible at this point but tell the user anyway
273
+ mode = 8 ; // Beyond Scale Too high
236
274
printMenuMsg ();
237
275
}
238
- if (R2 < 2 )
276
+ if (R2 < 2000 )
239
277
{
240
- print_Res (" M ohms" , R2);
278
+ if (R2 <= 10 )
279
+ {
280
+ Serial.println (" Decreasing Scale" );
281
+ mode = 1 ; // decrease scale
282
+ printMenuMsg ();
283
+ // ResetResRange();
284
+ ResScaleOne ();
285
+ return ;
286
+ }
287
+ PrintResult (" M ohms" , (R2/1000 ));
241
288
}
242
289
}
243
290
244
- // Function: calc_Res to calculate unknown resistor value
291
+ // Function: calcResult to calculate unknown resistor value
245
292
// Inputs: (2) 1, float R1 known resistor value for given scale
246
- // 2, Integer mulitple factor either 1 or 1000 depending on given scale.
293
+ // 2, Integer multiple factor either 1 or 1000 depending on given scale.
247
294
// Outputs: returns 1, float R2 unknown resitor value
248
- float calc_Res (float R1, int multi_factor)
295
+ float calcResult (float R1, int multi_factor)
249
296
{
250
297
float R2 = 0 ;
251
- float buffer = 0 ;
298
+ float tmpbuffer = 0 ;
252
299
int V_measured = 0 ;
253
- int Vin = 5 ;
300
+ uint8_t Vin = 5 ;
254
301
float Vout = 0 ;
255
- V_measured = analogRead (analogPin); // in 8bits
256
- buffer = V_measured * Vin;
257
- Vout = (buffer) / 1024.0 ; // in volts
258
- buffer = (Vin / Vout) - 1 ;
259
- R2 = R1 * buffer * multi_factor;
302
+ const uint8_t numReadings = 11 ; // number of analog samples
303
+ int readings[numReadings]; // the readings from the analog input
304
+
305
+ // Get 11(numreadings) values from ADC
306
+ for (int thisReading = 0 ; thisReading < numReadings; thisReading++)
307
+ {
308
+ readings[thisReading] = analogRead (analogPin); // ADC
309
+ if (thisReading != 0 ) // ignore first reading as it is bad during auto-range.
310
+ {
311
+ V_measured = V_measured + readings[thisReading]; // running total
312
+ }
313
+ }
314
+ V_measured = (V_measured /(numReadings-1 )); // average
315
+
316
+ Vout = (V_measured * Vin) / 1024.0 ; // Convert ADC to voltage
317
+ tmpbuffer = (Vin / Vout) - 1 ; // voltage divider (VIN/VOUT -1)
318
+ R2 = R1 * tmpbuffer * multi_factor; // R2 = R1(Vin/Vout -1)
260
319
return R2;
261
320
}
262
321
263
-
264
322
// Function to print various messages input based on Menu push button press
265
323
void printMenuMsg ()
266
324
{
@@ -284,7 +342,7 @@ void printMenuMsg()
284
342
Serial.println (" 2k to 20k range" );
285
343
break ;
286
344
case (3 ):
287
- display.print (" R 20k - 0.2M range" );
345
+ display.print (" R 20k - 200k range" );
288
346
display.display ();
289
347
Serial.println (" 20k to 200k range" );
290
348
break ;
@@ -307,9 +365,9 @@ void printMenuMsg()
307
365
OLEDready ();
308
366
break ;
309
367
case (8 ):
310
- display.print (" Increase Scale" );
368
+ display.print (" Out of Scale" );
311
369
display.display ();
312
- Serial.println (" Increase scale" );
370
+ Serial.println (" Out of scale" );
313
371
delay (1500 );
314
372
OLEDready ();
315
373
break ;
@@ -323,22 +381,23 @@ void TestRun()
323
381
switch (mode)
324
382
{
325
383
case (0 ):
384
+ DisplayHelpMsg ();
326
385
__asm__ (" nop\n\t " );
327
386
break ;
328
387
case (1 ):
329
- scale_one ();
388
+ ResScaleOne ();
330
389
break ;
331
390
case (2 ):
332
- scale_two ();
391
+ ResScaleTwo ();
333
392
break ;
334
393
case (3 ):
335
- scale_three ();
394
+ ResScaleThree ();
336
395
break ;
337
396
case (4 ):
338
- scale_four ();
397
+ ResScaleFour ();
339
398
break ;
340
399
case (5 ):
341
- L_test ();
400
+ Ltest ();
342
401
break ;
343
402
case (6 ):
344
403
AnalogDisplay ();
@@ -347,10 +406,10 @@ void TestRun()
347
406
digitalWrite (LED_BUILTIN, HIGH);
348
407
}
349
408
350
- // Function print_Res : Print calculated resistor value and unit to serial monitor
409
+ // Function PrintResult : Print calculated resistor value and unit to serial monitor
351
410
// Inputs (2) : 1 , String unit unit of resistance Kohms Mohms ,
352
411
// 2, float R2 , value of resistance calculated
353
- void print_Res (String unit, float R2)
412
+ void PrintResult (String unit, float R2)
354
413
{
355
414
Serial.println (" Resistance: " );
356
415
Serial.print (R2);
@@ -389,12 +448,11 @@ void AnalogDisplay()
389
448
display.print (VoltValue);
390
449
391
450
display.display ();
392
-
393
451
delay (2000 );
394
452
}
395
453
}
396
- // Function L_test : Calculates Inductance
397
- void L_test ()
454
+ // Function Ltest : Calculates Inductance
455
+ void Ltest ()
398
456
{
399
457
double pulse, frequency, capacitance, inductance = 0 ;
400
458
digitalWrite (OutLtestPin, HIGH);
@@ -409,20 +467,24 @@ void L_test()
409
467
frequency = 1 .E6 / (2 * pulse);
410
468
inductance = 1 . / (capacitance * frequency * frequency * 4 .*3.14159 * 3.14159 );
411
469
inductance *= 1E6 ;
412
-
413
470
}
414
471
415
472
// Serial print
416
473
Serial.print (" High for uS:" );
417
- Serial.print ( pulse );
474
+ Serial.println ( pulse );
418
475
Serial.print (" \t frequency Hz:" );
419
- Serial.print ( frequency );
476
+ Serial.println ( frequency );
420
477
Serial.print (" \t inductance uH:" );
421
478
Serial.println ( inductance );
422
479
423
- display.setCursor (0 , 15 );
480
+ display.setCursor (0 , 12 );
424
481
display.print (inductance);
425
- display.print (" uH" );
482
+ display.print (" uH " );
483
+ display.setCursor (0 , 22 );
484
+ display.print (frequency);
485
+ display.print (" Hz " );
486
+ display.print (pulse);
487
+ display.print (" uS " );
426
488
display.display ();
427
489
delay (2000 );
428
490
OLEDready ();
0 commit comments