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
}
}
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 give it a try and tell us if you step into some problems