Hey everyone,
I am currently struggeling in measuring a one way rectified sine wave with my arduino board. I have a function generator which gives me a sine wave. I rectify it by a diode and measure the signal with my adc. No problem so far.
Now I just like to have the maximum point of the sine wave, but my code gives me the right values sometimes, sometimes the wrong values. I know why, but I am not that experienced to solve the programming task here by myself. I am continuosly asking the adc if it has a higher value for me than in the variable "highvalue". If it is, we have a new highvalue, if not than a counter counts up. If that counter reaches 10 I return my highvalue.
The reason why I get wrong values is because of noise or a lack of adc accuracy. Then the counter counts up, but it shouldnt. Or in the half wave that has been cut off there is a little noise that when the upper_halfwave function starts there I will get a 5 in my highvalue and then just zeros out of the adc and in the end my high value is 5.
Can anyone help me out with that ? Here is my code:
I am currently struggeling in measuring a one way rectified sine wave with my arduino board. I have a function generator which gives me a sine wave. I rectify it by a diode and measure the signal with my adc. No problem so far.
Now I just like to have the maximum point of the sine wave, but my code gives me the right values sometimes, sometimes the wrong values. I know why, but I am not that experienced to solve the programming task here by myself. I am continuosly asking the adc if it has a higher value for me than in the variable "highvalue". If it is, we have a new highvalue, if not than a counter counts up. If that counter reaches 10 I return my highvalue.
The reason why I get wrong values is because of noise or a lack of adc accuracy. Then the counter counts up, but it shouldnt. Or in the half wave that has been cut off there is a little noise that when the upper_halfwave function starts there I will get a 5 in my highvalue and then just zeros out of the adc and in the end my high value is 5.
Can anyone help me out with that ? Here is my code:
Code:
int upper_halfwave()
{
while(1)
{
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC))
{
}
adc_value = ADCW;
usart(adc_value);
if (adc_value>=highvalue)
{
highvalue=adc_value;
counter=0;
}
else
{
counter++;
}
if (counter==10)
{
highvalue=0;
return highvalue;
}
}
}