PID code puzzle on a Micro

Am I trying to do the impossible here? Checking the MSB ( bit 15) of an integer to set a boolean - the compiler gets very upset and generates a huge amount of verbiage.
"X_control" is generated correctly from X_pro, and I am trying to separate out magnitude and direction for a coil drive current in the last half dozen lines.

` void X_update(){
X_pro = (err_X) * (X_pro_gain) ;
X_dif = (err_X - old_X) * (X_dif_gain) ;
X_int = (X_int) + (err_X * X_int_gain) ;
old_X = err_X ;
X_control = (X_pro) + (X_int) + (X_dif);

if (X_control)bit(15) = true
  X_positive = false  ;                // Save control demand polarity
  else   X_positive = true ; 
 
X_control = abs(X_control);            // Process further to generate PWM to coil driver

`

assuming "X_control" is a signed value, why are not simply do:

if X_control< 0
X_positive = false ; // Save control demand polarity
else X_positive = true ;

??

OMG - is it really that simple!!

Thanks - but why doesn't my code work? It seemed to be logical, but I'm still very much feeling my way with Arduino coding.

In your if there is 1 "=" only, should be 2 "==".
Try this way:

if (bitRead(X_control,15) == true)

RV mineirin

Tried this and the compiler responded with :- ......... error: expected '(' before 'X_control'

Bingo!! This did the trick.

Many thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.