How can i make while loop work faster

I was writting a code including ADC and i want to count changes in voltage. The program include lots of if() else() so i used a while() loop to count changes to increase speed . But program in while() is working even slower than before.
After changing prescale to 16 for ADC there was improvement but still i can count upto 10000 changes per sec using only void loop()
While while() can only count upto 5000.

Any suggestion is appreciated.
Thanks

Hello,

do yourself a favour and please read How to get the best out of this forum

if we were to know which ADC, which Arduino, how things are wired and powered and have a glimpse on your code, may be we could provide a meaningful input...

PS/ this topic has nothing to do in the Community / Website and Forum.. I moved it to a better place

Do you know how long it takes for the ADC you are using to take a reading? Getting 10000 readings a second may be beyond hobby grade components.

@devilsvenom Your findings make little sense outside of the context that supports them.

Please post your code, perhaps two versions if you think so, that we might see why you are getting the results you state.

The slow thing is probably the conversion. How you structure the code, if/else or while or whatever should have little to do with how fast you can loop the loop.

TIA

a7

I'm using an arduino nano

#define FASTADC 1 
#ifndef cbi 
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) 
#endif 
#ifndef sbi 
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) 
#endif


unsigned long int cl=0,cr=0,curti=0,sti=0,sub=0,v1=0,v2=0;
int x=0,y=0;

void setup(){
    
    #if FASTADC 
     sbi(ADCSRA,ADPS2) ;
     cbi(ADCSRA,ADPS1) ;
     cbi(ADCSRA,ADPS0) ;
    
    #endif
    
    Serial.begin(115200);
    
    
}

void loop(){
    
    if(analogRead(A1)>60){
    sti=micros();
        while(curti<sti+1000000){
            
                 
        if(analogRead(A1)>30&&x==0){
            cl=cl+1;
            x=1;
            
        }
        if(analogRead(A1)<30&&x==1){
            x=0;
        }
            
        
            
            
            curti=micros();
          
            
        }
        
        Serial.println(cr);
        Serial.println(cl);
       // v1=cr;
       // v2=cl;
        cl=0;
        cr=0;
        sub=0;
        
        delay(100);
        
    }
    
    
}
#define FASTADC 1 
#ifndef cbi 
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) 
#endif 
#ifndef sbi 
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) 
#endif


unsigned long int cl=0,cr=0,curti=0,sti=0,sub=0;
int x=0,y=0,m=0;

void setup(){
    
    #if FASTADC 
     sbi(ADCSRA,ADPS2) ;
     cbi(ADCSRA,ADPS1) ;
     cbi(ADCSRA,ADPS0) ;
    
    #endif
    
    Serial.begin(115200);
    
    
}



void loop(){
    
    if(analogRead(A1)>30&&x==0){
        cr++;
        x=1;
        m=1;
        if(y==0){
            sti=micros();
            y=1;
            
        }
    }
    if(analogRead(A1)<30&&x==1){
        x=0;
        
    }
    if(m==1){
    curti=micros();
        }
    if(curti-sti>1000000){
        Serial.println(cr);
        
        y=0;
        sti=0;
        curti=0;
        m=0;
        cr=0;
        
    }
}

I am using these two codes to compare the speed.

Not your issue but this

is better written as

while(curti - sti < 1000000UL) {

you call many times analogRead() in the same context. read it once and use the variable for the tests. use a bool instead of an int for x but not sure what you are trying to achieve there.

PS: is the space bar is broken on your keyboard ? :slight_smile: