gShield controlling stepper with out G-Code/PC?

Started by mrwildbob on in grblShield Support. 2 replies, last activity Aug 26, 2018.

#10466

Has anyone had any luck with writing arduino code to control the steppers with out using G-Code/PC? Here is what I am trying to do. I would like to control two steppers (X & Y) with a joystick. This is not a CNC machine but using it as a learning tool for some students. Long story short, I would like to interface the joystick with the gShield and move the motors. Acceleration and speed control would be a plus.

Here is the code I found somewhere on how to just turn the stepper. I'm not sure how to control the speed.

void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
pinMode(5, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(5, HIGH);
digitalWrite(2, LOW);
}
void loop() {
digitalWrite(2, HIGH);
delay(1);
digitalWrite(2, LOW);
delay(1);
}
#11116

yes, tried it today.
I'm using Version 5B, with an Arduino Due, Accelstepper lib

#include <AccelStepper.h>
#include <stdint.h>
#initial example captured from from mechapro.de

const uint8_t disablePin = 8;

// pin assignment X-Axis
//const uint8_t stepPin = 2;
//const uint8_t dirPin = 5;

// pin assignment Y-Axis
//const uint8_t stepPin = 3;
//const uint8_t dirPin = 6;

// pin assignment Z-Axis
const uint8_t stepPin = 4;
const uint8_t dirPin = 7;

// actuate stepper motor
void forwardstep() {  
  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(1);
  digitalWrite(stepPin, HIGH);
}

void backwardstep() {  
  digitalWrite(dirPin, LOW);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(1);
  digitalWrite(stepPin, HIGH);
}

// setting up the AccelStepper library with custom actuator functions
AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void setup()
{
  // configuration of gpio interface
  pinMode(stepPin, OUTPUT);
  //pinMode(errorPin, INPUT_PULLUP);
  pinMode(dirPin, OUTPUT); 
  pinMode(disablePin, OUTPUT); 
//  pinMode(sleepPin, OUTPUT); 
  
  digitalWrite(disablePin, HIGH);
  
  // ramp parameters
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20000);
}

int dir = 1;
void loop()
{
    if (stepper.distanceToGo() == 0)
    {
        // Random change to speed, position and acceleration
        // Make sure we dont get 0 speed or accelerations
        delay(10);
        stepper.move(dir * 300);
        dir = dir * -1;
    }
    stepper.run();
}
#11117

oops, please set

digitalWrite(disablePin, HIGH);

to

digitalWrite(disablePin, LOW);

to enable the stepper motors.

And set the 3rd line to
// initial example parts captured from mechapro.de

Back to grblShield Support or the forum archive.