JaBa, count is being incremented within an ISR because these are not stepper motors, they are brushed DC motors with encoders so I was under the impression that I cant tell them what a step is, I have to read when a step has been done, so i'm telling the motor to move, and it will stop when the ISR has read that its done the step. I hope that makes some sort of sense?
I guess tickNumber doesn't need to go inside the for loop and wasn't there for any particular reason, i've moved it to the start of the function now, but tickNumber may be different for each function I am calling.
Almost complete code (character limit so chopped some off the end):
#define InA1 11 // INA motor pin
#define InB1 12 // INB motor pin
#define PWM1 13 // PWM motor pin
#define encodPinA1 21 // encoder A pin
#define InA2 8
#define InB2 9
#define PWM2 10
#define encodPinA2 20
#define InA3 5
#define InB3 6
#define PWM3 7
#define encodPinA3 19
#define InA4 2
#define InB4 3
#define PWM4 4
#define encodPinA4 18
#define LOOPTIME 100
#define FORWARD 1
#define BACKWARD 2
unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
volatile long count = 0; // rotation counter
volatile long count2 = 0;
volatile long count3 = 0;
volatile long count4 = 0;
long countInit;
long countInit2;
long countInit3;
long countInit4;
long tickNumber = 0;
long tickNumber2 = 0;
long tickNumber3 = 0;
long tickNumber4 = 0;
long tick = 0; //testing giving it 1 tick as a standard
long tick2 = 0;
long tick3 = 0;
long tick4 = 0;
int magnetPin = 22 ;
boolean run = false; // motor moves
void setup() {
Serial.begin(9600); // baud rate
Serial.flush();
pinMode(magnetPin, OUTPUT); // keep the pin high unless told to be low in void loop() this means that the magnets are always on unless told to be off
digitalWrite(magnetPin, HIGH); // the reason to keep magnets on is we cant do 2 things at once in loop so this is the only way to have the magnet on and be moving the robot
pinMode(InA1, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(encodPinA1, INPUT);
digitalWrite(encodPinA1, HIGH); // turn on pullup resistor
attachInterrupt(2, rencoder, FALLING); //interrupt 2 is pin 21 on 2560 mega according to one source, others people say otherwise... but they are wrong
pinMode(InA2, OUTPUT);
pinMode(InB2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(encodPinA2, INPUT);
digitalWrite(encodPinA2, HIGH);
attachInterrupt(3, rencoder2, FALLING);
pinMode(InA3, OUTPUT);
pinMode(InB3, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(encodPinA3, INPUT);
digitalWrite(encodPinA3, HIGH);
attachInterrupt(4, rencoder3, FALLING);
pinMode(InA4, OUTPUT);
pinMode(InB4, OUTPUT);
pinMode(PWM4, OUTPUT);
pinMode(encodPinA4, INPUT);
digitalWrite(encodPinA4, HIGH);
attachInterrupt(5, rencoder4, FALLING);
}
void loop() {
//************************************* Motor 1,2,3&4 Mixed Operations **************************************
test1(); // calls test 1 subroutine
resetCounts();
test2(); // calls test 2 subroutine
resetCounts();
while (1) {}
}
//*******************************************************Operations*************************************************************************
void resetCounts() {
countInit = 0; //reset all counters in preparation for next routine
count = 0;
countInit2 = 0;
count2 = 0;
countInit3 = 0;
count3 = 0;
countInit4 = 0;
count4 = 0;
}
//************************************************
void test1() { //Aiming to move the arm from home position into position 1
const long ticknumber = 2040;
const long tickNumber2 = 2560;
const long tickNumber3 = 3840;
const long tickNumber4 = 510;
while ((count3) < (tickNumber3))
{
if ((count) < (tickNumber))
{
moveMotor(FORWARD, 204, 1); //direction, PWM speed, 10 tick
}
if ((count2) < (tickNumber2))
{
moveMotor2(FORWARD, 204, 1);
}
if ((count3) < (tickNumber3))
{
moveMotor3(BACKWARD, 204, 1);
}
if ((count4) < (tickNumber4))
{
moveMotor4(FORWARD, 204, 1);
}
}
}
//*************************************************
void test2() { //return to home position from last move
const long ticknumber = 2040;
const long tickNumber2 = 2560;
const long tickNumber3 = 3840;
const long tickNumber4 = 510;
while (((count3) < (tickNumber3)))
{
if ((count) < (tickNumber))
{
moveMotor(BACKWARD, 255, 1);
}
if ((count2) < (tickNumber2))
{
moveMotor2(BACKWARD, 255, 1);
}
if ((count3) < (tickNumber3))
{
moveMotor3(FORWARD, 255, 1);
}
if ((count4) < (tickNumber4))
{
moveMotor4(BACKWARD, 255, 1);
}
}
}
//************************************************
//******************************************Motor control stuff***********************************************
void moveMotor(int direction, int PWM_val, long tick) {
countInit = count; // abs(count)
// tickNumber = tick;
if (direction == FORWARD) motorForward(PWM_val);
else if (direction == BACKWARD) motorBackward(PWM_val);
}
void moveMotor2(int direction, int PWM_val, long tick2) {
countInit2 = count2; // abs(count)
// tickNumber2 = tick2;
if (direction == FORWARD) motorForward2(PWM_val);
else if (direction == BACKWARD) motorBackward2(PWM_val);
}
void moveMotor3(int direction, int PWM_val, long tick3) {
countInit3 = count3; // abs(count)
// tickNumber3 = tick3;
if (direction == FORWARD) motorForward3(PWM_val);
else if (direction == BACKWARD) motorBackward3(PWM_val);
}
void moveMotor4(int direction, int PWM_val, long tick4) {
countInit4 = count4; // abs(count)
// tickNumber4 = tick4;
if (direction == FORWARD) motorForward4(PWM_val);
else if (direction == BACKWARD) motorBackward4(PWM_val);
}
// ******************************************** Encoder counting **************************************************
void rencoder() { // pulse and direction, direct port reading to save cycles
if (PD0 == LOW || PD0 == HIGH) { //added the || to make sure i am getting into the ISR, it counts ++ whatever the pin state.
count++;
}
else {
count--;
}
if ((count) > tickNumber) {
motorBrake(); //apply the brake when the count of the motor exceeds the tick number (what we ask it to do in void loop() eg 510*1.2
}
}
void rencoder2() {
if (PD1 == HIGH || PD1 == LOW ) {
count2++;
}
else {
count2--;
}
if ((count2) > tickNumber2) {
motorBrake2();
}
}
void rencoder3() {
if (PD2 == LOW) {
count3++;
}
else {
count3--;
}
if ((abs(count3)) > tickNumber3) {
motorBrake3();
}
}
void rencoder4() {
if (PD3 == LOW) {
count4++;
}
else {
count4--;
}
if ((abs(count4)) > tickNumber4) {
motorBrake4();
}
}
// ***********************Code for forward backward and brake *******************************
//************Motor 1*********************
void motorForward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, LOW);
digitalWrite(InB1, HIGH);
run = true;
}
void motorBackward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, LOW);
run = true;
}
void motorBrake() {
analogWrite(PWM1, 0);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, HIGH);
run = false;
}
void motorOff() {
analogWrite(PWM1, 0);
digitalWrite(InA1, LOW);
digitalWrite(InB1, LOW);
run = false;
}
Sorry its a mess and there are lines that likely no longer serve any purpose