Hi, I'm Julen My circuit works good because I can see the signal. But I have implement a code and it does not work, the frequencies are very low and decrease with time.
Do they do anything?
Yes, but the frequencie at the beginning is too high and at the end tends to zero
Please edit your post to add code tags ("</>" button on post editor).
done
Perhaps use Serial.print() to show the actual values used in your calculation. And change the 1000000 to 1000000.0 to ensure the compiler knows it is a float.
Here, you carefully collect values for tempsm and tempsM, then immediately overwrite those with something else. Why?
if (Value < minimum) {
minimum = Value; // store min peak
tempsm=micros();
}
if (Value > maximum) {
maximum = Value; // store max peak
tempsM=micros();
}
tempsm=TEMPSm[s];
tempsM=TEMPSM[s];
still doesnot work
hi, this was the old code, I have actualized it
Did you actually print the values being used?
sorry, but i do not understand your question
Did you print the actual values of the variables being used in your calculation so you can verify they are what you expect? If they are not what is expected, then back track through the code to see where they went wrong.
Sort of stupid to post the wrong code, don't you think?
Sorry! it is posted the "right" code now
If the value is not stable enough you can add filtering.
int MaxInputValue = 0;
int MinInputValue = 1024;
boolean WasAboveLimit = false;
unsigned long PreviousRisingEdgeTime = 0;
float PulseFrequency;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int vActual1 = analogRead(A0); // Leer seƱal
if (vActual1 > MaxInputValue)
MaxInputValue = vActual1;
if (vActual1 < MinInputValue)
MinInputValue = vActual1;
// Is it above the midpoint of the lowest and highest readings?
boolean aboveLimit = vActual1 > ((MaxInputValue + MinInputValue) / 2);
// Turn on the built-in LED when the input is high.
digitalWrite(LED_BUILTIN, aboveLimit);
// If the value has crossed the limit...
if (aboveLimit != WasAboveLimit)
{
WasAboveLimit = aboveLimit;
if (aboveLimit)
{
// Rising edge
unsigned long risingEdgeTime = micros();
unsigned long pulseDuration = risingEdgeTime - PreviousRisingEdgeTime;
PreviousRisingEdgeTime = risingEdgeTime;
PulseFrequency = 1000000.0 / pulseDuration;
}
}
// Report the value periodically
unsigned long currentMillis = millis();
static unsigned long lastPrintMillis = 0;
const unsigned long printInterval = 5000;
if (currentMillis - lastPrintMillis >= printInterval)
{
lastPrintMillis = currentMillis;
Serial.print("Valor de la frecuencia: ");
Serial.print(PulseFrequency, 4);
Serial.println(" HZ");
}
}
You could expand the code to make it a little clearer:
// Midpoint of the lowest and highest readings
int middleOfRange = ((MaxInputValue + MinInputValue) / 2);
// Is it above the midpoint?
boolean aboveLimit = vActual1 > middleOfRange;
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.