Stepper motor and light barrier

I would like to change my code so that stepper motor only turns when the light in your light barrier is interrupted. Then he should take a certain number of steps and turn back after 10 seconds. #include <Stepper.h>

// Define number of steps per revolution
const int stepsPerRevolution = 32;

// Initialize the stepper motor with the sequence 1-3-2-4 for Arduino Nano Every
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

unsigned long previousMillis = 0;
const long interval = 1000; // 1 second
bool motorActive = false;
bool motorReturning = false;

void setup() {
// Set the pin mode for A0
pinMode(A0, INPUT);

// Start the serial communication
Serial.begin(9600);
}

void loop() {
// Stepper motor speed
myStepper.setSpeed(900);

// Check the state of the pin A0
int sensorValue = digitalRead(A0);
unsigned long currentMillis = millis();

// Print the state of the pin A0 to the serial monitor
Serial.print("A0 state: ");
Serial.println(sensorValue);
delay(500);
if (sensorValue == LOW && !motorActive && !motorReturning) {
myStepper.step(2048);
previousMillis = currentMillis; // reset the timer
motorActive = true;
}

// Check if 1 second has passed since the last step
if (motorActive && (currentMillis - previousMillis >= interval)) {
myStepper.step(-2048);
motorActive = false; // reset motor active state
motorReturning = true; // indicate motor has returned to rest
}
}

You have our permission.

2 Likes

Your code looks pretty straight forward?

What exacly do you need help with? I dont think write code for you, we might help you with problems on your way changing your code by yourself :slight_smile: give it a try and tell us if you step into some problems :wink:

Br Max

you logic is a bit convoluted

look this over

const int stepsPerRevolution = 32;

#define MyHW
#ifdef MyHW
# include "sim.hh"
const byte PinSensor = A1;
Stepper myStepper (stepsPerRevolution, 10, 11, 12, 13);

#else
# include <Stepper.h>
const byte PinSensor = A0;
Stepper myStepper (stepsPerRevolution, 8, 10, 9, 11);

#endif


const unsigned long Interval = 1000; // 1 second
      unsigned long msecLst;

const char *stateStr [] = { "Idle", "Forward", "Reverse", "Stop" };
enum { Idle, Forward, Reverse, Stop };
int state = Idle;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (Idle != state && msec - msecLst >= Interval)  {
        msecLst += Interval;

        if (Stop == state)
            state = Idle;
        else if (Forward == state)  {
            myStepper.step (-2048);
            state = Reverse;
        }
        else {
            myStepper.step (2048);
            state = Forward;
        }
        Serial.println (stateStr [state]);
    }

    if (HIGH == digitalRead (PinSensor))  {
        if (Reverse == state)  {
            state = Stop;               // wait for current cycle to complete
        }
    }
    else  if (Idle == state)  {
        myStepper.step (2048);
        msecLst = msec;
        state   = Forward;
    }
}


void setup () {
    pinMode (PinSensor, INPUT_PULLUP);
    Serial.begin (9600);
    myStepper.setSpeed (900);
}

That should be 2048.

myStepper.setSpeed(900);

Max speed for a 28BYJ-48 motor is about 12 RPM.

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