Struggling to stop stepper motor for project after a loop

Hi,
A simple project to finish. The project is simple, but I'm a complete beginner. It's about motorised blinds which automatically close when it is bright and open when it is dark. Thanks for any help
this is the code :
''''
#include <Stepper.h>//Include the Arduino Stepper Library

const float STEPS_PER_REV = 32; // Number of steps per internal motor revolution

const float GEAR_RED = 64;// Amount of Gear Reduction

const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;// Number of steps per geared output rotation

int StepsRequired;// Number of Steps Required

Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

int photo; // sensor

void setup()

{

// Nothing (Stepper Library sets pins as outputs for stepper motor)

pinMode(6,OUTPUT); // led blue

pinMode(5,OUTPUT); // led green

}

void loop()

{

photo = analogRead(A1);

Serial.begin(9600);

Serial.print("signal from sensor = " );

Serial.println(photo);

if (photo <250){

digitalWrite(6,HIGH); // blue led is on

digitalWrite(5,LOW);

StepsRequired = STEPS_PER_OUT_REV * 2 ; // Rotate CW 2 turns

steppermotor.setSpeed(1000);

steppermotor.step(StepsRequired);

delay(1000);}

else {

photo = analogRead(A1);

Serial.begin(9600);

Serial.print("signal from sensor = " );

Serial.println(photo);

digitalWrite(6,LOW);

digitalWrite(5,HIGH);//green is on

StepsRequired = - STEPS_PER_OUT_REV * 2; // Rotate CCW 2 turns

steppermotor.setSpeed(1000);

steppermotor.step(StepsRequired);

delay(1000);

//steppermotor.setSpeed(0);

}

}
'''

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi, @michalganobis
Welcome to the forum.

What is the problem with the code you posted in post #1?

Have you developed this code in stages?
In other words;
Have you got code that JUST reads the sensor, please post link to data/specs.
Have you got code that JUST runs the stepper back and forth, please post a link to data/specs.

The idea is to prove your hardware individually before combining and trying to get it to work.

What does your Serial.print say in the IDE monitor, if you have a problem?
How are you powering your project?

Can you please post a circuit diagram, please no Fritzy images?
An image of a hand drawn schematic will be fine and include component names and pin labels.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

hi
Firstly I try to use a dc motor in the project but probably need to add limit switches on both ends of the blinds to stop rotation, so I try with a stepper motor. But when I add on at the end of the circle 0 rotation motor stops responding for sensor change( LDR or PHOTO TRAN).thats my problem motor should wait for a change of signal? And then move opposite direction the same amount rotation.
driver board - ULN 2003 pin - 8,9,10,11 ; STEP MOTOR 28BYJ-48 ;LEDs -6,5 ,LDR pin A1
Any suggestion I will be delighted.
Thanks, michal

is a speed of 1000 rpm valid (1000*64*32 steps/sec). i've found that i can't step a motor faster than once / msec. why not try something much smaller and try increasing it.

unnecessary code suggests confusion about how things work

  • "Serial.begin()" should be called just once (not 3x) in setup.
  • there's no need for a 2nd analogRead() nor the additional prints
  • there's no need for the delays except possibly outside the if/else just to limit the # of prints
  • setSpeed() can be done once in setup
  • StepsRequired could be calculated just once and the argument to step() be StepsRequired or -StepsRequired

Hi,
What is the device circled?
Please post an image of it please.

An LDR will need a different circuit to a phototransistor.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.