Can you help about my code is wrong

Hey Guy :~
I try for create measure period code of sine wave.

But It's wrong.
This is my code:

/////////////////////////////////////////////////////////////////////

unsigned int PulseRate=0;
unsigned int PulseRef=0;
unsigned long TimeHR=0;
unsigned long Time_HR_Ref=0;
unsigned int k=0; 
unsigned int i=0;
unsigned int sensorPin = A0;
void setup(void)
{  
  Serial.begin(9600);
}
void loop(void)
{
  HR();
}
void HR(void)
{
    PulseRate=analogRead(sensorPin);
    if(PulseRate>PulseRef)
    {
      PulseRef=PulseRate;
      if(k==1)
      {
        k=2;
      }
    }
    if(PulseRate==PulseRef)
    {
      PulseRate=analogRead(sensorPin);
    }  
    if(PulseRate<PulseRef)
    {
      PulseRef=PulseRate;
      if(k==0)
      {
         Time_HR_Ref=millis();
         k=1;
      }
      if(k==2)
      {
        TimeHR=millis()-Time_HR_Ref;
        Serial.print(TimeHR);
        Serial.print("\n");
        k=0;
      }
    }
}

//////////////////////////////////////////////////////////
Picture is sine wave at I want measure.
if someone have code please sent code to me.
(gonggangza@hotmail.com)
Thx :smiley: :smiley: :smiley:

But It's wrong.

Not helpful.

Please use code tags when posting code.

AWOL:

But It's wrong.

Not helpful.

Please use code tags when posting code.

This is first post.
Sorry

You still haven't said what is "wrong"

AWOL:
You still haven't said what is "wrong"

This code can not measure period of sine wave.
This code can print value but value not true. When I compare with oscilloscrope.
:~

Well to expect anybody to reverse engineer your comment-less code is a bit much.

Maybe you should explain your logic. What's the input? What are those "k" factors?

    PulseRate=analogRead(sensorPin);

You seem to have a fundamental misunderstanding of what analogRead() does. It returns a value that, at that point in time, represents a point on the curve (the positive portion, anyway). That does not tell you anything AT ALL about the rate that curve is doing anything.

JimboZA:
Well to expect anybody to reverse engineer your comment-less code is a bit much.

Maybe you should explain your logic. What's the input? What are those "k" factors?

I want measure time between max value to max value of sine wave.

unsigned int PulseRate=0;
unsigned int PulseRef=0;
unsigned long TimeHR=0;
unsigned long Time_HR_Ref=0;
unsigned int k=0; 
unsigned int i=0;
unsigned int sensorPin = A0;
void setup(void)
{  
  Serial.begin(9600);
}
void loop(void)
{
  HR();
}
void HR(void)
{
    PulseRate=analogRead(sensorPin);// Read Analog In put A0
    if(PulseRate>PulseRef)//This function is check uptrend
    {
      PulseRef=PulseRate;//memory old value for referent
      if(k==1)//When sine wave uptrend after Vmax first. So K=2 for check up trend between Vmax first and Vmax Second.
    }
    if(PulseRate<PulseRef)//This function is check downtrend
    {
      PulseRef=PulseRate;//memory old value for referent
      if(k==0)//K=0 factor for check. When sine wave downtrend first (This is Vmax first)
      {
         Time_HR_Ref=millis();//memory time Vmax first
         k=1;
      }
      if(k==2)//K=2 factor for check. When sine wave downtrend second (This is Vmax second)
      {
        TimeHR=millis()-Time_HR_Ref;//calculate period time of( Vmax second -Vmax first=period of signal )
        Serial.print(TimeHR);//Print value show to computer for check
        Serial.print("\n");
        k=0;
      }
    }
}

I not good English But I want create code.
Thx everyone for helpme :slight_smile:

PaulS:

    PulseRate=analogRead(sensorPin);

You seem to have a fundamental misunderstanding of what analogRead() does. It returns a value that, at that point in time, represents a point on the curve (the positive portion, anyway). That does not tell you anything AT ALL about the rate that curve is doing anything.

Thank Pauls.
I know function of analogRead() for read Voltage in put. I just want know Vmax from analogRead()
I want know look like picture down.

sine2.png

I just want know Vmax from analogRead()

Then you should be using variables with names that make sense. PulseRate, the way you are using it, does not.

Get a value AND the time that you read that value:

int analogValue - analogRead(sensorPin);
unsigned long signalReadTime = micros();

See if analogValue is higher than a previous value. If so, copy the current value and time to the max value and time variables. If the value is less than some previous value, copy the current value and time to the min value and time variables. When you've found a min and a max, you know when they occurred, so you can determine the frequency.

Keep in mind that reading at the exact moment when the curve is at a peak (min or max) is very unlikely.

Also, keep in mind that feeding negative values into the Arduino is not good for its long term health.

PaulS:

I just want know Vmax from analogRead()

Then you should be using variables with names that make sense. PulseRate, the way you are using it, does not.

Get a value AND the time that you read that value:

int analogValue - analogRead(sensorPin);

unsigned long signalReadTime = micros();




See if analogValue is higher than a previous value. If so, copy the current value and time to the max value and time variables. If the value is less than some previous value, copy the current value and time to the min value and time variables. When you've found a min and a max, you know when they occurred, so you can determine the frequency.

Keep in mind that reading at the exact moment when the curve is at a peak (min or max) is very unlikely.

Also, keep in mind that feeding negative values into the Arduino is not good for its long term health.

Sorry about variables parameter.
Do you have example code for learning?
I can't create code. =(

I can't create code.

Then, it doesn't seem like this is the project for you.