Hi,
I have received 2 318-ENC130175F-12PS rotary encoders yesterday and have been playing with them all day today, or at least tried to play with them.
I'm using this code, from the Arduino site, and nothing happens.
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
void setup() {
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
attachInterrupt(0, doEncoderA, CHANGE); // encoder pin on interrupt 0 (pin 2)
attachInterrupt(1, doEncoderB, CHANGE); // encoder pin on interrupt 1 (pin 3)
Serial.begin (9600);
Serial.println ("start reading encoder");
Serial.println (encoder0Pos, DEC);
}
void loop(){ //Do stuff here
}
void doEncoderA(){
if (digitalRead(encoder0PinA) == HIGH) { // look for a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
if (digitalRead(encoder0PinB) == HIGH) { // check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out
}
void doEncoderB(){
if (digitalRead(encoder0PinB) == HIGH) { // look for a low-to-high on channel B
if (digitalRead(encoder0PinA) == HIGH) { // check channel A to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else { // Look for a high-to-low on channel B
if (digitalRead(encoder0PinA) == LOW) { // check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out
}
Should I use resistors between the encoder and the pins?
Are the pins analog or digital pins?
There are no images showing how to connect stuff in the playground.
as I rotate the encoder and the serial monitor is started nothing happens.
Did I break my Arduino not using resistors and shorting the pins to 5V? (I have tested all used pins as inputs and they still seem to work fine)