Skip to content

Commit eea1818

Browse files
Version 2.2
1 parent adfe92e commit eea1818

File tree

3 files changed

+159
-87
lines changed

3 files changed

+159
-87
lines changed

LR_meter/LR_meter.ino

+127-65
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
Name : LR_meter_ardunio
34
Title : Resistance and Inductance meter. Ardunio ucontroller based.
@@ -10,7 +11,6 @@
1011
*/
1112

1213
//****************** Libraries ********************
13-
#include <Wire.h>
1414
#include <Adafruit_SSD1306.h> // OLED 1.1.2
1515

1616
//***************** GLOBALS ********************
@@ -31,10 +31,8 @@ int lastButtonModeState = HIGH; // the previous reading from the input pin
3131
// the following variable are unsigned longs because the time, measured in
3232
// milliseconds, will quickly become a bigger number than can be stored in an int.
3333
unsigned long lastDebounceModeTime = 0; // the last time the output pin was toggled
34-
3534
unsigned long debounceDelay = 50; // the debounce time;
3635

37-
3836
// Var to hold menu mode
3937
uint8_t mode = 0;
4038

@@ -58,12 +56,31 @@ const int PulseInPin = 12; //digital pin to read in pulse
5856
#define OLED_RESET 4
5957
Adafruit_SSD1306 display(OLED_RESET);
6058

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+
6178
//*************** SETUP *************
6279
void setup()
6380
{
6481
Serialinit();
6582
GPIOinit();
66-
Display_init();
83+
DisplayInit();
6784
}
6885

6986
//******************* MAIN LOOP *****************
@@ -76,13 +93,28 @@ void loop()
7693

7794
// ********************* Functions *************************
7895

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()
8198
{
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+
}
82112

113+
//Function to init OLED and display OLED welcome message
114+
void DisplayInit()
115+
{
83116
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
84117
display.clearDisplay();
85-
86118
display.setCursor(0, 0);
87119
display.setTextSize(2);
88120
display.setTextColor(WHITE);
@@ -102,16 +134,14 @@ void OLEDready()
102134
display.setCursor(0, 0);
103135
display.setTextSize(2);
104136
display.setTextColor(WHITE);
105-
display.print("LR Meter");
137+
display.print(" MODE");
106138
display.setCursor(0, 15);
107-
display.print("Ready");
139+
display.print("Ready TEST");
108140
display.display();
109141
Serial.println("LR Meter Ready");
110142
digitalWrite(LED_BUILTIN, HIGH);
111143
mode = 0;
112144
delay(50);
113-
114-
115145
}
116146

117147
//Function to setup serial called from setup
@@ -147,11 +177,10 @@ void GPIOinit()
147177
//Inductance test pins
148178
pinMode(PulseInPin, INPUT);
149179
pinMode(OutLtestPin, OUTPUT);
150-
151180
}
152181

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()
155184
{
156185
digitalWrite(apply_voltage, HIGH);
157186
pinMode(Res2K, OUTPUT);
@@ -161,20 +190,23 @@ void scale_one()
161190
digitalWrite(Res2K, LOW);
162191
float R2 = 0;
163192
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);
165194
if (R2 > (R1 * 1000))
166195
{
167-
mode = 8; //increase scale
196+
Serial.println("Increasing Scale");
197+
mode = 2; //increase scale
168198
printMenuMsg();
199+
//ResetResRange();
200+
ResScaleTwo();
169201
}
170202
if (R2 < (R1 * 1000))
171203
{
172-
print_Res("ohms", R2);
204+
PrintResult("ohms", R2);
173205
}
174206
}
175207

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()
178210
{
179211
digitalWrite(apply_voltage, HIGH);
180212
pinMode(Res2K, INPUT);
@@ -184,20 +216,23 @@ void scale_two()
184216
digitalWrite(Res20K, LOW);
185217
float R2 = 0;
186218
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);
188220
if (R2 > R1)
189221
{
190-
mode = 8; //increase scale
222+
Serial.println("Increasing Scale");
223+
mode = 3; //increase scale
191224
printMenuMsg();
225+
//ResetResRange();
226+
ResScaleThree();
192227
}
193228
if (R2 < R1)
194229
{
195-
print_Res("k ohms", R2);
230+
PrintResult("k ohms", R2);
196231
}
197232
}
198233

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()
201236
{
202237
digitalWrite(apply_voltage, HIGH);
203238
pinMode(Res2K, INPUT);
@@ -207,60 +242,83 @@ void scale_three()
207242
digitalWrite(Res200K, LOW);
208243
float R2 = 0;
209244
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);
211246
if (R2 > R1)
212247
{
213-
mode = 8; //increase scale
248+
Serial.println("Increasing Scale");
249+
mode = 4; //increase scale
214250
printMenuMsg();
251+
//ResetResRange();
252+
ResScaleFour();
215253
}
216254
if (R2 < R1)
217255
{
218-
print_Res("k ohms", R2);
256+
PrintResult("k ohms", R2);
219257
}
220258
}
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()
223261
{
224262
digitalWrite(apply_voltage, HIGH);
225263
pinMode(Res2K, INPUT);
226264
pinMode(Res20K, INPUT);
227265
pinMode(Res200K, INPUT);
228266
pinMode(Res1M, OUTPUT);
229-
digitalWrite(Res200K, LOW);
267+
digitalWrite(Res1M, LOW);
230268
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)
234272
{
235-
mode = 8; //increase scale, impossible at this point but tell the user anyway
273+
mode = 8; //Beyond Scale Too high
236274
printMenuMsg();
237275
}
238-
if (R2 < 2)
276+
if (R2 < 2000)
239277
{
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));
241288
}
242289
}
243290

244-
// Function: calc_Res to calculate unknown resistor value
291+
// Function: calcResult to calculate unknown resistor value
245292
// 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.
247294
// 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)
249296
{
250297
float R2 = 0;
251-
float buffer = 0;
298+
float tmpbuffer = 0;
252299
int V_measured = 0;
253-
int Vin = 5;
300+
uint8_t Vin = 5;
254301
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)
260319
return R2;
261320
}
262321

263-
264322
// Function to print various messages input based on Menu push button press
265323
void printMenuMsg()
266324
{
@@ -284,7 +342,7 @@ void printMenuMsg()
284342
Serial.println("2k to 20k range");
285343
break;
286344
case (3):
287-
display.print("R 20k - 0.2M range");
345+
display.print("R 20k - 200k range");
288346
display.display();
289347
Serial.println("20k to 200k range");
290348
break;
@@ -307,9 +365,9 @@ void printMenuMsg()
307365
OLEDready();
308366
break;
309367
case (8):
310-
display.print("Increase Scale");
368+
display.print("Out of Scale");
311369
display.display();
312-
Serial.println("Increase scale");
370+
Serial.println("Out of scale");
313371
delay(1500);
314372
OLEDready();
315373
break;
@@ -323,22 +381,23 @@ void TestRun()
323381
switch (mode)
324382
{
325383
case (0):
384+
DisplayHelpMsg();
326385
__asm__("nop\n\t");
327386
break;
328387
case (1):
329-
scale_one();
388+
ResScaleOne();
330389
break;
331390
case (2):
332-
scale_two();
391+
ResScaleTwo();
333392
break;
334393
case (3):
335-
scale_three();
394+
ResScaleThree();
336395
break;
337396
case (4):
338-
scale_four();
397+
ResScaleFour();
339398
break;
340399
case (5):
341-
L_test();
400+
Ltest();
342401
break;
343402
case (6):
344403
AnalogDisplay();
@@ -347,10 +406,10 @@ void TestRun()
347406
digitalWrite(LED_BUILTIN, HIGH);
348407
}
349408

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
351410
//Inputs (2) : 1 , String unit unit of resistance Kohms Mohms ,
352411
// 2, float R2 , value of resistance calculated
353-
void print_Res(String unit, float R2)
412+
void PrintResult(String unit, float R2)
354413
{
355414
Serial.println("Resistance: ");
356415
Serial.print(R2);
@@ -389,12 +448,11 @@ void AnalogDisplay()
389448
display.print(VoltValue);
390449

391450
display.display();
392-
393451
delay(2000);
394452
}
395453
}
396-
//Function L_test: Calculates Inductance
397-
void L_test()
454+
//Function Ltest: Calculates Inductance
455+
void Ltest()
398456
{
399457
double pulse, frequency, capacitance, inductance = 0;
400458
digitalWrite(OutLtestPin, HIGH);
@@ -409,20 +467,24 @@ void L_test()
409467
frequency = 1.E6 / (2 * pulse);
410468
inductance = 1. / (capacitance * frequency * frequency * 4.*3.14159 * 3.14159);
411469
inductance *= 1E6;
412-
413470
}
414471

415472
//Serial print
416473
Serial.print("High for uS:");
417-
Serial.print( pulse );
474+
Serial.println( pulse );
418475
Serial.print("\tfrequency Hz:");
419-
Serial.print( frequency );
476+
Serial.println( frequency );
420477
Serial.print("\tinductance uH:");
421478
Serial.println( inductance );
422479

423-
display.setCursor(0, 15);
480+
display.setCursor(0, 12);
424481
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 ");
426488
display.display();
427489
delay(2000);
428490
OLEDready();

0 commit comments

Comments
 (0)