Harold, In your example if adc has 0-9 it will call DoSomething(); but what we really need is to create spot if it get to 9-10 then DoSomething(); thank you very much for your help Andre Harold Hallikainen wrote: >Sure, I think that would work. The else if would be faster, but with a >small number of comparisons, the improved readability of leaving out the >elses is probably worthwhile. If the ranges "run into each other", a quick >assembly language technique is to add negative numbers, less negative to >more negative, until the result is negative, then branch out of the >checks. A similar technique here would be to use a series of if elses with >only one check per test. For example: > > if(adc<10) > DoSomething(); > else if (adc<20) > DoSomethingElse(); > else if(adc<30) > DoAnotherSomethinElse(); > >This results in only one compare per if and skips the compares after success. > >Harold > > > > > >>Harold, >> >>thanks for your replay. >>Your brought interesting point on ADC conversion. >>suppose I am looking for 951 I better test for 949-953 >> >>if ((adc_x>948)&&(adc_x<954)) // looking for 951 >> >>do you think above example will work? >> >>thanks for your help >> >>Andre >> >> >> >> >>Harold Hallikainen wrote: >> >> >> >>>Without all the elses, the if's after the successful test will continue >>>to >>>run. That is, every test will be run every time. With the elses, tests >>>after the success will be skipped. Another way to do this, which is, I >>>believe, more readable, is to use the switch-case statement. >>> >>>I am surprised that you are testing for 10 specific ADC values. It is >>>unlikey that successive ADC readings will land on exactly a particular >>>number. More likely they will be one count higher or lower, perhaps more >>>variation depending on circuit stability. Do yo want to test the ADC for >>>a >>>range of values? If so, the if, or if else if test seem to be he only way >>>to go (or a very large switch-case statement or a large array where the >>>ADC indexes into it). >>> >>>Harold >>> >>> >>> >>> >>> >>> >>>>Hi to all engineers, >>>> >>>>I am reading ADC to compare 10 different values if they are matched >>>>then each match >>>>has it's own set value. my question is. >>>> >>>>1. what is the different between if else if for example >>>> >>>> if (adc_x==1023) // 5v 1023 >>>> { >>>> xbyte_lsb=0xff ; >>>> xbyte_msb=0x01 ; >>>> } >>>> else if (adc_x==921) // 4,5v 921 >>>> { >>>> xbyte_lsb=0x9A ; >>>> xbyte_msb=0x01 ; >>>> } >>>> >>>>or without else if >>>> >>>> if (adc_x==1023) // 5v 1023 >>>> { >>>> xbyte_lsb=0xff ; >>>> xbyte_msb=0x01 ; >>>> } >>>> if (adc_x==921) // 4,5v 921 >>>> { >>>> xbyte_lsb=0x9A ; >>>> xbyte_msb=0x01 ; >>>> } >>>> >>>>I do not clearly understand what does "else if" do? >>>> >>>>is there better way of testing ADC ? >>>> >>>>Any feedback will appreciate. >>>> >>>>Andre >>>>-- >>>>http://www.piclist.com PIC/SX FAQ & list archive >>>>View/change your membership options at >>>>http://mailman.mit.edu/mailman/listinfo/piclist >>>> >>>> >>>> >>>> >>>> >>> >>> >>> >>> >>-- >>http://www.piclist.com PIC/SX FAQ & list archive >>View/change your membership options at >>http://mailman.mit.edu/mailman/listinfo/piclist >> >> >> > > > > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist