Why doesn't my calculation work?

I have connected a potentiometer to my Arduino and convert the values ​​into a scale from 0 to 100:

 // Variablen für Potentiometer int Potentiometerwert = 0; int Potentiometerminimum = 125; float Potentiometerproz = 0; int Potentiometeranz = 0; int Potentiometermax = 896 - Potentiometerminimum; int Potentiometerdifferenz = 0; int Gradkorrektur = 0; //Potiprozent berechnen Potentiometerwert = analogRead(A6); Potentiometerdifferenz = Potentiometerwert - Potentiometerminimum; Potentiometerproz = 100. / Potentiometermax * Potentiometerdifferenz; Potentiometeranz = 100. - (int)Potentiometerproz; Gradkorrektur = (int)Potentiometeranz - 50.;

This works fine up to the second-to-last line – but when I subtract 50 from the measured value to get into the negative range, it only gives me house numbers between -50 and 5024.

Background – the potentiometer is intended to be used to correct a southerly orientation. The sensor (magnetometer) delivers values ​​between -179° and 180° and is relatively inaccurate. If I want to correct below 0, I need to be able to set a value on the potentiometer that is then converted to a negative number – so the center position should deliver 0.

Can anyone tell me where the error is?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MisterCallidus
9 months ago

Hello!

The code had the problem that the use of int and float variables led to precision loss and wrong calculations. By changing degrees correction to float, precision is maintained. The use of 100.0 instead of 100 ensures that the division is treated as a float division. As a result, the values are calculated correctly, and degree correction provides the expected results.

Corrected version of your code:

// Variables for potentiometer

int potentiometer value = 0;

int potentiometer minimum = 125;

potentiometer proz = 0;

int potentiometer value = 0;

int Potentiometermax = 896 – Potentiometerminimum;

int potentiometer difference = 0;

float degree correction = 0; // Change this to float

// Calculate potency percentages

Potentiometer value = analogRead(A6);

potentiometer difference = potentiometer value – potentiometer minimum;

Potentiometerproz = 100.0 / Potentiometermax * Potentiometer differential; // Use 100.0 for float Division

Potentiometeranz = 100 – Potentiometerproz; // Lass Potentiometerproz as float

Grade correction = potentiometerance – 50.0; // Use float subtraction

// Review issue

Serial.print(“Potentiometer value: “);

Serial.println(Potentiometer value);

Serial.print(“Potentiometer Difference: “);

Serial.println(Potentiometer difference);

Serial.print(“Potentiometerproz: “);

Serial.println(Potentiometerproz);

Serial.print(“Potentiometeranz: “);

Serial.println(Potentiometeranz);

Serial.print(“degree correction: “);

Serial.println(degree correction);

Mr.Callidus

MisterCallidus
9 months ago
Reply to  stealthuser

Very happy! 😊