Hi, I'm trying to refer to the variable 'encoder0Pos from the interrupt, so that when it reaches a certain value, the stepper motor stops rotating. The problem I found is that the value of the variable is 0 and although it's changing in the interrupt, I need to use it in the loop function. Can anyone help please?
The first thing to do is not to print inside the ISR. Printing depends on interrupts and they are disabled when in an ISR. What do you see if you print it in loop() ?
Where, in loop(), are you printing it? The Stepper::step() method is a blocking function. It will not return until the number of steps that it was supposed to take has been reached. Checking the position of the encoder at that point seems pointless.
Using an encoder, the idea is to step, check, step, check, etc. until the motor is at the desired position.
Do you mind helping me with the step, check, step, check because I can't figure it out.
The loop() function is called over and over, so the repeat part is handled.
For the rest, if the encoder value is lower than the desired end position, step(1). If it is higher than the desired end position, step(-1). That's simple enough, isn't it?
Read encoder0pos into a temporary variable in loop() and do your calculations with the temporary variable. You should briefly stop the interrupts while you read the value