Bonjour,
Alors perso je rigole bien, mais en vrai c'est pas drôle. J'utilses un capteur de luminosité Celui-ci , j'ai borné ses résultats entre 200 et 800 et je les convertis en 0 255 afin d'allumer une led avec la même intensité. La carte est une arduino Uno.
Afin de savoir le pourcentage de luminosité compris dans la fourchette dédiée, je fais un bete et simple produit en crois en utilisant la valeur de sortie du capteur *100 et /800. Sauf que le résultat est juste totalement abérant ! En affichant les résultats, par exemple le capteur me dis 631, le calcul vaut 0 et la valeur du capteur *100 vaut -2351.
Je veux bien dire que je suis nul en math mais là quand même ahah
Voilà le code, le calcul se trouve dans la dernière boucle tout à la fin "convert()"
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
float rawRange = 1024; // 3.3v
float logRange = 5.0;
float ValConvert=0;
int ValConvert1=0;
void setup() {
// initialize serial communications at 9600 bps:
analogReference(EXTERNAL); //
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
sensorValue = constrain(sensorValue, 200, 800);
outputValue = map(sensorValue, 200, 800, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
//Serial.print("Sensor = " );
//Serial.print(sensorValue);
//Serial.print(" Lux = ");
//Serial.print(RawToLux(sensorValue));
//Serial.print(" Output = ");
//Serial.print(outputValue);
//Serial.print(" Val convert = ");
Serial.print(convert(sensorValue));
Serial.println(" ");
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(1500);
}
float RawToLux(int raw)
{
float logLux = raw * logRange / rawRange;
return pow(10, logLux);
}
float convert(int sensorValue)
{ Serial.print(" Val sensor = ");
Serial.print(sensorValue);
Serial.print(" ");
Serial.print(ValConvert1);
Serial.print(" ");
ValConvert1= sensorValue * 100;
ValConvert=ValConvert1/800;
return ValConvert;
}