Code Runs, But Stops....?

Hi Guys,

I am currently working on a PID-control to regulate DC motors for my self-balancing robot. Basically this code will go through a setup process at the beginning and calculate the voltage bias for the accelometer. Then it will loop through and read the current angle and subtract it from the bias and go to a while loop. From here it will check if the angle is at the setpoint (0 degrees) if not it will go through these processes to re-read the angle, run through the PID control to spit a PWM value, and then tell the motors to move back or forward.

The problem I have here is that the program uploads fine, but can’t seem to get halfway down the entire program… I don’t know whether it defined a statement wrong or used the PID or motorshield incorrectly.

The code is below. Thanks!

Balancing_Attempt_2.ino (9.4 KB)

In setup(), you read the value from the accelerometer into the Accel_X variable.

Accel_X = analogRead(xpin); //Read values from X-AXIS port i.e. A5 --> Redfine Accel_X to value obtained

But in the loop, you just assign the pin number to Accel_X. Then you try to use that pin number as if it is an acceleration value. It's not. It's just a pin number. Maybe you meant to read the sensor again?

Accel_X = xpin; //Read values from A0, convert from bits->volts using conversion factor (5/1024), define them to accel_x

The comment included there would seem to indicate that there should be a lot more going on in that line.

Hi Delta,

It appears that I made an error by not putting analogRead(xpin) again. That line is meant to read the sensor once more within the while loop. But the code still appears to stop half way through execution.

Replace the motors with LEDs. If you still get the problem then it is the code. If however you do not then the problem is with the motors generating noise interfering with the Arduino.