Hi,
I am new to Arduino and this forum.
I am working on a project where I use an encoder to measure an angle. The code below works well when connecting my encoder to Digital pins 2 and 3. My idea is to use a TFT display to show the angle. But the TFT uses digital pins 2 and 3, and actually all pins from 2 to 13. I wanted to use two digital pins 23 to 53 in the Arduino mega, I have used pins 44 to 46, 52 & 53, pretty much all from 23 to 53, and the readings I got are erroneous. If I connect back to any pin digital 2 to 13, it reads well.
I would be grateful if someone can guide me in solving this.
#include <Encoder.h>
Encoder myEncoder(2, 3);
int inPin = 7; // pushbutton connected to digital pin 7
const unsigned long timePeriod = 1000; //1000 miliseconds
unsigned long startTime;
float cpr = 2000; //Cycles per revolution (from the encoder)
float ppc = 4; //Pulses per Cycle (from encoder. TYpical 4-> A=0 B=0, A=1 B=0, A=1 B=1 & A=0 B=1
float angperpulse = 360 / (cpr*ppc); // Angle per pulse
void setup() {
Serial.begin(9600);
Serial.println("Encoder Test:");
}
long position = -999;
void loop() {
int zero = digitalRead(inPin);
long newPosition;
newPosition = myEncoder.read();
if (newPosition != position) {
Serial.print("Angle = ");
Serial.print(newPosition * angperpulse, 3);
Serial.print(", zero = ");
Serial.print(zero);
Serial.println();
position = newPosition;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (zero == 1) {
Serial.read();
Serial.println("Reset myEncoder to zero");
myEncoder.write(0);
}
}