Hello I am trying to make motors turn a certain amount of degrees,
I have some questions regarding how to make this happen:
-
Is it absolutely necessary to use PID, because I have almost 0 programming experience.
-
I managed to find some code and adjust it to where I can get a single motor to display how much it turned based on encoder ticks, where: my motors has 48CPR and 34 Gear ratio, so it's ~4.5 tics/degree.
-
Could I specify a degree range to make the motor stop where I could simply do something like:
--->Motor 1 speed =100%,
if motor is at {pulses' equivalence of 1.5-1.6 turns},
then stop/decelerate?
3.1. If that is possible, how would that command look? -
Is it possible to use a single MEGA board to count the turns of 4 different motors so they can move independently?
This is the code I have so far, not my code, I just found and changed it a bit so it isn't limited to 360 degree turns and is based on my encoder and motor.
Also my motor driver has only 1 direction pin, when it's high motor will go say CW and low it'l go CCW
int pulses;
int deg = 0;
int encoderA = 2;
int encoderB = 3;
int pulsesChanged = 0;
void setup(){
Serial.begin(115200);
Serial.println("Degrees turned");
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
attachInterrupt(0, A_CHANGE, CHANGE);
attachInterrupt(1, B_CHANGE, CHANGE);
}//setup
void loop(){
if (pulsesChanged != 0) {
pulsesChanged = 0;
Serial.println(pulses*0.22059);
}
}
void A_CHANGE(){
if( digitalRead(encoderB) == 0 ) {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is low
pulses--; // moving reverse
} else {
// A rose, B is low
pulses++; // moving forward
}
} else {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is high
pulses++; // moving forward
} else {
// A rose, B is high
pulses--; // moving reverse
}
}
// Make sure the pulses are between 0 and 359
//if (pulses > 359) {
// pulses = pulses - 360;
// } else if (pulses < 0) {
// pulses = pulses + 360;
//}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}
void B_CHANGE(){
if ( digitalRead(encoderA) == 0 ) {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is low
pulses++; // moving forward
} else {
// B rose, A is low
pulses--; // moving reverse
}
} else {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is high
pulses--; // moving reverse
} else {
// B rose, A is high
pulses++; // moving forward
}
}
// Make sure the pulses are between 0 and 359
//if (pulses > 359) {
// pulses = pulses - 360;
// } else if (pulses < 0) {
// pulses = pulses + 360;
// }
// tell the loop that the pulses have changed
pulsesChanged = 1;
}