Pages: [1]   Go Down
Author Topic: Read ADC sine wave values  (Read 1241 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Code:
#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
« Last Edit: August 22, 2012, 10:02:58 am by CrossRoads » Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 [code] Paste your code here [/code] 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.

Code:
for(i=0;i<=10;i++)
Your array has ten elements, not eleven.

Code:
double Vp = (3*10^8);
I'm not sure of the relative precedences of the multiplication and XOR operators, but I suspect you mean
Code:
double Vp = (3*10E8);
« Last Edit: August 22, 2012, 10:46:43 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Online Online
Faraday Member
**
Karma: 56
Posts: 2974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In addition to AWOL's comments -

 With these variable definitions:
Code:
float VCO;
floatVCO_Final2[10];
float VCO_Final;
your code below does not compile.
Code:
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 for( ; ; ) {  } 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
« Last Edit: August 22, 2012, 09:38:10 am by michael_x » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Online Online
Faraday Member
**
Karma: 56
Posts: 2974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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; // puts some undefined value into VCO_Final
if(VCO_Final < VCO_Final2)   // should never be true, after the previous line
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
Code:
double Vp = (3*10E8);

would normally be written

Code:
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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you need this

Code:
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

Code:
  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.
Logged

Check out my experiments http://blog.blinkenlight.net

Pages: [1]   Go Up
Jump to: