Read ADC sine wave values

Hello Friends,

I am struck in my programming where i am not able to read ADC values (half sine wave signals) from Arduino microcontroller.

I am working on my project where i need to measure the maximum and minimum values (maximum value of that pulse and minimum value of corresponding or adjacent values and compare them). On the other hand i am using a variable frequency and voltage control oscillator to vary the voltage level.

Here is my program - can anyone plz guide me where i can use the loop, functions to read maximum and minimum values.

(code tags added by moderator)

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);


void setup ()

{
  Serial.begin (9600);
}
int Pin=3;
int Pin_VCO=4;
int Pin_ADC=5;

int value = 0;
float voltage = 0;

int i,j;
float VCO = 0;
float VCO_Final2[10];
float VCO_Final;

float ADC_values[10];
float ADC_Final;
float f1=ADC_values[9], f2=ADC_values[0];

double Vp = (3*10^8);

float n =0;
float distance = 0;
float wavelength1 = 0,wavelength2 = 0;

void loop()
{
 VCO = analogRead(Pin_VCO); //Read VCO continuously.
 for(i=0;i<10;i++)
   VCO_Final2[i]= (5.0*VCO)/1023.0; //Read VCO and store in an array
 
 VCO_Final = VCO_Final2[i]; //take every element of array into another single variable
   if(VCO_Final < VCO_Final2[i]) //check if current value < stored value
     cable_Fault(); //if yes, perform further actions.
}

void cable_Fault () 
{
 value = analogRead(Pin); //read input voltage
 voltage = (5.0*value)/1023.0;
 
 for(i=0;i<=10;i++)
 {
   ADC = analogRead(Pin_ADC); //read ADC
   ADC_Final = (5.0*value)/1023.0;
   ADC_values[i] = ADC_Final; //store ADC values in an array of size 10 i.e. 10 values are read for ADC
   Serial.println(ADC_values[i]);
 }
 
 Serial.print(voltage);
 delay(1000);
 
 if(VCO_Final>0 && VCO_Final<1) //if VCO lies between 0 and 1, calculate wavelength, distance and others.
 { 
  calculate();
 }
 else if(VCO_Final>1 && VCO_Final<2)
 {
   calculate();
 }
 else if(VCO_Final>2 && VCO_Final<3)
 {
   calculate();
 }
 else if(VCO_Final>3 && VCO_Final<4)
 {
   calculate();
 }
 else if(VCO_Final>4 && VCO_Final<5)
 {
   calculate();
 }
 else if(VCO_Final>5 && VCO_Final<6)
 {
   calculate();
 }
 else if(VCO_Final>6 && VCO_Final<7)
 {
   calculate();
 }
 else if(VCO_Final>7 && VCO_Final<8)
 {
   calculate();
 }
 else if(VCO_Final>8 && VCO_Final<9)
 {
   calculate();
 }
 else
 {
   calculate();
 }
}

void calculate()
{
  for(i=0;i<=10;i++)
 {
   if(ADC_values[i] > VCO_Final) //if current ADC value > current VCO value
   { 
     VCO_Final = VCO_Final + 0.1; //increment VCO by 0.1
     Serial.println(VCO_Final);
   }
 }
    //calculations and formulae for all parameters. 
     wavelength1 = Vp / f1; 
     wavelength2 = Vp / f2;
     n = wavelength2 / (wavelength1 - wavelength2);
     distance = (n*wavelength1) / 4;
}

Thanks in advance

Sorry, but I can't get italics, or smileys, into the Arduino IDE.

Point 6 here - http://arduino.cc/forum/index.php/topic,97455.0.html

Sorry i didnt get you..

i tried my program running on my microcontroller it gives me junk values and its not comparing the values (may be i have not mentioned on my program to compute the values for minimum and maximum values and update )

Can anyone help me how i can improve my program to work and read the values

Sorry i didnt get you..

The code you posted above contains smileys and italics, because the forum has translated your text, like [i], into tags.

If you post your code between Paste your code here tags, people will be able to read your code as it is intended.

If you go back to the post, click on "modify", highlight the code, then click on the # icon on the toolbar, then click on "save", all should be well.

 for(i=0;i<=10;i++)

Your array has ten elements, not eleven.

double Vp = (3*10^8);

I'm not sure of the relative precedences of the multiplication and XOR operators, but I suspect you mean

double Vp = (3*10E8);

In addition to AWOL's comments -

With these variable definitions:

float VCO;
floatVCO_Final2[10];
float VCO_Final;

your code below does not compile.

for(i=0;i<10;i++)
   VCO_Final2= (5.0*VCO)/1023.0; //Read VCO and store in an array
 
 VCO_Final = VCO_Final2; //take every element of array into another single variable

When reading the comment, the question "why?" remains ...
Is there something with the scope of your [i][b]for( ; ; ) {  }[/b][/i] range, which you intend different than you do ?
What's the purpose of that array ?

What's the reason to include code that is not used ( Cable_fault, calculate )
Trim posted code to the minimum showing your problem, please.

Edit : take care not to have smileys in code

The code doesn't compile because you cut and pasted from the forum, which has interpreted the array subscripts as italic tags, as we've already pointed out.

Thanks, AWOL and CrossRoads, now the code makes more sense and shows a clear problem:

With a globally defined
** ** int i;** **
one can use it even when not appropriate any more.
After the for( ) has finished, i is beyond the range of the array.

VCO_Final = VCO_Final2[i]; // puts some undefined value into VCO_Final
if(VCO_Final < VCO_Final2[i]) // should never be true, after the previous line

After the for( ) has finished, i is beyond the range of the array.

It is beyond the range of the array within the for loop.

double Vp = (3*10E8);

would normally be written

double Vp = 3.0E8;

Or even just "3e8". My preference is to put in the ".0" or else it looks to the eye like a hex number.

Why do you need this

if(VCO_Final>0 && VCO_Final<1) //if VCO lies between 0 and 1, calculate wavelength, distance and others.
 { 
  calculate();
 }
 else if(VCO_Final>1 && VCO_Final<2)
 {
   calculate();
 }
 else if(VCO_Final>2 && VCO_Final<3)
 {
   calculate();
 }
 else if(VCO_Final>3 && VCO_Final<4)
 {
   calculate();
 }
 else if(VCO_Final>4 && VCO_Final<5)
 {
   calculate();
 }
 else if(VCO_Final>5 && VCO_Final<6)
 {
   calculate();
 }
 else if(VCO_Final>6 && VCO_Final<7)
 {
   calculate();
 }
 else if(VCO_Final>7 && VCO_Final<8)
 {
   calculate();
 }
 else if(VCO_Final>8 && VCO_Final<9)
 {
   calculate();
 }
 else
 {
   calculate();
 }

instead of this

  calculate();

And while we are at it: why is "calculate" a separate function? You call it only once and it shares the data with the main loop anyway.

Also you use lots of global variables for no apparent reason. I suggest you cleanup your code a little bit before any further analysis.