/* Encoder Library - NoInterrupts Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
// If you define ENCODER_DO_NOT_USE_INTERRUPTS *before* including
// Encoder, the library will never use interrupts. This is mainly
// useful to reduce the size of the library when you are using it
// with pins that do not support interrupts. Without interrupts,
// your program must call the read() function rapidly, or risk
// missing changes in position.
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
// Beware of Serial.print() speed. Without interrupts, if you
// transmit too much data with Serial.print() it can slow your
// reading from Encoder. Arduino 1.0 has improved transmit code.
// Using the fastest baud rate also helps. Teensy has USB packet
// buffering. But all boards can experience problems if you print
// too much and fill up buffers.
// Change these two numbers to the pins connected to your encoder.
// With ENCODER_DO_NOT_USE_INTERRUPTS, no interrupts are ever
// used, even if the pin has interrupt capability
#define ENC1 0
#define ENC2 1
Encoder myEnc(ENC1, ENC2);
int led = 14;
// avoid using pins with LEDs attached
void setup() {
pinMode(ENC1,INPUT_PULLUP);
pinMode(ENC2,INPUT_PULLUP);
Serial.begin(9600);
while(!Serial); // wait for user to connect...
Serial.println("Basic NoInterrupts Test:");
pinMode(led, OUTPUT);
}
long position = -999;
elapsedMillis dbad;
uint8_t led_state=0;
void loop() {
long newPos = myEnc.read();
if (newPos != position) {
position = newPos;
Serial.println(position);
}
if(dbad>999)
{
dbad=0;
led_state=1-led_state;
digitalWrite(led,led_state);
} // don't delay so much if you want encoder results without using interrupts...
// With any substantial delay added, Encoder can only track
// very slow motion. You may uncomment this line to see
// how badly a delay affects your encoder.
//delay(50);
}