two sensors to control stepper motor 28byj-48.

Hello,

I am currently working on a project where I want to use two sensors to control stepper motor 28byj-48.

I will try to shortly explain my project. I want to control presence of object by Laser sensor + I want to use optical sensor to move motor to required position/zero position.

This Part of program is already done:

1st step- press button to start cycle /program.
2nd step - Laser diode will check presence of object.

  • if object will be detected motor will move in one direction (1500steps)
  • if object will be not detected motor will move in reverse direction.

Curretly I want to add optical sensor, with target to move motor to required position.

I would like to program it in following way :

1st step- press button to start cycle /program.
2nd step - Laser diode will check presence of object.
a) if object will be detected motor will move to one direction (1500 steps),
then motor continues in moving in the same direction, and then motor will be stopped by optical sensor in required/zero position

b) if object will be not detected motor will move in reverse direction,.
then motor continues in moving in the reverse direction, and then motor will be stopped by optical sensor in required/zero position

I have spent several days with programing ( playing with Stepper and Accelstepper liberies), but I didnt find way how to implement optical sensor to my program succesfully.

I will appreciate any recomendation from your side.

Thank you

Lubos

Here is code

#include <Stepper.h> // Include the 'Stepper' Library

#define LASER 6 // pin 6 for  Laser sensor
#define ACTION 13 // pin 13 for action to do something
#define SENSOR 7 // pin 7 for Optical sensor
#define buttonPin 8//pin for button

int run;
int buttonPin;

Stepper myStepper(300, 2,4,3,5); 

void setup() {

  pinMode(LASER, INPUT);//define detect input pin
  pinMode(SENSOR, INPUT);//define detect input pin
  pinMode(ACTION, OUTPUT);//define ACTION output pin
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop() {

  if(digitalRead(buttonPin) == LOW) // If button is pushed enter this loop
{
       
int detected = digitalRead(LASER);// read Laser sensor
 
  if(detected == HIGH)
  {
    
myStepper.setSpeed(30); // Set the speed, you can play around with this.
myStepper.step(1500); // Number of steps to go clockwise
delay(500); // Delay 1/2 second before next

// Here I want to program, that motor will continue moving in the same direction, then motor will be stopped by optical sensor in required/zero position

}else{
  
myStepper.setSpeed(30); // Set the speed to half the above one
myStepper.step(-1500); // Go the same number of steps, but this time anti-clockwise
delay(500); // Wait another 1/2 sec before repeating
// Here I want to program, that motor will continue moving in the reverse direction, then motor will be stopped by optical sensor in required/zero position
  }
  delay(200);
}
}

I assume you want to be able to detect the laser while the motor is moving. If so you need to use the AccelStepper library and its non-blocking run() or runSpeed() functions. The standard Stepper library blocks the Arduino until it completes.

The same is also true of the delay() function. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Hello Robin, Thank you for answer.

Laser will check presence of object ( in my case availability of subcomponent), then motor will move in one / or reverse direction ( to sort parts into two groups , with sub component /without subcomponets). And use optical sensor to bring motor to required position, be prepared for next cycle.

I know how to bring motor to specific position ( by defining exact number of steps to reach this position ), but I don know how to add to program, that motor will continue moving in same direction to reach zero/required position.

Thank you for any help.

Lubos

Perhaps I got things backwards. It seems that the laser needs to detect something while the motor is stationary and the optical sensor needs to be able to detect something while the motor is moving so it stops at the right spot. Is that correct?

If I am correct then what I said in Reply #1 still applies.

If I have still misunderstood the requirement perhaps you can post a photo of a simple diagram that illustrates the machine you are trying to create.

...R
Simple Image Posting Guide

Hello Robin, Finally, I have solved my problem. I have programed it in different way, anyway my application is working well :slight_smile:

Thank you for your support.

Lubos

here is code

#include <Stepper.h>                                            //Arduino Stepper Library

const int stepsPerRevolution = 200;                            //Number of steps per revolution 

Stepper sm(stepsPerRevolution, 2, 4, 3, 5);                   // pins definition for motor

int stepCount = 0;                                             // number of steps the motor has taken
int stepDirection = 1;
int OpticalSwitch = 10;                                        //input for Optical Switch
int run;
int buttonPin;

#define LASER 6                                              // pin 6 for  Laser sensor
#define ACTION 13                                            // pin 13 for action to do something


void setup()
{             
  pinMode(OpticalSwitch, INPUT);                                 //input sense for limitswitch1
  pinMode(LASER, INPUT);                                         //define detect input pin
  pinMode(ACTION, OUTPUT);                                       //define ACTION output pin
  pinMode(buttonPin, INPUT_PULLUP);                              //define buttonPIN as input
  buttonPin = 7;                                                 //pin for button
  
}

void loop()
{
  int speedKnob = analogRead(A1);                              //potentiometer; pin A1
  int motorSpeed = map(speedKnob, 0, 1023, 50, 150);         

  if (motorSpeed > 0)                                       
  {
    sm.setSpeed(motorSpeed);
  }

  if(digitalRead(buttonPin) == LOW)                         // If button is pushed enter this loop
{
       
int detected = digitalRead(LASER);                          // read Laser sensor
 
  if(detected == HIGH)                                      // if high do following :
  {

while(digitalRead(OpticalSwitch) == LOW)                     //OpticalSwitch read - blocked
  {
     stepDirection = 1;                         //define or change direction
                                     
    sm.step(stepDirection); 
} 

delay(1000);

if (digitalRead(OpticalSwitch) == HIGH) {         //OpticalSwitch read - open

sm.step(-100);                                  //define steps to clears the OpticalSwitch

}  
}
else
{
while(digitalRead(OpticalSwitch) == LOW)                     //OpticalSwitch read - blocked
  {
     stepDirection = -1;                         //define or change direction
                                     
    sm.step(stepDirection); 
} 

delay(1000);

if (digitalRead(OpticalSwitch) == HIGH) {         //OpticalSwitch read - open

sm.step(100);                                  //define steps to clears the OpticalSwitch

} 
} 
}
}