Rotary Encoder Simulation

I don't know if this is the right forum for my issue, but if it is not, please excuse my misuse.

I need a rotary encoder simulator program for an Arduino mini pro -- What I need is to simulate an encoder with A and B outputs, with a pot controlling the speed. The output of the mini pro (or I can use a Arduino Uno - wouldn't have to be a mini.) has to have two signals -- square waves -- with one 'out-of-phase with the first. I can manage generating the first signal, but I need the second signal to be 'out-of-phase' by half the first signal's phase, but basically running at the same frequency. Needless to say, I am new at programming the Arduino -- I have a lot of experience with other programming languages, but never used C, so trying to learn that..

Any help would be greatly appreciated....

Plenty to explore in this thread about quadrature signal generation.

Oooo… Let me try!

const byte Speed_AIPin = A0;
const byte QuadratureA_DOPin = 4;
const byte QuadratureB_DOPin = 5;

const int DeadZone = 5; // Minium excursion from center that causes steps

const unsigned long MinDelay = 1;  // 1 millisecond delay so about 1000 steps per second
const unsigned long MaxDelay = 1000;  // 1000 millisecond delay so about 1 step per second

const byte A = 1;
const byte B = 2;
const byte QuadratureStates[4] = {0, A, A + B, B};
byte QuadratureState = 0;

void setup()
{
  pinMode(QuadratureA_DOPin, OUTPUT);
  pinMode(QuadratureB_DOPin, OUTPUT);
}

void loop()
{
  int speed = analogRead(Speed_AIPin) - 512;

  if (speed < -DeadZone)
  {
    QuadratureState = (QuadratureState - 1) % 4;
    speed = -speed;
  }
  else if (speed > DeadZone)
  {
    QuadratureState = (QuadratureState + 1) % 4;
  }

  if (speed > DeadZone)
  {
    digitalWrite(QuadratureA_DOPin, QuadratureStates[QuadratureState] & A);
    digitalWrite(QuadratureB_DOPin, QuadratureStates[QuadratureState] & B);
    delay(map(speed, DeadZone, 512, MaxDelay, MinDelay));
  }
}

has to have two signals -- square waves -- with one 'out-of-phase with the first. I can manage generating the first signal, but I need the second signal to be 'out-of-phase' by half the first signal's phase, but basically running at the same frequency.

digitalWrite(QuadratureA_DOPin, QuadratureStates[QuadratureState] & A);
    digitalWrite(QuadratureB_DOPin, QuadratureStates[QuadratureState] & B);
    delay(map(speed, DeadZone, 512, MaxDelay, MinDelay));

John, I think you need a phasing delay between the A and B signals.

I'm not certain that your generator code signals without this delay can be read as a quadrature encoder on a 16Mhz Arduino. The A/B lag is about 4us for the digitalWrite, and determining the B signal level when you detect an A change is going to be difficult.

cattledog:
John, I think you need a phasing delay between the A and B signals.

Don’t worry, only one pin changes at a time. The A signal changes only on the 0<->1 and 2<->3 transitions while the B signal changes only on the 1<->2 and 3<->0 transitions.