Issue with rotation sensor not updating

Hi, im having an issue with my rotation sensor. When printing to the console, it just prints the first value recorded at the start, and even if i physically move the lift really does not update, except maybe it randomly it increases 10 seconds after i move the lift.
Here is my code:

first 2 lines are the important ones really.

i have no idea why. when looking at the built in statistics on the lcd, the rotation sensor ouputs the same first angle as the one in the code, however it changes too when moving the lift.

Sensors are fun! Until they don’t work…

It appears that the problem with the code is that you’re redeclaring the variable LiftMotorPosition repeatedly inside the while loop. This is very bad practice and can cause the code to seemingly behave randomly.

To solve the issue, you should put
‘’’int LiftMotorPosition = 0;’’’
Before the while loop, and then remove the ‘int’ keyword from your assignment operation at the start of the while loop.

Also, another best practice thing in c++ is to use lower camel case for variable names, so it would be better to use ‘liftMotorPosition’ instead of ‘LiftMotorPosition’.

TL;DR: don’t declare variables inside a while loop.

not true.

use of camel case, snake case etc. is completely up to the programmer unless some coding style guidelines are required by a specific project or company.

5 Likes