camera pan control using encoder and stepper motor. code problem


i’ve been working on my first project that involves an arduino mini, an encoder, and easy driver and a stepper motor. the idea behind the project is to have the encoder control the stepper motor 1:1. i have this working with this code:

int encoder_a = 2;
int encoder_b = 3; 
int motor_direction = 5;
int motor_step = 4;
int enablepin = 11;
#include <delay.h>
volatile long motor_position, encoder;

void setup () {

  digitalWrite(enablepin, LOW);    
  digitalWrite(encoder_a, HIGH); 
  digitalWrite(encoder_b, HIGH); 
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
  pinMode(enablepin, OUTPUT);
  pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);

  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, encoderPinChangeA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  encoder = 0; //reseet the encoder position to 0

void loop() {
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    encoder = 0; //encoder--;
  else if (encoder < 0) {
    digitalWrite(motor_direction, LOW); 
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    encoder = 0; //encoder++;
void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
  else {

the one thing i would like to add and cant work out, is how to make the encoder not only move the motor but trigger the enablepin low for 2 seconds after which it returns to high. i would like it to restart this timer whenever the encoder moves.

i looked at the code below for an led and it seemed perfect but couldn’t seem to make it work within my project.

if you could help me translate the code below to work for my project i would be very grateful. i am new to code and desperately need some help. thank you

Example 1

Implementing a simple timer
unsigned long Timer “ALWAYS use unsigned long for timers, not int”
(variable declaration outside setup and loop, of course)

if digitalRead==Down
then “set timer” Timer = millis , digitalWrite ON
if “timer expired” millis-Timer >= 15000UL
then digitalWrite Off

(the “UL” after the number is a syntax detail that is important when
dealing with large numbers in millis and micros, therefore it
is shown although this is pseudo code)

Result: It works just as well as the delay version, but slightly differently: The timer is constantly reset as long as the button is pushed. Thus only when releasing does it start “count” the 15 seconds. If you push shortly inside the 15 seconds period, it starts counting from anew. Notice in particular that digitalRead is looking at the input as fast as loop() runs.
The code is doing a digitalWrite OFF,even when the LED is not lit for every loop() pass, and likewise digitalWrite ON as long as the button is pushed. Fortunatly this has no ill effects (apart from taking a handfull of microseconds time)

Please use code tags when posting code. Click Modify on your post, highlight code, click # button, and save.

sorry about that, i'm new here.