1. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    hi there,

    I have 4 rotary type variable resistors each attached to one of the
    ADC channel of an atmega8 (PC5-PC2).

    An unsigned integer array of size 4 named as "min" holds the minimum ADC value for each of the resistors (i.e when resistance is maximum)

    similarly an unsigned integer array of size 4 named as "max" holds the maximum ADC value for each of the resistors (i.e when resistance is minimum)

    Both of above are measured during runtime.

    The problem arises under following case

    4 LEDs are set to glow when respective resistor's ADC value crosses the minimum value.

    but here only three of the LEDs glow when min their repective min value is crossed while the 4th one one takes much higher ADC value than minimum measured for it to glow (almost maximum)!

    why does it happens so ?

    again to cross check my circuit I set the LEDs to glow when the ADC value is more than mid of min and max values (syntactically (max+min)/2)

    this time it works correctly and the LED glows after its respective ADC value reaches midway

    also If I specifically set only the 4th LED to glow when the ADC value of its associated resistor goes above half of the min value then it glows after ADC has crossed about 1/4th of min to max

    how is this possible ? why there is such anomalous behaviour?

    here are code snippets
    Code ( (Unknown Language)):
    1.  
    2. //taking min values
    3.          for(int i=1;i<5;i++)
    4.          {
    5.          min[i-1]=getADC(i);//getADC returns ADC value for i+1 th channel
    6.          }
    7. //similar for max
    8.  
    Code ( (Unknown Language)):
    1. //check ADC value
    2. for(short i=1;i<5;i++)
    3. {reading[i-1]=getADC(i);
    4.        
    5. if(reading[i-1]>min[i-1])
    6. {ledSelective(i,1);}
    7. else
    8. {ledSelective(i,0);}
    9.  //led Selective glows up LED for resistor i
    Code ( (Unknown Language)):
    1.  
    2. //check against mid way value (WORKING ONE)
    3.  
    4. for(short i=1;i<5;i++)
    5. {reading[i-1]=getADC(i);
    6.        
    7. if(reading[i-1]>((max[i-1]+min[i-1])/2))
    8. {ledSelective(i,1);}
    9. else
    10. {ledSelective(i,0);}
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Odd ADC behavior happens when there isn't a bypass cap on the IC, try a 0.1uF / 100nF cap between V+ and GND

    For the odd display, how often are the two functions called? I don't see a test for minimum before you put value in the minimum array? If both are essentially the same function, and called within thousandths of a second from each other, won't they be the same values for min and max of each resistor?
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Such an array has elements 0, 1, 2, and 3.

    Your code is accessing elements 1, 2, 3, and 4.

    Let me guess: the pot connected to element 4 is the one with strange response, correct?
     
  4. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    NOPE I have written min[i-1] for that purticular reason
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Perhaps, you should post your whole code and a schematic. Otherwise, it's all going to be stabbing in the dark...
     
Loading...