-
Notifications
You must be signed in to change notification settings - Fork 0
/
LOFR
172 lines (148 loc) · 3.35 KB
/
LOFR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int inbutton = 9;
int buttonstate = LOW;
int lastbuttonstate = HIGH;
int actual;
int trigpin = A4;
int echopin = A5;
int aPinIn = A0;
int val = 0;
float Vout = 0.0;
float Vin = 4.66;
float Rknown = 10000.0;
float Runknown = 0.0;
float U = 0.0;
float I = 0.0;
int backlight = 13;
int analogPin = 1;
int chargePin = 6;
int dischargePin = 7;
float resistorValue = 1000.0;
unsigned long startTime;
unsigned long elapsedTime;
float microFarads;
float nanoFarads;
float picoFarads;
void setup(){
pinMode(inbutton, INPUT);
digitalWrite(inbutton, HIGH);
lcd.begin(16, 2);
pinMode(backlight, OUTPUT);
pinMode(chargePin, OUTPUT);
digitalWrite(chargePin, LOW);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
digitalWrite(backlight, HIGH);
}
void loop()
{
buttonstate = digitalRead(inbutton);
if (buttonstate == HIGH && buttonstate != lastbuttonstate)
{
if(actual == 3) actual = 0;
else actual++;
}
lastbuttonstate = buttonstate;
switch(actual)
{
case 0:
range();
break;
case 1:
ohm();
break;
case 2:
farad();
break;
}
}
void range() {
float duration;
float cm;
pinMode(trigpin, OUTPUT);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
pinMode(echopin, INPUT);
duration = pulseIn(echopin, HIGH);
cm = microsecondsToCentimeters(duration);
if (cm > 500){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Out of Range");
delay(500);
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("The object is");
lcd.setCursor(0, 1);
lcd.print(cm);
lcd.print(" cm far");
delay(500);
}
}
float microsecondsToCentimeters(float microseconds){
return microseconds / 29 / 2;
}
void ohm(){
lcd.clear();
val = analogRead(aPinIn);
Vout = (Vin/1024.0) * val;
U = (Vin - Vout);
I = (U/Rknown);
Runknown = (Vout/I);
lcd.print("Vout: ");
lcd.print(Vout);
lcd.setCursor(0, 1);
lcd.print("R: ");
lcd.print(Runknown);
delay(1000);
}
void farad(){
digitalWrite(chargePin, HIGH);
startTime = millis();
while(analogRead(analogPin) < 648){
}
elapsedTime= millis() - startTime;
microFarads = ((float)elapsedTime / resistorValue) * 1000;
if(elapsedTime == 0){
lcd.clear();
lcd.print("Insert a");
lcd.setCursor(0, 1);
lcd.print("Capacitor");
delay(1000);
}
if (microFarads >= 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print((long)microFarads);
lcd.print(" microFarad");
lcd.setCursor(0,0);
}
else if (microFarads <= 1){
nanoFarads = microFarads * 1000.0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print((long)nanoFarads);
lcd.print(" nanoFarad");
lcd.setCursor(0,0);
}
if (nanoFarads >= 1){
picoFarads = nanoFarads * 1000.0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print((long)picoFarads);
lcd.print(" picoFarad");
lcd.setCursor(0,0);
}
digitalWrite(chargePin, LOW);
pinMode(dischargePin, OUTPUT);
digitalWrite(dischargePin, LOW);
while(analogRead(analogPin) > 0){
}
pinMode(dischargePin, INPUT);
}