[solved] switching between functions with a button code issue

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.

    while (analogRead(analogPinc) < 648)       // 647 is 63.2% of 1023, which corresponds to full-scale voltage
    {
    }
    while (analogRead(analogPinc) > 0)         // wait until capacitor is completely discharged
    {
    }

I would be suspicious of these portions of code. What do you see it you print the value on the pin ?

As a general comment I really dislike do/while loops because unless they are very short and the code is properly formatted it is often not immediately obvious what the exit condition(s) are. Is there a reason why you don't use normal while loops ?

UKHeliBob:

    while (analogRead(analogPinc) < 648)       // 647 is 63.2% of 1023, which corresponds to full-scale voltage

{
    }





while (analogRead(analogPinc) > 0)        // wait until capacitor is completely discharged
    {
    }



I would be suspicious of these portions of code. What do you see it you print the value on the pin ?

As a general comment I really dislike do/while loops because unless they are very short and the code is properly formatted it is often not immediately obvious what the exit condition(s) are. Is there a reason why you don't use normal while loops ?

I exactly copied the capacitancemeter from arduino tutorials and it has worked fine as a standalone code . I chose do-while over while since I wanted the code to run once so booleans "p" and "q" receive a value based on the if inside the first do-while loop.

but I tried while as well, with a single if at the beginning of the loop to give p a value based on the buttons situation and make the code start.

the ResistanceMeter part (first while loop) runs fine but pushing the button does not send it into the next loop , but instead it just gets stuck and sends nothing into the serial monitor.

I also checked the buttons status with an oscilloscope and the button operates normally (works as expected) so it has to be something with the code :confused:

    while (analogRead(analogPinc) > 0) Are you sure that this analogRead() ever returns zero ?

UKHeliBob:
    while (analogRead(analogPinc) > 0) Are you sure that this analogRead() ever returns zero ?

Yes , I believe that is the part that checks to see if the capacitors voltage is 0 or not , in order to know when it becomes 0 and measure time.
I have used those parts alone and they worked great (copied from here )
but I guess the logic behind the while loops is wrong and I need to think another way , like maybe include a break or something so when the button is pushed it breaks out of this while and proceeds onto the next , but then the problem is that the break is inside the if so I need a double break or something :confused:

thanks for the assistance.
I noticed I had made another mistake which caused the issue , a wrong connection in the breadboard which stopped the capacitance measuring . Arduino kept waiting for a signal but the wire is connected to a wrong place so arduino gets stuck in the capacitance measuring loop and stops everything from working.
Though right now it isnt exactly persistent but thats just because of my bad overall design (buttons and all)