<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 myEnc(2,3);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
from this site
My serial monitor only shows a single digit on each line- either a zero or a one
Then I tried this code:
#include <Encoder.h>
Encoder knob(2,3);
void setup()
{
Serial.begin(9600);
}
long positionKnob = -999;
void loop(){
long newKnob;
newKnob = knob.read();
if (newKnob != positionKnob ) {
Serial.print("Knob = ");
Serial.print(newKnob);
Serial.println();
positionKnob = newKnob;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knob.write(0);
}
}
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
// put your setup code here, to run once:
AFMS.begin();
myMotor->setSpeed(150);
myMotor->run(FORWARD);
}
void loop() {
// put your main code here, to run repeatedly:
}
What encoder? Post a link to the product page or data sheet.
How, exactly, did you connect the encoder to the Arduino? Post a hand drawn wiring diagram.
When experimenting with a new sensor, it is a great idea to use that sensor all by itself, with simple example code and without any other distracting things like motors, LCD displays, etc.
Connect the encoder according to one of the many on line tutorials (avoid Instructables, as most of them are crap), load the example program and twist the encoder shaft by hand to test it.
Did you try the following test, from that Instructable, to verify correct encoder operation?
A little experiment I did with multimeter :
power the encoder with 2 AA battery, attach multimeter GND cable to GND of the battery , and multimeter RED cable to the C1 or C2 output of the encoder.
As you turn the motor slowly, you can see that the output voltage of C1 and C2 change from 0V to 3V.
If you have 2 multimeter, attach C1 and C2 each to a multimeter, then turn the multimeter slowly you can see the value of multimeter change from 3V-3V, 0V-3V,0V-0V,3V-3V, which is the pulse according the background theory)