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;
}
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
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.
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