Hello.
I was trying to make this arduino-based multimeter and at some point of testing I had to test the situation where I wanted to switch between capacitance measuring and resistance measuring.
first I tried using a rotary encoder but it was a bit too complicated for me so I tried a button and made this code (I copied CapacitanceMeter from arduino projects and a resistance thing from a website then made 2 resistance measuring functions ("lowend" with 10k reference resistor for up to 50k ohms and "highend" with 1M reference for more)
but then when I added the capacitance meter I had an issue. I made 2 do-while s in my void loop , one of them does the resistance measuring and the other is for capacitance.
I tried using such a structure in my code for switching between the two with a button :
do{
//resistance measuring code
if (digitalRead(9)){
p = false;
q = true;
}
else {
p = true;
q=false;
}
}while(p);
do{
//capacitance measuring code
if (digitalRead(9)) {
p = true;
q= false;
}
else {
p = false;
q = true;
}
}while(q);
9 is connected to the switch.
the code starts measuring resistance (results are printed in serial monitor) but when I press the button it just stops measuring resistance and doesnt print capacitance results.
this is my entire code but its really messy (since it is a mixture of 3 different codes) :
I have also pasted it on pastebin since it was a bit long
#define analogPinc 2 // analog pin for measuring capacitor voltage
#define chargePin 8 // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin 7 // pin to discharge the capacitor
#define resistorValue 10000.0F // change this to whatever resistor value you are using
// F formatter tells compliler it's a floating point value
unsigned long startTime;
unsigned long elapsedTime;
float microFarads; // floating point variable to preserve precision, make calculations
float nanoFarads;
int analogPin= 0;
int raw= 0;
int Vin= 4.3;
float Vout= 0;
float R1= 10000;
int analogPinp = 1;
float R2= 0;
float buffer= 0;
bool p,q;
void setup()
{
Serial.begin(9600);
pinMode(chargePin, OUTPUT); // set chargePin to output
digitalWrite(chargePin, LOW);
}
void loop()
{
do{
/* THIS PART IS OHMMETER PART
*/
lowend();
Serial.print(" lowend : ");
Serial.println(R2);
Serial.println(" ");
if (R2 > 50000) {
highend();
Serial.print(" highend : ");
Serial.println(R2);
Serial.println(" ");
Serial.println("=======================");
}
delay(1000);
if (digitalRead(9)){
p = false;
q = true;
//break;
}
else {
p = true;
q=false;
}
}while(p);
do{
/* THIS PART IS THE CAPACITANCEMETER CODE
*/
digitalWrite(chargePin, HIGH); // set chargePin HIGH and capacitor charging
startTime = millis();
while(analogRead(analogPinc) < 648){ // 647 is 63.2% of 1023, which corresponds to full-scale voltage
}
elapsedTime= millis() - startTime;
// convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ), net 10^3 (1000)
microFarads = ((float)elapsedTime / resistorValue) * 1000;
Serial.print(elapsedTime); // print the value to serial port
Serial.print(" mS "); // print units and carriage return
if (microFarads > 1){
Serial.print((long)microFarads); // print the value to serial port
Serial.println(" microFarads"); // print units and carriage return
}
else
{
// if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad).
// This is a workaround because Serial.print will not print floats
nanoFarads = microFarads * 1000.0; // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
Serial.print((long)nanoFarads); // print the value to serial port
Serial.println(" nanoFarads"); // print units and carriage return
}
/* dicharge the capacitor */
digitalWrite(chargePin, LOW); // set charge pin to LOW
pinMode(dischargePin, OUTPUT); // set discharge pin to output
digitalWrite(dischargePin, LOW); // set discharge pin LOW
while(analogRead(analogPinc) > 0){ // wait until capacitor is completely discharged
}
pinMode(dischargePin, INPUT); // set discharge pin back to input
if (digitalRead(9)) {
p = true;
q= false;
//break;
}
else {
p = false;
q = true;
}
}while(q);
//PCMR
/* Serial.print("buffer2: ");
Serial.println(buffer);
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
Serial.println(" ");
Serial.println(" ");*/
delay(1000);
}
void lowend () {
// MEASURES RESISTANCE WITH A 10K REF RESISTOR
//o
//o
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
raw= analogRead(analogPin);
if(raw)
{
buffer= raw * Vin;
//Serial.print("raw : ");
//Serial.println(raw);
//Serial.print("Vout: ");
//Serial.println(Vout);
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
}
}
void highend () {
//MEASURES RESISTANCE WITH A 1M REF RESISTOR
//o
//o
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
raw= analogRead(analogPin);
if(raw)
{
R1 = 1000000;
analogPin = 1;
buffer= raw * Vin;
//Serial.print("raw : ");
//Serial.println(raw);
//Serial.print("Vout: ");
//Serial.println(Vout);
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
}
}
I would be more than grateful if someone could help me with this , I dont understand why it fails to work and I keep trying to trace it but cant seem to understand the issue.