Stepper motor, limit switches, and random function

Hi again,
I posted last week about connecting my Nema 23 stepper motor movememt to limit switches. I

I have built an 8’ linear stage powered by a Nema 23 stepper motor with driver that turns a 8’ acme
rod. Attached to the rod is a nut with a housing that travels back and forth on an aluminum track. I
have successfully hooked up the motor, driver, and arduino and have achieved forward and
backward movement as well as random movement.

At this point I’ve attached the limit switches, they are pressed and tell the motor to start turning the opposite direction. I’m very happy with this, but the random movement I want in between the limit switches isn’t quite right. I have limited the range from 50,000 steps to 200,000 steps (motor has 200 steps per revolution). But the largest random number generated is usually around 30,000. The serial monitor is also showing negative numbers are being generated. I’m confused. Here is the code:

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
int lim1=4; //define backward switch pin
int lim2=3; //define forward switch pin
int limitSwitch1;
int limitSwitch2;
int locTarget;
int counter;
int dirPin = LOW;//this will be used for direction

long randomNumber;

void setup() {

//start serial communication

  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode(lim1, OUTPUT);
  pinMode(lim2, OUTPUT);
  digitalWrite(DIR,dirPin);//start out with DIR LOW
  locTarget=(int)random(20000,191000); //target location for motor


void loop() {
limitSwitch1= digitalRead(lim1); //read input pin
limitSwitch2= digitalRead(lim2); //read input pin

//send at start messages to the serial monitor

  //switch directions to go right
  Serial.println("lim1");//tell us that the switch was hit
  dirPin=LOW;//change direction
  delay(10); //avoid bouncing
  locTarget=(int)random(50000,200000); //target location for motor on the LEFT side
 }else if (limitSwitch2==1){
  //switch directions to go left
  Serial.println("lim2");//tell us that the switch was hit
  dirPin=HIGH; //change direction
  delay(10); //avoid bouncing
  locTarget=(int)random(50000,200000); //target location for motor on the RIGHT side
  //move the motor
      digitalWrite(DIR, dirPin);

     //increment counter
      //reset our location target
      locTarget=(int)random(20000,300000); //target location for motor on the LEFT side
      if(dirPin ==LOW) dirPin = HIGH;
      if(dirPin ==HIGH) dirPin = LOW;  


I’m also confused as to why the limit switches are functioning as OUTPUT. I came to this resolution after trial and error, they were not working when they were input.
I’m attaching a screen shot of the serial monitor (without Limit switch activation, just the random integers generated).
Any thoughts or advice will be greatly appreciated!

  locTarget = (int)random(20000, 191000); //target location for motor

What is the largest value that a signed int can hold ?

I don't know. I was unfamiliar with signed/unsigned integers until your post.

I read online that: "The difference between unsigned ints and (signed) ints, lies in the way the highest bit, sometimes referred to as the "sign" bit, is interpreted. In the Arduino int type (which is signed), if the high bit is a "1", the number is interpreted as a negative number, and the other 15 bits are interpreted with (2’s complement math)."

Do you suggest writing

 locTarget = (unsigned int)random(20000, 191000); //target location for motor

Ok, disregard prior response. I changed

int locTarget;

(line in the beginning) to

unsigned int locTarget;

That resolved the negative integer issue (THANK YOU!) but I'm not seeing random number generations above 63000. It should be generating up to 191,000.

You missed the point.

Read this int and this unsigned int

Are you pointing out that I have a limited range of random integers depending on int/unsigned int?

Ok, I changed the int() and unsigned int() to long() and I'm getting the range I need!