Need help with random number generator for stepper motor

Below is what I have so far for my program, I’m trying to make a stepper motor start at a “0” point, than go to a random position at least 90 degrees past “0” on the opposite side, then return to “0”. Eventually I would like to incorporate an analog signal to make it return to “0” from the random number. I am brand new to this type of programming and any help would be greatly appreciated.

// What is what… accoding to Bjorn
// AO is analog value proportional to speed
// Pin 11 is output - direction
// Pin 12 is output - step
// Pin 13 is output - ~step

int mPulseCount = 0;
long mTargetPos = 0;
bool bMovingToTarget = false;

// One-time setup of hardware/software
void setup() {

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);

pinMode(A0, INPUT);
Serial.begin(9600);
Serial.println(“Hello world”);

// Seed random number generator
randomSeed(analogRead(1));
}

// Process loop
void loop()
{

// Read the analog value on Pin Analog 0
int iAnalogValue = analogRead(A0);

// If pulsing, continue
if ( mPulseCount > 0 )
{
mPulseCount = mPulseCount - 1;
}

// Start a move
else
{

// If WAS moving to target, move back
if ( bMovingToTarget )
{
mPulseCount = mTargetPos;
digitalWrite(11, mTargetPos < 0);
bMovingToTarget = false;
}

// Otherwise pick a new target location
else
{
mTargetPos = 1250 + random(90) * 138;
if ( mTargetPos > 2500 )
{
mTargetPos = mTargetPos - 4000;
}
bMovingToTarget = true;
digitalWrite(11, mTargetPos > 0);
}

mPulseCount = abs(mTargetPos);
Serial.print(“Joe’s Loop…”);
// Write the count out the serial port to the BT
Serial.print(mTargetPos);
Serial.print(", “);
Serial.print(mPulseCount);
Serial.print(”, “);
Serial.print(bMovingToTarget);
Serial.print(”, ");
Serial.print(iAnalogValue);
Serial.println();
// Wait
delay(4000);

}

// Perform a Step
digitalWrite(12, LOW);
digitalWrite(13, HIGH);

// Wait
delayMicroseconds(1 + iAnalogValue);

digitalWrite(12, HIGH);
digitalWrite(13, LOW);

// Wait
delayMicroseconds(1 + iAnalogValue);

}

Please edit you post to use code tags around the code. See How to use the forum.

But uhm, what is your problem? You never told us that....

// What is what...  accoding to Bjorn
// AO is analog value proportional to speed
// Pin 11 is output - direction
// Pin 12 is output - step
// Pin 13 is output - ~step

And instead of that bit, it would have been easier if Bjorn just assigned logical variable names to the pins ;)

const byte SpeedPin = A0;
const byte MotorDirectionPin = 11;
const byte MotorStepPin = 12;
const byte MotorNotStepPin = 13; //or whatever you mean with ~step
const byte SomeOtherRandomPinWithoutDescription = 10;

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Also what stepper driver do you have?

Thanks.. Tom... :)

I apologize that I apparently used the forum wrong but I have never used or even heard of an Arduino before this project. A guy at my work fumbled through and made the rough program but doesn't seem to want to tell me how any of it works or what it means. The problem I'm having obviously is that the program just goes to random positions. (SMAKN® TB6600 Upgraded Version 32 Segments 4A 40V 57/86 Stepper Motor Driver) is the driver that I am using. If someone is familiar enough to do something like this and is willing to get paid to do it than message me, I am willing to hire someone, if I had any idea how to find someone to hire than I would have already. Any help is greatly appreciated, thanks a lot guys.

Go to the forum section called Gigs and Collaborations

bikerhick: The problem I'm having obviously is that the program just goes to random positions.

Actually, it wasn't at all obvious to me that the random part was the problem. Especially as the program seems to be specifically written to do just that.

It's like saying:

"Hey I'm having some major problems with this program that flashes the LED on and off".

"Ok, but what specifically is the problem?"

"Well obviously the problem is that it's flashing the LED on and off, but I want it to play a tone. :confused: