Move telescope part 2

Continuation of: Original Post

I have all the hardware now ( I hope). I have:

CNC Shield Expansion Board V3.0
Arduino UNO R3 Board
A4988 Stepper Motor Driver with Heatsink
12v 30a Dc Universal Regulated Switching Power Supply 360w
Nema 17 Stepper Motor 42BYGH 1.8 Degree 38MM 1.5A 42 Motor (17HS4401S) x2
Jumpers
Joystick module

I have jumped MO and M1 on the CNC board for the motors to set the step.

Looking at the pictures:

  1. Do I need any other jumpers?
  2. Where do I connect the blue, grey, and white wires from the joystick on the CNC board?

20190928_181957.jpg
20190929_131438.jpg

With everything connected, the motor is not energized and spins freely. I assume if power was going to the motor, the motor wold not spin freely. Here is the code I used from Robin2:

// Simple demonstration to show how to move a servo with a joystick
//   or potentiometer so that the servo does not lose its position
//   when the joystick centres itself

#include <Servo.h>

byte servoPin = 7;  // servo signal connection
Servo myServo;
int servoPos = 90;
int servoMax = 130;
int servoMin = 45;
int servoMove = 3;  // number of degrees per movement

byte potPin = A0;   // center pin of joystick or potentiometer connected here
int potValue = 0;
int potCentre = 512; // adjust to suit your joystick
int potDeadRange = 50; // movements by this much either side of centre are ignored

unsigned long curMillis;
unsigned long readIntervalMillis = 100;
unsigned long lastReadMillis;

void setup() {
 Serial.begin(9600);
 Serial.println("Starting JoystickServo.ino");
 
 myServo.attach(servoPin);
 myServo.write(servoPos);
}

void loop() {
 curMillis = millis();
 readJoystick();
 moveServo();
 showPosition();
}

void readJoystick() {
 // check the time
 if (curMillis - lastReadMillis >= readIntervalMillis) {
 lastReadMillis += readIntervalMillis;
 // read the joystick
 potValue = analogRead(potPin);
 // figure if a move is required
 if (potValue > potCentre + potDeadRange) {
 servoPos += servoMove;
 }
 if (potValue < potCentre - potDeadRange) {
 servoPos -= servoMove;
 }
 // check that the values are within limits
 if (servoPos > servoMax) {
 servoPos = servoMax;
 }
 if (servoPos < servoMin) {
 servoPos = servoMin;
 }
 }
}

void moveServo() {
 myServo.write(servoPos);
}

void showPosition() {
 Serial.print("PotValue ");
 Serial.print(potValue);
 Serial.print("   ServoPos ");
 Serial.println(servoPos);
}

20190928_181957.jpg

20190929_131438.jpg

The enable pin for the stepper drivers [pin 8] is pulled up on the CNC shield. You need to set the pinMode for pin 8 to OUTPUT and write a LOW to pin 8 to enable the stepper driver outputs.

  1. Where do I connect the blue, grey, and white wires from the joystick on the CNC board?

Don't know what the colors represent. Do you have a data sheet for the joystick?

Assuming that the joystick is an analog stick, the analog inputs on the Uno are carried out to some of the pins on the CNC shield header with the limit switch inputs. See the attached image on bottom left of the Uno board.

cnc shield Uno pins.jpg

cnc shield Uno pins.jpg

Thanks, groundfungus. Update, I added a jumper on the shield at EN/GND which now energizes the attached motor. No longer spins freely. I don't understand your pin 8 remarks. I see that the pin is connected from uno to shield. Not what you mean about write. Also, do you know where my 3 joystick wires should connect on shield? I uploaded 2 images to show where the wires come from on the joystick. Just the same, white is Y axis, grey is X axis, blue is switch, black is ground, and red is 5v. Red and black are connected and I have power at the joystick.

Tia, Jim

If you want to control the enable pin from the Uno, you set the pinMode on pin 8 to output and then digitalWrite pin 8 LOW to enable the stepper driver and HIGH to disable the stepper driver. You may want, at some point, to disable the drivers to save power. Unpowered motors will not hold their positions, though.

If it is an analog joystick (potentiometers), it would connect, through the CNC shield, to analog inputs (A0 - A3) on the Uno. But you won't tell me what joystick you have so I have no idea what the wire colors mean.

Sorry, I did not realize that the first photo was the joystick. The X and Y wires go to anal8g inputs. Read the inputs with analogRead().

It's analog. I provided picture of what pins mean. Not sure what else I can provide. Also, you restated the same thing about write low. Write it where? Apologies, I am total newb to this stuff.

So, for example, my white wire from joystick that represent y-axis should connect to Abort on cnc which corresponds to A0 on Uno? Ok, white or black row of pins? Or does it matter?

Or does it matter?

It matters. The white side of the header connects to the Uno pins. The black side pins all connect to ground. You could connect the X to feed/hold (A1) and the switch (SW) to cycle start (A2).

Set the pinMode for A2 (pin 16) to INPUT_PULLUP. The pin will read HIGH when the switch is open (unpressed) and LOW when closed (pressed). Analog input pins can be used as digital pins so read the pin with digitalRead()

pinMode(16, INPUT_PULLUP);
// and 
switchState = digitalRead(16);

Ok. So, connecting white wire to Abort still does nothing. I am guessing there is something missing in the code? Maybe that's what you were driving at but I don't understand. :slight_smile:

I just throw this anywhere in the code? hahahaha, I hate this. Please know that I am laughing aloud. I wish this came easier to me.

You will need to do an analogread() of the pin to get the joystick position value.

int yPosition = 0;  // declare a global variable to hold y position
// in the loop code 
yPosition = anaogRead(A0);

I just throw this anywhere in the code?

What do you want to do with the joystick values?

You are very helpful but these snippets of code...I don't know where to place them.

What do I want the joystick to do;

When I move the joystick right, the motor keeps moving in that direction until I let go and then doesn't move. Same for left, up, and down. I have no use for the depress switch (SW). This is for a telescope so small movements preferred. I think for now a single speed is all I need. I may discover after using it that more push to right (direction) equals more speed. For now, I'd just like to see a motor respond to joystick input. :slight_smile:

I am busy right now, but in a bit i will write an example for you. I can't do it right now because I will want to set up an Uno with joystick and stepper so that I can test any code that I write before I post it and can't get to my PC and stuff right now. I am on my phone, now.

The switch would be a good way to select course/fine adjustments.

In the code that you posted in the original post you have servos. Is that a fact or are the motors actually steppers?

2 stepper motors. Servos came from code provided by Robin2. Since they were just notes, I ignored them.

Goal is to move telescope up and down as well as rotate the base left or right.

Thanks for all the help, especially from your phone!!

Here is a short example to show how to do 1 (the X) axis. When the joystick is centered the stepper is stopped. When moved off center the stepper will move slowly and when the joystick is moved toward the extremes the motor will move faster. The stepper step code is from Robin2's Simple Stepper code tutorial. Another of his tutorials that may be of interest is the Stepper Motor Basics.

// simple code to move a stepper with a joystick
// by C. Goulding aka groundFungus

#define FORWARD 1
#define REVERSE 0

const byte xJoyPin = A0; // Abort on CNC shield
const byte xStepPin = 2;
const byte xDirPin = 5;
const byte enablePin = 8;

boolean xDir = FORWARD;
int xPos;
int lowSpeed = 20;
int highSpeed = 5;

unsigned long curMillis = 0;
unsigned long prevStepMillisX = 0;
unsigned long millisBetweenStepsX= 10;

void setup()
{
   Serial.begin(115200);
   pinMode(xStepPin, OUTPUT);
   pinMode(xDirPin, OUTPUT);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}

void loop()
{
  curMillis = millis();
  xPos = analogRead(xJoyPin);
  
  if(xPos >= 0 && xPos < 200)
  {
    millisBetweenStepsX = highSpeed;
    xDir = REVERSE;
    singleStepX();
  }
  else if(xPos > 201 && xPos < 450)
  {
    millisBetweenStepsX = lowSpeed;
    xDir = REVERSE;
    singleStepX();
  }
  else if(xPos > 560 && xPos < 850)
  {
    millisBetweenStepsX = lowSpeed;
    xDir = FORWARD;
    singleStepX();
  }
    else if(xPos > 851 && xPos < 1024)
  {
    millisBetweenStepsX = highSpeed;
    xDir = FORWARD;
    singleStepX();
  }
  //else stop
}

void singleStepX()
{
   if (curMillis - prevStepMillisX >= millisBetweenStepsX)
   {
      digitalWrite(xDirPin, xDir);
      prevStepMillisX = curMillis;
      digitalWrite(xStepPin, HIGH);
      digitalWrite(xStepPin, LOW);
   }
}

Tested and works on my Uno, analogJoystick, CNC shield and stepper.

NOTE: I added an X to the timing variable names and the step function name so that you can pretty much copy the X code and change the Xs to Ys for the other axis (or name the axes however you want).

Works like a charm! Can't thank fungus enough!!!