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);
}
}