For a project I am supposed to create an Inverted pendulum balancing robot, controlled by PID via arduino.

I am using a Mega2560 with the motor shield, and an optical encoder, however I am struggling a little with the code, I dont think I'm too far away, but unfortunately when uploading, nothing happens. I was wondering if anyone could point me where the problem may be? Most of it is assembly of Library codes (Note: I have the encocer, stepper and PID libraries installed, so its not that)

Currently when the code is ran, nothig happens, I have tested the stepper motor components in another sketch and then work, but even when assigning a number for the step (say 200) in this sketch, the stepper does not move.

The only way for this "Serial.println(newPosition);" to change is if the stepper moves, and the only way the stepper is going to move is if "m" changes. Stepper does not move equals no change in encoder, so your result will ALWAYS be 0.

If I where to '//' out the later serial prints, I should be able to read the optical encoder output right (in this case, 'newPosition') and It would show the written value, plus any changes due to the encoder rotation, correct? However currently it gives;

Quote

Basic Encoder Test:-180

However, since this is a single value, that doesnt not change with time or rotation, it suggests something is wrong with the read, or the connections. the encoder has channels A and B plugged into slots 20 and 21, I have changed the code to include the lines, 'int chanA = 20' and the same for channel B, the names on myEnc have been altered correspondingly. Unfortunately the Basic encoder test still returns the same written result.

On the bright side, I seem to be starting to think through this slightly more methodically

EDIT: If I change 'myStepper.step(m)' to a value of 500, the motor does not move, however the same code in another sketch does...

I thought something was wrong with that, however using 'int m - myPID.Compute();' I get an error saying the myPID is not declared in scope, if I put it before this, or 'm' is not declared if I put it afterwards. If i just use 'int m = 0' it says something about 'invalid conversion from 'int' or 'double*'

Input: The variable we're trying to control (double)Output: The variable that will be adjusted by the pid (double)Setpoint: The value we want to Input to maintain (double)Kp, Ki, Kd: Tuning Parameters. these affect how the pid will chage the output. (double>=0)Direction: Either DIRECT or REVERSE. determines which direction the output will move when faced with a given error. DIRECT is most common.

I think it would be best to start over and do tests to see if it works, and again after each additional code.

Okay, i have gone back and and sorted out the stepper motor, this code was a second attempt, and the bits you mentioned I had forgotten to copy over. So now the motor will now step if commanded.

I'm still struggling to input a 'double*' however, I guess the code ' double int = (something) ' is used, but I am unsure what to put as the something. I guess a similar thing will happen for the variable 'newPosition'? to give a variable that has the double format required by the PID? (and hopefully fixing the encoder reader in the process)

I've looked with an oscilloscope, and the encoder changes its values when rotated in manner that I would expect, however unfortunately no change its still not showing in the serial monitor, with it sticking at the initial value (now -512) lke this:

Quote

-512.00Basic Encoder Test-ÿ-512.00-512.00-512.00-512.00-512.00

I'm not sure what the Y'' is, here is the most recent code for good measure.

Try this, comment out "myEnc.write(-512);" and spin the encoder, does it give out readings. Whats happening is, your telling the encoder to be set to a value, and then without any actual movement, its reading back that same value.