I am changing the phase of signal from 0 to 360 by each degree to get max voltage value.Because if i change phase of the signal the voltage also changes.I have the fallowing code to find max value. and corresponding phase

void Maxphase(float *max, unsigned int *index) 
    {
    *max = 0.0;
    float value;
    unsigned int i, data;
    for (i=0;i<=360;i++) 
    {              
        phaseset(i); 
        delay_ms(100);
        data = readvalue(); 
        value = voltage(mux1);    
        if(value > *max)   //find max value 
        { 
            *max = value;    //max voltage 
            *index = i;   
        }  
    }                           
    }

from the above code I am getting Max value(voltage) after 38 sec(360*100) because for every read operation my device needs 100ms delay. This is too large, I can't change hardware thus i want to get the max value within 2 to 3 sec by optimizing software.
then I have tried with the fallowing code.

 void Maxphase(float *max1, unsigned int *index1) 
    {
      max = 0.0;
      float value;
      unsigned int i,j,data;
      for (i=0;i<360;i+=10) 
       {              
        phaseset(i); 
        delay_ms(100);
        data = readvalue(); 
        value = voltage(mux1);    
        if(value > max)   //find max value 
        { 
            max = value;    //max voltage 
            index = i;   
        }  
       }    
       *max1=max;
       *index1=index;
       for (i=*index1-9;i<=*index1+9;i+=1) 
         {       
         j=i;       
        phaseset(j); 
        delay_ms(100);
        data = readvalue(); 
        value = voltage(mux1);    
        if(value > *max1)   //find max value 
        { 
            *max1 = value;    //max voltage 
            *index1 = i;   
        }  
        }                         
    }

I have reduced time from 45 sec to 7 sec. i have reduced iterations 360 to 54(54*100). I want to reduce it 7 sec to 2 sec.

Can any one help me with better program that i can get max value from (0 to 360) with in 2 to 3 sec.

I have measured the voltage values using scope by changing phase. I have written below how it vary voltage with phase.

Phase (degree)     voltage(max)
  0             0.9mv

 45             9.5mv

 90             9.0mv

135             0.9mv

180             292mv

225             601mv

270             555mv

315             230mv

360             0.9mv

I am new to C programming. Can anyone provide sample code.

This is a lot like a binary search.
Looking at this, you can already tell where to look after 4 readings.
So drop it to 4 readings: 0,90,180,270 (you already know 0 and 360 are the same.)
Find your two highest.
Pick exactly in the middle.
Take a reading.
Repeat.

For sanity's sake, you may want to start with an initial reading of 8 values.

can you provide a smaple code, if possible.

I apologize that I haven't gotten this to you yet. I got halfway through then realized the data (being somewhat symmetrical) can be misleading. I'm still working on it. I'll test it Saturday and Sunday.

Happy coding!

Dean, he's posted this thread on at least 3 programming boards, and received code and the most detailed software and hardware discussion on another board.

He's had at least 4 algorithms and several snippets of code given to him, already. He wants code - and he's been given code, but doesn't want to work with it, apparently.

If the voltage at the phase angles were to stay contant for the angle, then it becomes a simple matter of testing some values, sorting the values you recieve, and a binary search would be very fast look up to find the perfect phase angle.

But that's not the case. He needs minimum testing, since he needs the answer within 2 seconds, and each test takes 100 milliseconds. So the question becomes, what's the stability of voltage to phase angle, over time? Is it consistent, or does it vary? And by how much?

He's been advised to have the tester changed to continuous testing mode, which is much faster, after looking at the data for the tester he has. He says the hardware "guy" is reluctant or unwilling to make that change.

Without consistency info, you can't design the absolute best algorithm for the software, but he's been given several excellent one's, including code for some of them.

Go to CBoard C forum, for that discussion. I wouldn't spend more time on it, although the problem is intriguing.

But that's not the case. He needs minimum testing, since he needs the answer within 2 seconds, and each test takes 100 milliseconds. So the question becomes, what's the stability of voltage to phase angle, over time? Is it consistent, or does it vary? And by how much?

I am generating the signal and giving it as input to sensor and taking sensor output.I am selecting both the signals by using MUX. The ouput signal is phase shifted by some degree to the input. I am using the capacitive sensor so the phase shift depends on the medium of capacitor with respect to time. The phase shift will change if medium chamges for example Air or water or some other medium pass through the capacitor then Phase will change otherwise it will be constant. I want to measure that phase diffrence between two signals at every 5 sec, as well as voltages.

But the question remains :"Is there a consistent relationship between phase angle and voltage?", because these other things you have mentioned, are not all relevant to creating a best software algorithm.

I agree completely that hardware can do all this a lot faster, but that might be expensive and may not even be practical, I'm not sure. For a good software algorithm, we need more of the relavent info.

In your hardware, what is the relationship between the phase angle and voltage? Does it vary? By how much?

If there is a good relationship between phase angle and voltage consistently, then which is easier to read for you? If it's consistent, we can get data from several runs, and see clearly what is the best setting.

Otherwise, we are making suggestions that are sub optimal.

@Adak: Much obliged. I'd been worrying about that.

Sorry for the late reply,
I have been out of internet from last one month, I have taken holidays.

The thing here is voltage will vary with phase angle. when I vary from 0 to 360 degree the voltage will start from 0 to 2V but i don't know at what phase angle it will start from zero. Sometimes it may be 0 degree or 30 degree what ever. but if you take readings from 0 to 360 degrees and if you take voltage values then plot a graph, you will see sine wave.

I am trying with software but if you have any hardware solutions then i am so happy.

The concept is I am generating one sine signal with ATmega32A controller with the help of AD9833 wave form generator. I am sending that signal through a cylindrical capacitor. I am taking output from capacitive sensor. The voltage and phase of the output signal will depends on the medium between capacitive electrodes.

So i need to caluculate phase shift between two signals input and output and as well as amplitudes of both signals then only i can caluculate permitivity of the medium. Do you have any ideas to caluculate permitivity of the medium? If you need more information then ask me please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.