I am very new to Arduino.
Could somebody write code for me? I want to use encoder to control Stepper(sync 1:1 or any changeable Ratio, like 1:3).
BTW, I prefer NOT use "Interrupt" to achieve, because I will use this code as one subroutine in my loop, "Interrupt" may cause mess in loop.
Thanks in advance.
BTW, I prefer NOT use "Interrupt" to achieve
To achieve what? Encoders are almost always read using interrupts, since you do not want to miss any pulses, even if you are busy doing something else (like waiting for a stepper to step).
because I will use this code as one subroutine in my loop, "Interrupt" may cause mess in loop.
Comparing the number of steps commanded and the number of steps achieved can be done in the subroutine, even though the number of steps achieved is counted in the ISR.
"Interrupt" may cause mess in loop.
Not if properly written. The clock ticks. That triggers an interrupt. Does that cause mess in loop()? Of course not. Serial data arrives. That causes an interrupt. Does that cause mess in loop()? Of course not.
Thanks for your reply.
The code(sketch) I need:
"use Rotary Encoder(OUTPUT A, B) to control Stepper motor(DIR, STEP), when encoder turns 1 rev. , Stepper motor turns 3 rev.)
(Using Interrupt in code is OK)
First thing to do is look at the playground, and find the article on encoders. Copy the code, and connect the encoder correctly. Verify that changes to the encoder position actually cause the interrupt routine to be invoked.
When you know that that works, computing the number of steps that need to be taken is simply a matter of knowing the encoder count last time stepping happened, and the current encoder count. Multiply the difference by 3, step, and reset the previous encoder count value.
Note that the requirement is based on a ratio of revolutions, not a ratio of steps.