Hi, I am following this tutorial.
http://www.instructables.com/id/Motor-With-Encoder-How-to-Read-Input-Value-From-En/
And it is working well. But now I want to attach another motor. I need the Encoder to at an Interrupt Pin. How do I add more? There is only two on the Uno and I already using the SCL/SDA for the motor (same as 2 and 3). I tried this, but it doesn't work. The encoder does count up, but very slowly.
https://playground.arduino.cc/Main/PinChangeInterrupt
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEncLeft(3,2);
Encoder myEncRight(7,8);
// avoid using pins with LEDs attached
long oldPosition = -999;
void pciSetup(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
// Use one Routine to handle each group
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
digitalWrite(13,digitalRead(8) and digitalRead(9));
}
ISR (PCINT1_vect) // handle pin change interrupt for A0 to A5 here
{
digitalWrite(13,digitalRead(A0));
}
ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here
{
digitalWrite(13,digitalRead(7));
}
void setup() {
int i;
// set pullups, if necessary
for (i=0; i<=12; i++)
digitalWrite(i,HIGH); // pinMode( ,INPUT) is default
for (i=A0; i<=A5; i++)
digitalWrite(i,HIGH);
pinMode(13,OUTPUT); // LED
// enable interrupt for pin...
pciSetup(7);
pciSetup(8);
pciSetup(9);
pciSetup(A0);
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
void loop() {
long masterPower = myEncLeft.read();
if (masterPower != oldPosition) {
oldPosition = masterPower;
Serial.print("L: ");
Serial.println(masterPower);
delay(500);
}
long slavePower = myEncRight.read();
if (slavePower != oldPosition) {
oldPosition = slavePower;
Serial.print("R: ");
Serial.println(slavePower);
delay(500);
}
}