Stepper motor code

Hello I hope this is the correct place in the forum for my question.

I'm working on a homemade analog camera that has an Arduino for controlling a couple of functions
. I have cobbled together a code from a pre written code off the internet, the problem is it's working the way I want.

If I post the code and what I require any chance of some help. If this is against the rules I guess I can use one of the pay for programming services. Colin

ColRay:
If I post the code and what I require any chance of some help.

That's how it works. Don't forget code tags. Read the two threads circled in the picture first.

Hi Delta Thanks for your response I have read the information on the two pages. Now I'm not sure of how I should go, not being involved that much in electronics ( I have a very limited knowledge ) My interest is homemade film cameras and I'm only just branching out into using an Arduino as a control unit. So from what I can understand I need better knowledge before using this forum . Colin

Who said that? Those posts were about the forum etiquette and how to post your code. Why don’t you just ask your question already.

Hi it looks like I didn’t understand.

I have loaded the code I’m using on ‘Pastbin’ I hope it works.

The way I would like the program to run A/F.

Then the Arduino is turned on the stepper motor is in a docking position

  1. it is started by pushing a button.
  2. the motor mover CW and stops
    3 . press the button again and the motor moves CCW and stops at the docking position.
    4 the Arduino now cac be turned off

I didn’t get it quite right, So second try this time using the code protocol

 int button = 8;
int startStepping;
//Put all the pins in an array to make them easy to work with
int pins[] {
  2, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
  3, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
  4, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
  5 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
};

//Define the wave drive sequence.
//With the pin (coil) states as an array of arrays
int waveStepCount = 4;
int waveSteps[][4] = {
  {HIGH, LOW, LOW, LOW},
  {LOW, HIGH, LOW, LOW},
  {LOW, LOW, HIGH, LOW},
  {LOW, LOW, LOW, HIGH}
};

//Define the full step sequence.
//With the pin (coil) states as an array of arrays
int fullStepCount = 4;
int fullSteps[][4] = {
  {HIGH, HIGH, LOW, LOW},
  {LOW, HIGH, HIGH, LOW},
  {LOW, LOW, HIGH, HIGH},
  {HIGH, LOW, LOW, HIGH}
};


//Keeps track of the current step.
//We'll use a zero based index.
int currentStep = 0;

//Keeps track of the current direction
//Relative to the face of the motor.
//Clockwise (true) or Counterclockwise(false)
//We'll default to clockwise
bool clockwise = true;

bool counterclockwise = false;
// How many steps to go before reversing, set to zero to not bounce.
//int targetSteps = 0; //targetSteps 0 means the motor will just run in a single direction.
//int targetSteps = 2048; //2049 steps per rotation when wave or full stepping
int targetSteps = 3072; //3072 steps for 1.5 rotations when wave or full stepping
//int targetSteps = 4096; //4096 steps per rotation when half stepping

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  for (int pin = 0; pin < 4; pin++) {
    pinMode(pins[pin], OUTPUT);
    digitalWrite(pins[pin], LOW);
  }
  pinMode(button, INPUT_PULLUP);
}

void step(int steps[][4], int stepCount) {
  //Then we can figure out what our current step within the sequence from the overall current step
  //and the number of steps in the sequence
  int currentStepInSequence = currentStep % stepCount;

  //Figure out which step to use. If clock wise, it is the same is the current step
  //if not clockwise, we fire them in the reverse order...
  int directionStep = clockwise ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;

  //Set the four pins to their proper state for the current step in the sequence,
  //and for the current direction
  for (int pin = 0; pin < 4; pin++) {
    digitalWrite(pins[pin], steps[directionStep][pin]);
  }
}
void loop() {
  startStepping = digitalRead(button);
  while (startStepping == 0) {
    //Comment out the Serial prints to speed things up
    //Serial.print("Step: ");
    //Serial.println(currentStep);

    //Get a local reference to the number of steps in the sequence
    //And call the step method to advance the motor in the proper direction

    //Wave Drive
    //int stepCount = waveStepCount;
    //step(waveSteps,waveStepCount);

    //Full Step
    int stepCount = fullStepCount;
    step(fullSteps, fullStepCount);

    // Increment the program field tracking the current step we are on
    ++currentStep;

    // If targetSteps has been specified, and we have reached
    // that number of steps, reset the currentStep, and reverse directions
    if (targetSteps != 0 && currentStep == targetSteps) {
      currentStep = 0;
      startStepping = 1;
      //clockwise = !clockwise;
    } else if (targetSteps == 0 && currentStep == (stepCount - 1)) {
      // don't reverse direction, just reset the currentStep to 0
      // resetting this will prevent currentStep from
      // eventually overflowing the int variable it is stored in.
      currentStep = 0;
    }

    //2000 microseconds, or 2 milliseconds seems to be
    //about the shortest delay that is usable. Anything
    //lower and the motor starts to freeze.
    //delayMicroseconds(2250);
    delay(2);
  }
}[code]

ColRay:
the problem is it's working the way I want.

Why would it be a problem if it is doing what you want? Or did you forget the word "not".

Assuming you did then you need to tell us in as much detail as possible what the program actually does and what you want it to do that is different.

If the program won't compile then you need to post the compiler error message.

...R

hi robin the code compiles fine no error messages. The problem is I can't get it working after step two. When the button is pushed it restarts at cycle two

hen the Arduino is turned on the stepper motor is in a docking position

  1. it is started by pushing a button.
  2. the motor mover CW and stops
  3. press the button again and the motor moves CCW and stops at the docking position.
    4 the Arduino now can be turned off until the next time the camera is used.

I reckon it would be much easier to make a working program using the Stepper library as it will take care of the sequences of coil energizing. Then all you need is something like myStepper.step(375); to go one way and myStepper.step(-375); to go the other way.

...R

Thanks, Robin I will give it a try

You should read this tutorial on state machines.