Hey guys, Its my first post, so be nice
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)
#include <PID_v1.h>
#include <Encoder.h>
#include <Stepper.h>
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int STEPS = 200;
const int m = 0;
const int newPosition = 0;
Stepper myStepper(STEPS, dirA, dirB);
Encoder myEnc(20, 21);
PID myPID(newPosition, m, 0, 1, 20, 100, 1);
void setup() {
myStepper.setSpeed(90);
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
myEnc.write(-180);
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
myPID.Compute();
myStepper.step(m);
Serial.println(m);
delay (1);
}
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.
Since a picture speaks a thousand words, I have included one below
Thanks for your help, Yew
Moderator edit: Quote tags swapped for code tags