Go Down

Topic: The Jitters (Read 1 time) previous topic - next topic

Thud

Thanks, frank26080115.

I'll give that a go. Cheers.

dshay

#6
Apr 26, 2011, 08:31 am Last Edit: Apr 26, 2011, 08:33 am by dshay Reason: 1
I'd like to give Joe Pardue of Smiley Micros credit for me learning this, if you want a quick, dirty, and efficient way to input on your ADC with less jitter, do a bit shifting 3 bit trick as follows:

int thisval;
int total=0;

for ( int x=0;  x<8;  x++)              //Inputs the value from pin (now variable a) in a for loop.
       
        {
            total += analogRead(xpin);     //Compound addition operation
        }                               //Equivalent to total = total + AnalogInput
                                       
     
      thisval = total >> 3;              //Averages the eight values the for loop added via bit shifting to smooth out sensor noise/inaccuracy, and thus also helps with digital debouncing. (false   

                                               //triggering if value is a level threshold measurement)

// There are a couple tricks here, first +=, which you can study on compound addition in the reference section, second, and less easy to grasp, is what's happening with:   total >>3;
// Like I said before, here's where the savings starts. You get to do an average without doing floating point math. You shift the sum of eight numbers 3 bits over and you'll pretty much //end up with the average of the eight and bit shifting is easier on a microprocessor than floating point math. These efficiency savings add up, even though my program worked fine while I //was doing an average of three numbers.  

frank26080115


I'd like to give Joe Pardue of Smiley Micros credit for me learning this, if you want a quick, dirty, and efficient way to input on your ADC with less jitter, do a bit shifting 3 bit trick as follows:

int thisval;
int total=0;

for ( int x=0;  x<8;  x++)              //Inputs the value from pin (now variable a) in a for loop.
       
        {
            total += analogRead(xpin);     //Compound addition operation
        }                               //Equivalent to total = total + AnalogInput
                                       
     
      thisval = total >> 3;              //Averages the eight values the for loop added via bit shifting to smooth out sensor noise/inaccuracy, and thus also helps with digital debouncing. (false   

                                               //triggering if value is a level threshold measurement)

// There are a couple tricks here, first +=, which you can study on compound addition in the reference section, second, and less easy to grasp, is what's happening with:   total >>3;
// Like I said before, here's where the savings starts. You get to do an average without doing floating point math. You shift the sum of eight numbers 3 bits over and you'll pretty much //end up with the average of the eight and bit shifting is easier on a microprocessor than floating point math. These efficiency savings add up, even though my program worked fine while I //was doing an average of three numbers.  


This method is expensive time-wise during 8 consecutive calls to analogRead

You can also keep the 8 previous samples in a buffer and only execute analogRead once per iteration instead of 8, much faster at the cost of more memory for the buffer.
Freelance engineer, consultant, contractor. Graduated from UW in 2013.

dshay


This method is expensive time-wise during 8 consecutive calls to analogRead

You can also keep the 8 previous samples in a buffer and only execute analogRead once per iteration instead of 8, much faster at the cost of more memory for the buffer.


If concern for time is an overriding factor the greatest time hog will be sensor noise/bounce being transmitted at a serial baud rate of 32150 constantly, a debounce function would also help,
but even then the project would keep transmitting at a duty cycle of whatever the debounce length is.

Musical projects need control and repeatability. With light sensing you'd need an average of as many readings as you can get away with before lag and if it's only one sensor eight readings definitely isn't a problem, and also, one could create the buffer array you speak of to save on time.

Take Reading

Reading different than last?

Send Controller Change Message.

Without doing a good amount of averaging change messages would be sent repeatedly, even when nothing is being sensed or the musician is holding perfectly still.

Although by analysis .1(current reading) + .9(last value) is pretty close to the average of eight. Each multiplication in that equation takes up one operation cycle. If I knew how to simulate in AVR studio I could analyze this. I'm going to get on that one of these days.

I think I like your method better come to think of it but mine works on my current project.

Go Up