Control stepper motor via CANBUS

Hi!
Im having problem controling my stepper motor via CAN, im not sure im using "currentPosition" right tho...

When im trying the motor is just wibrating...

Im using PCAN.

#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Stepper.h>
#include <CustomStepper.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

#include <mcp_can.h>
#include <mcp_can_dfs.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2,4,6,7);
AccelStepper stepper;
int stepCount = 0; // number of steps the motor has taken
int currentPosition = 0;
//long currentPosition (); //current position in the start

void goToPosition (int steps){
//long currentPosition;
Serial.print("JAG ÄR INNE!");

if (steps <= 0 && stepper.currentPosition() <= 0){ // 0% to 50%
Serial.print("STEPS < CURRENT");
Serial.println(steps);
myStepper.step(steps-stepper.currentPosition());
currentPosition=stepper.currentPosition();
Serial.print("Current = " );
Serial.print(currentPosition);
Serial.print("\t Goal = ");
// if (steps < currentPosition){
//
// myStepper.step(steps - currentPosition));
// }
// if (steps > currentPosition){
//
// myStepper.step(steps - currentPosition);
// }
if (stepper.currentPosition() == 0){
myStepper.step(steps);
currentPosition=stepper.currentPosition();
}
//currentPosition == steps;
}
if (steps >= 0 && currentPosition >= 0){ // 0% to 100%
Serial.print("STEPS>CURRENT");
if(steps > currentPosition){
myStepper.step(steps - currentPosition);
}
if(steps < currentPosition){
myStepper.step(currentPosition-steps);

}
//currentPosition == steps;
}

}

void setup (){

myStepper.setSpeed(60); // 60 rpm
Serial.begin(9600);

if(Canbus.init(CANSPEED_250)){
Serial.println("CAN Init ok");
}
else{
Serial.println("Can't init CAN");
}

Serial.println("");
delay(1000);
Serial.println("port manipulation commenced");
DDRD = DDRD | B11111000;
Serial.println("33%");
DDRB = DDRB | B00000001;
Serial.println("66%");
DDRC = DDRC | B00111111;
Serial.println("100%");
Serial.println("port manipulation complete");
Serial.println("");

}

void loop() {
int steps;

tCAN message;
if (mcp2515_check_message()) {
Serial.print("Check msg ");
if (mcp2515_get_message(&message)){
Serial.print("Get msg ");

if(message.id == 0x300){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 0%");
//myStepper.step(-59);
steps = -59;
goToPosition(steps);
Serial.print("DONE MOVE!");
}
if (b1 == 0){
Serial.print("DONT MOVE!!!");
myStepper.step(0);
}

}

if (message.id == 0x325){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position in between 0% and click");
steps == -23;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

if (message.id ==0x337){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To left click");
steps == -13;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

if (message.id ==0x350){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 50%");
steps == 0;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

if(message.id == 0x363){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 100%");
steps == 59;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

if (message.id == 0x381){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position in between 100% and click");
steps == 23;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

if (message.id ==0x400){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To right click");
steps == 13;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}

}

}
}

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. Then I will study it. See How to use the Forum Your code is too long to study quickly.

Can you make the motor move the way you want when you are not using CANBUS ?

...R
Stepper Motor Basics

#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Stepper.h>
#include <CustomStepper.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

#include <mcp_can.h>
#include <mcp_can_dfs.h>

//Skriver in alla metoder i setup, ropar efter dem i loopen. Här kan man ju då ropa efter dom lite hur man vill.
//"0 grader" (-59 steg från 50 grader), "100 grader" (59 grader från 50 grader), klicken ligger 12 steg från 50 grader. Svep: 118 steg.

// CAN-ID:
// 0% : 300
// 25% : 325
// Left click : 337
// 50% : 350
// Right click : 363
// 75% : 381
// 100% : 400


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor

Stepper myStepper(stepsPerRevolution, 2,4,6,7);   
AccelStepper stepper;
int stepCount = 0;         // number of steps the motor has taken
int currentPosition = 0;
//long currentPosition (); //current position in the start

void goToPosition (int steps){
//long currentPosition;
Serial.print("JAG ÄR INNE!");
  
if (steps <= 0 && stepper.currentPosition() <= 0){ // 0% to 50%
    Serial.print("STEPS < CURRENT");
    Serial.println(steps);
    myStepper.step(steps-stepper.currentPosition());
    currentPosition=stepper.currentPosition();
    Serial.print("Current = " );                       
    Serial.print(currentPosition);      
    Serial.print("\t Goal = ");  
//    if (steps < currentPosition){
//      
//      myStepper.step(steps - currentPosition));
//    }
//    if (steps > currentPosition){
//      
//    myStepper.step(steps - currentPosition);
//    }
    if (stepper.currentPosition() == 0){
      myStepper.step(steps);
      currentPosition=stepper.currentPosition();
    }
    //currentPosition == steps;
  }
if (steps >= 0 && currentPosition >= 0){ // 0% to 100%
  Serial.print("STEPS>CURRENT");
    if(steps > currentPosition){
      myStepper.step(steps - currentPosition);
    }
    if(steps < currentPosition){
      myStepper.step(currentPosition-steps);
      
    }
    //currentPosition == steps; 
    }

}


void setup (){
  
myStepper.setSpeed(60); // 60 rpm
Serial.begin(9600);


if(Canbus.init(CANSPEED_250)){
    Serial.println("CAN Init ok");
  }
  else{
    Serial.println("Can't init CAN");
  }

  Serial.println("");
  delay(1000);
  Serial.println("port manipulation commenced");
  DDRD = DDRD | B11111000;
  Serial.println("33%");
  DDRB = DDRB | B00000001;
  Serial.println("66%");
  DDRC = DDRC | B00111111;
  Serial.println("100%");
  Serial.println("port manipulation complete");
  Serial.println("");
   
}
  
void loop() {
  int steps;
 
 tCAN message;
  if (mcp2515_check_message()) {
    Serial.print("Check msg ");
    if (mcp2515_get_message(&message)){
      Serial.print("Get msg ");
      
      if(message.id == 0x300){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
        if(b1 == 1){
        Serial.print("To position 0%");
        //myStepper.step(-59);
        steps = -59;
        goToPosition(steps);
        Serial.print("DONE MOVE!");
      }
        if (b1 == 0){
          Serial.print("DONT MOVE!!!");
        myStepper.step(0);
      }
    
    }
    
     if (message.id == 0x325){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
        if(b1 == 1){
        Serial.print("To position in between 0% and click");
        steps == -23;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
     
      }

      if (message.id ==0x337){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
       if(b1 == 1){
        Serial.print("To left click");
        steps == -13;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
     
      
      }
     
      if (message.id ==0x350){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
       if(b1 == 1){
        Serial.print("To position 50%");
        steps == 0;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
       
     }

    if(message.id == 0x363){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
        if(b1 == 1){
        Serial.print("To position 100%");
        steps == 59;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
       
    }
    
     if (message.id == 0x381){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
        if(b1 == 1){
        Serial.print("To position in between 100% and click");
        steps == 23;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
       
     }

      if (message.id ==0x400){
        int b1 = message.data[0];
        int b2 = message.data[1];
        int b3 = message.data[2];
       if(b1 == 1){
        Serial.print("To right click");
        steps == 13;
        goToPosition(steps);
      }
        if (b1 == 0){
        myStepper.step(0);
      }
      
     }
        

   }
}

}

I can make the motor take one step at the time, but not many steps at one time..

Thank you so much!!

Can't I use "currentPosistion" with a stepper motor? :o

I can make the motor move as I want when Im not using the CANBUS

Jeanettem:
I can make the motor move as I want when Im not using the CANBUS

That suggests that the problem has to do with sending or receiving data using the CANBUS

Have you written a short program just to figure out how to do that?

What is generating the data that you are receiving from the CANBUS?
Have you any control over the format or content of the data?

What data are you trying to receive?

...R