I have an Arduino Mega 2560, an Omron Optical encoder with an Index (360 steps), a 60 led NeoPixel ring (no Clk wire) and another Arduino Uno. Here is my question, I have some code written that works with the Arduino Mega and optical encoder outputting decently to the serial monitor for diagnostic purposes. When I add the additional code to run the Neopixel ring I get a terrible response on the ring. I have tried the NeoPixel and FastLED libraries with the same terrible success. It even states in their documentation that they don't work well with interrupts. I started thinking about sending the 3 digit output to a Direct Pin output from the Mega to be read by another Mega/Uno to run the Neopixel Ring and a 4x20 LCD screen. Does anyone else have any ideas on this. Below is my code for the Mega and the Encoder.
// Wiring connections for my encoder:
// Brown : VCC = 5V
// Blue + Shield : 0V = GND
// Black : ENCODER0PINA, Pin 18
// White : ENCODER0PINB, Pin 17
// Orange : ENCODER0PINI, Pin 19
#define ENCODER0PINA 18 // this pin needs to support interrupts
#define ENCODER0PINB 17 // no interrupt required
#define ENCODER0PINI 19 // this pin needs to support interrupts
// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;
volatile byte INTFLAG1 = 0;
// track last position so we know whether it's worth printing new output
long previousPosition = 0;
void setup()
{
// inputs
pinMode(ENCODER0PINA, INPUT);
digitalWrite(ENCODER0PINA, HIGH); // turn on pullup resistor
pinMode(ENCODER0PINB, INPUT);
digitalWrite(ENCODER0PINB, HIGH); // turn on pullup resistor
pinMode(ENCODER0PINI, INPUT);
digitalWrite(ENCODER0PINI, HIGH); // turn on pullup resistor
// interrupts
attachInterrupt(5, onInterrupt, RISING); // encoder track A on interrupt 3 - pin 18
attachInterrupt(4, onReset, RISING); // encoder Index track on interrupt 4 - pin 19
// enable diagnostic output
Serial.begin (115200);
Serial.println("Ready.");
}
void loop()
{
// only display position info if has changed
if (encoder0Position != previousPosition )
{
Serial.println(encoder0Position, DEC);
previousPosition = encoder0Position;
}
}
// interrupt function needs to do as little as possible
void onInterrupt()
{
// read both directional inputs
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB);
if (a == b)
{
// b is leading a (counter-clockwise)
encoder0Position--;
if (encoder0Position < 0)
{
encoder0Position = ((encoder0Position + 360) % 360);
}
}
else
{
// a is leading b (clockwise)
encoder0Position++;
if (encoder0Position >= 360)
{
encoder0Position = ((encoder0Position - 360) % 360);
}
}
}
void onReset()
{
// read Index input
int i = digitalRead(ENCODER0PINI);
if (i == 1)
{
encoder0Position = INTFLAG1;
}
}