Go Down

#### sajidkaleem

##### Aug 22, 2012, 12:47 pmLast Edit: Aug 22, 2012, 05:02 pm by CrossRoads Reason: 1
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: [Select]
`#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;}`

#### dxw00d

#1
##### Aug 22, 2012, 12:56 pm
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

#### sajidkaleem

#2
##### Aug 22, 2012, 01:12 pm
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

#### AWOL

#3
##### Aug 22, 2012, 01:27 pmLast Edit: Aug 22, 2012, 05:46 pm by AWOL Reason: 1
Quote
Sorry i didnt get you..

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

If you post your code between [font=Verdana][[/font]code] Paste your code here [font=Verdana][[/font]/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: [Select]
` for(i=0;i<=10;i++)`
Your array has ten elements, not eleven.

Code: [Select]
`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: [Select]
`double Vp = (3*10E8);`
"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.
I speak for myself, not Arduino.

#### michael_x

#4
##### Aug 22, 2012, 03:31 pmLast Edit: Aug 22, 2012, 04:38 pm by michael_x Reason: 1

With these variable definitions:
Code: [Select]
`float VCO;floatVCO_Final2[10];float VCO_Final;`your code below does not compile.
Code: [Select]
`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 )

Edit : take care not to have smileys in code

#### AWOL

#5
##### Aug 22, 2012, 04:58 pm
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.
"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.
I speak for myself, not Arduino.

#### michael_x

#6
##### Aug 22, 2012, 06:01 pm
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

#### AWOL

#7
##### Aug 22, 2012, 06:57 pm
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.
"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.
I speak for myself, not Arduino.

#### gardner

#8
##### Aug 22, 2012, 07:01 pm
Code: [Select]
`double Vp = (3*10E8);`

would normally be written

Code: [Select]
`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.

#### udoklein

#9
##### Aug 22, 2012, 07:21 pm
Why do you need this

Code: [Select]
`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(); }`

`  calculate();`