Hi guys!
I want to get the highest value from a sensor reading.
From example, the "x" sensor returns a lot of values, when it sensor returns 200 values i need to get the highest value from this 200 values.
I hope i explained myself well. xD
Hi guys!
I want to get the highest value from a sensor reading.
From example, the "x" sensor returns a lot of values, when it sensor returns 200 values i need to get the highest value from this 200 values.
I hope i explained myself well. xD
Show us what you have tried so far.
People are pretty helpful on here, but you will get a much better response if you show that you have at least tried.
Post any code in code tags </>.
What is the data type, char, byte, int, long, float?
We can come up with many sketches, it depends on your project what the best method is.
Nevertheless, I made this example:
int counter; // count 200 samples
int minValue;
int maxValue;
void setup()
{
Serial.begin(115200);
Serial.println( "The sketch has started");
counter = 0; // start at the begin
minValue = maxValue = SensorValue(); // give them a value from the sensor to start with
}
void loop()
{
int newValue = SensorValue();
if( newValue > maxValue)
{
maxValue = newValue;
Serial.print( "▲"); // this is a UTF-8 character
}
else if( newValue < minValue)
{
minValue = newValue;
Serial.print( "▼"); // this is a UTF-8 character
}
else
{
Serial.print( "─"); // this is a UTF-8 character
}
counter++;
if( counter == 200)
{
Serial.println();
Serial.print( "Minimum value: ");
Serial.print( minValue);
Serial.print( ", ");
Serial.print( "Maximum value: ");
Serial.println( maxValue);
counter = 0; // start at the begin
minValue = maxValue = SensorValue(); // give them a value from the sensor to start with
}
delay( 50);
}
int SensorValue()
{
// This is no fun:
// return random( -20000, +20001);
// This is more fun:
return( random( -32, 32) * random( -32, 32) * random( -32, 32));
}
This sketch in Wokwi:
sorry bro, was just speculating. I had nothing done, just the idea. But this is how i did it:
//ASC712 Nos da 2.5v para 0A
//La sencibilidad proporcionada por el fabricante varia dependiendo
//de la capacidad del sensor
//float Sensibilidad = .185; //Sensicibilidad en Voltios/Amperio para sensor de 5A
float Sensibilidad = .100; //Sensicibilidad en Voltios/Amperio para sensor de 20A
//float Sensibilidad = .066; //Sensicibilidad en Voltios/Amperio para sensor de 30A
void setup()
{
Serial.begin(9600);
}
void loop()
{
float I = obtenerI(200);
Serial.print("Corriente: ");
Serial.println(I,3);
//delay(100);
}
float obtenerI(int numMuestras)
{
float voltajeSensor;
float valorMax = 0;
float corriente;
for(int i = 0; i < numMuestras; i++)
{
voltajeSensor = analogRead(A0)*(5.0 / 1023.0); //Leemos el sensor y lo convertimos a voltaje
if (voltajeSensor > valorMax){
valorMax = voltajeSensor;
}
}
corriente = (valorMax - 2.5)/Sensibilidad;
return(corriente);
}
Thx Koepel!
Ur code has better ideas than mine! I gonna include them to improve it!
The initial value is important.
This is okay, start with the lowest possible value.
float valorMax = 0; // initial value, lowest possible value
A float number is almost never that number, since floating point numbers are approximations. For a 'float' value I prefer to set a value lower than the lowest possible value.
Assign a value that never happens, to know if something went wrong if that value has not changed. For example when reading a temperature.
float temperatureMax = -1000.0; // initial value, this temperature will never happen
The alternative is to get a real value from the sensor as initial value. That is for sure a good value. That is what I did in my example.
I hadnt thought of it that way, you are absolutely right. Thanks for everything!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.