CONTROL STEPPER MOTOR ON AND OFF

Hi I am building for myself a labeling machine for bottles (Wine and Beer) and will be using a stepper motor to drive the roll of prepared labels.

I will be using a UNO and an EasyDriver.

The concept is as follows:

A micro-switch will be used to start the process, it would be held on by the weight of the bottle.

Once activated the stepper will start and drive the roll until a sensor detects an edge of the label at a gap between each label and a signal will be sent to the UNO to stop the stepper.

I have in the past used PLC's for the control in most of the machines I have built but have now decided to use Arduino's. I am having trouble with the coding (as a 78 yo having to activate his brain with something different).

Have spent a lot of time with tutorials and also with published sketches starting with Blink and steppers etc.

I have used Demo for Different Functions as a guide and have also had success with speed control using a potentiometer.

By using the Demo for D-Fs and only using the forward motion (1 ) I can get the motor to work and have increased the loop for forward stepping to x < 10000 and it stops.

the code I have is shown

However I cannot get it to stop

Any help would be appreciated

Thanks

Beckett

Beckett:
...
the code I have is shown ...

Hey Beckett, whoops. I don't see any code.

Sorry see later

Here is code and sorry about the St up

[code]
//\DEMO FOR DIFFERENT FUNCTION

/**ENABLE TO BE USED ADD BUTTON TO BOTTOM
  SparkFun Easy Driver Basic Demo
  Toni Klopfenstein @ SparkFun Electronics
  March 2015
  https://github.com/sparkfun/Easy_Driver

  Simple demo sketch to demonstrate how 5 digital pins can drive a bipolar stepper motor,
  using the Easy Driver (https://www.sparkfun.com/products/12779). Also shows the ability to change
  

  Example based off of demos by Brian Schmalz (designer of the Easy Driver).
  http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html
******************************************************************************/
//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN  6

//Declare variables for functions
char user_input;
int x;
int state;

void setup() {

  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);

  resetEDPins(); //Set step, direction, microstep and enable pins to default states
  Serial.begin(9600); //Open Serial connection for debugging
  Serial.println();
  //Print function list for user selection
  Serial.println("Enter number :");
  Serial.println("1. START AND RUN .");

  Serial.println();
}

//Main loop
void loop() {
  while (Serial.available()) {
    user_input = Serial.read(); //Read user input and trigger appropriate function
    digitalWrite(EN, LOW); //Pull enable pin low to allow motor control THIS CAN BE USED TO START AND STOP
    if (user_input == '1');
    StepForwardDefault();
  }



  {


    {

    }


    {

    }
    resetEDPins();
  }
}

//Reset Easy Driver pins to default states
void resetEDPins()
{
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
}

//Default microstep mode function
void StepForwardDefault()
{
  Serial.println("Moving forward at default step mode.");
  digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
  for (x = 1; x < 10000; x++) //Loop the forward stepping enough times for motion to be visible
  {
    digitalWrite(stp, HIGH); //Trigger one step forward
    delay(1);
    digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
}

Beckett:
However I cannot get it to stop

Looking at your code which is designed to make the stepper take 10,000 steps I'm guessing you want to be able to stop it before it has completed all those steps.

A much better approach is to make it move one step at a time and check the sensor between every step using code something like

void loop() {
  checkForBottle();
  checkForEdgeOfLabel();
  if (bottleDetected == true and edgeOfLabelDetected == false) {
     moveOneStep();
  }
}

...R
Stepper Motor Basics
Simple Stepper Code

Planning and Implementing a Program

  if (bottleDetected == true and edgeOfLabelDetected == false) {
     moveOneStep();
  }

I'd prefer to see that function called rotateBottleOneStep().

PaulS:

  if (bottleDetected == true and edgeOfLabelDetected == false) {

moveOneStep();
  }



I'd prefer to see that function called rotateBottleOneStep().

I take your point. However I think the motor is rotating the roll of labels and if so it should be moveLabelsOneStep()

...R