Time Read Arduino

Hello, I have a doubt that is:

Through a signal generator inject a syn wave (2pi * f) at pin A0 Arduino.

  • I do what analogRead (A0); Then convert to Volt through reason (5/1023) * analogRead (A0);

  • The Arduino reads for example the following values:

0 1 2 4 6 7 8

My point is that the Arduino reads the values of the sinusoid but that time?

The value 2 corresponds to the time instant? do not know!

How do I know the value that each time corresponding amplitude read by Arduino?

ricperes:
Hello, I have a doubt that is:

Through a signal generator inject a syn wave (2pi * f) at pin A0 Arduino.

  • I do what analogRead (A0); Then convert to Volt through reason (5/1023) * analogRead (A0);

  • The Arduino reads for example the following values:

0 1 2 4 6 7 8

My point is that the Arduino reads the values of the sinusoid but that time?

The value 2 corresponds to the time instant? do not know!

How do I know the value that each time corresponding amplitude read by Arduino?

what is the voltage range of the sin wave 0..5v? IF so then an equation that replicates this would be

v=sin(angle)*2.5+2.5

sin(angle)=(v-2.5)/2.5
angle = asin((v-2.5)/2.5)

So angle is between -pi/2 and pi/2 the only way to identify quadrant 2 or quadrant 3 values is by inspection. if you know the sine wave is flowing 0, pi/2, pi, 3pi/2, 2pi you could compare current value of angle to prior value. If prior value was in quadrant 1, and angle was decreasing then angle = pi -angle.
if prior value was in quadrant 2 and and angle < 0 then angle = pi - angle. if

#include <Math.h>

static double prior=0;
/* this function only works if called with voltage values faster than f*4 */

double getPhaseAngle( double voltage){ // 0.. 2pi
double angle=asin((voltage-2.5)/2.5);
if(angle>0){
  if(angle>=prior){// still quadrant 1
    raw=angle;
    quadrant = 1;
    prior=angle;
    }
  else { // quadrant 2
    raw=angle;
    quadrant = 2;
    prior=angle;
    angle = pi - angle;
    }
  }
else {
  if(angle<=prior) { // 
    raw=angle;
    quadrant = 3;
    prior = angle;
    angle = pi - angle; // minus a negative equals a plus
    }
  else {// moving through quadrant 4
    raw=angle;
    quadrant = 4;
    prior = angle;
    angle = 2*pi + angle;
    }
  }
return angle;
}

chuck.

chucktodd:
what is the voltage range of the sin wave 0..5v? IF so then an equation that replicates this would be

v=sin(angle)*2.5+2.5

sin(angle)=(v-2.5)/2.5
angle = asin((v-2.5)/2.5)

So angle is between -pi/2 and pi/2 the only way to identify quadrant 2 or quadrant 3 values is by inspection. if you know the sine wave is flowing 0, pi/2, pi, 3pi/2, 2pi you could compare current value of angle to prior value. If prior value was in quadrant 1, and angle was decreasing then angle = pi -angle.
if prior value was in quadrant 2 and and angle < 0 then angle = pi - angle. if

#include <Math.h>

static double prior=0;
/* this function only works if called with voltage values faster than f*4 */

double getPhaseAngle( double voltage){ // 0.. 2pi
double angle=asin((voltage-2.5)/2.5);
if(angle>0){
  if(angle>=prior){// still quadrant 1
    raw=angle;
    quadrant = 1;
    prior=angle;
    }
  else { // quadrant 2
    raw=angle;
    quadrant = 2;
    prior=angle;
    angle = pi - angle;
    }
  }
else {
  if(angle<=prior) { //
    raw=angle;
    quadrant = 3;
    prior = angle;
    angle = pi - angle; // minus a negative equals a plus
    }
  else {// moving through quadrant 4
    raw=angle;
    quadrant = 4;
    prior = angle;
    angle = 2*pi + angle;
    }
  }
return angle;
}




chuck.

Yes, i Sum 2.5V DC because arduino don't read negative values.

Sorry , my doubts but, i don't understand , i want to read a sinusoidal wave, and know like this:

For example:

Read : 2 0.000001 s
Read: 3 0.000002 s
. . . .
. ..

I want to know the time that arduino read value, because arduino do it automatically.

ricperes:
Yes, i Sum 2.5V DC because arduino don't read negative values.

Sorry , my doubts but, i don't understand , i want to read a sinusoidal wave, and know like this:

For example:

Read : 2 0.000001 s
Read: 3 0.000002 s
. . . .
. ..

I want to know the time that arduino read value, because arduino do it automatically.

The arduino has a free running timer millis() that counts milliseconds in a unsigned long value
so if you use this code:

unsigned long sampleTime = millis();
unsigned long t;
unsigned int sampleVoltage = analogRead(A0);
//Serial.print("Voltage=");
Serial.print(float(sampleVoltage)*5.0/1024.0,2);
/* uncomment this section to have full time since boot displayed 

 Serial.print(" day=");
Serial.print(sampleTime/(1000L*60*60*24));
sampleTime %=(1000L*60*60*24);
Serial.print(" time=");
t= sampleTime/(1000L*60*60);
if(t<10)Serial.print('0');
Serial.print(t);
Serial.print(":");
sampleTime %=(1000L*60*60);
t= sampleTime/(1000L*60);
if(t<10)Serial.print('0');
Serial.print(t);
sampleTime %=(1000L*60);
Serial.print(":");
t= sampleTime/(1000L);
if(t<10)Serial.print('0');
Serial.print(t);
sampleTime %=1000;
t=sampleTime;
*/
t= sampleTime % (1000L);
Serial.print(" .");
if(t<10)Serial.print("00");
else if(t<100) Serial.print("0");
Serial.println(t);

Chuck

It takes the Arduino about 0.1mS for the Arduino not the 1uS in your example.
While you can take a time stamp of when your reading it will not be synchronised to your waveform. You can only synchronise it by referencing it to something like a zero crossing which is best done by using a comparator to generate an interrupt. Then have the interrupt service routine capture the millis time as a referance.

Hi,
What is it that you want to do?
Measure the period / frequency of the sinewave?
Measure the amplitude of the sinewave?

Tom... :slight_smile:

TomGeorge:
Hi,
What is it that you want to do?
Measure the period / frequency of the sinewave?
Measure the amplitude of the sinewave?

Tom... :slight_smile:

Hello Tom, I measure a amplitude of the sinewave but i want to know in that this measuring time is made.
Do you undestand me?

thanks

Do you undestand me?

Well if I have not given you the answer then no I don't understand you.

Hi,

Do you want to know the time BETWEEN samples?

OR do you want to know the period/frequency of the sinewave?

WHICH ONE or NIETHER?

What is the application of your sketch, what is the final function of your project?

Tom... :slight_smile:

TomGeorge:
Hi,

Do you want to know the time BETWEEN samples?

OR do you want to know the period/frequency of the sinewave?

WHICH ONE or NIETHER?

What is the application of your sketch, what is the final function of your project?

Tom... :slight_smile:

this is for a project, eletronic and programming also.

I want to know the time between samples,ie the time instant that each amplitude value corresponds,
not the time it takes to make a reading.

What is wrong with answer #4?
Do you not understand it?
If so ask.

Grumpy_Mike:
What is wrong with answer #4?
Do you not understand it?
If so ask.

Sorry , I don't understand, can you show me a code to explain me that please?

thanks

can you show me a code to explain me that please?

No the way this forum works is you write the code and we can help you with it. I have already told you what the code should be, so you have a go at writing it. Then test it and post your code and the results.

Grumpy_Mike:
No the way this forum works is you write the code and we can help you with it. I have already told you what the code should be, so you have a go at writing it. Then test it and post your code and the results.

ok, thanks anyway

ricperes:
ok, thanks anyway

Please do my homework for me.

chuck.