Hello,
I'm using Rotary Encoder to switch on Light and set up the Brightness on UNO (YUN).
The Code have got 2 Parts: Switch ON/OFF and the Brightness SetUp with Interrupts.
Without the Brightness part of the Software, the I/O Switch is working fine.
When I add the Interrupts for the Brightness, the I/O Switch working only on every second Stop.
The Encoder, with 30 positions, I'm using is this one: http://www.conrad.at/ce/de/product/705538/Drehimpulsgeber-5-VDC-001-A-Schaltpositionen-30-360-DDM-Hopt-Schuler-427-021101AL001-IP50-zwischen-Achse-und-Gehae?ref=list
enum PinAssignments
{
encoderPinA = 2, // right (labeled DT on our decoder, yellow wire)
encoderPinB = 3, // left (labeled CLK on our decoder, green wire)
//clearButton = 8 // switch (labeled SW on our decoder, orange wire)
};
volatile unsigned int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 0; // change management
static boolean rotating=false; // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;
the Setup
pinMode(encoderPinA, INPUT_PULLUP); // new method of enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
//pinMode(clearButton, INPUT_PULLUP);
// turn on pullup resistors (old method)
//digitalWrite(encoderPinA, HIGH);
// digitalWrite(encoderPinB, HIGH);
// digitalWrite(clearButton, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
the Loop
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
//START Rotary Encoder
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.print("Index:");
Serial.println(encoderPos, DEC);
lastReportedPos = encoderPos;
}
/*if (digitalRead(clearButton) == LOW ) {
encoderPos = 0;
}*/
}
// Interrupt on A changing state
void doEncoderA(){
// debounce
if ( rotating && ledState == HIGH) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set && encoderPos < 240 && ledState == HIGH)
encoderPos += 16;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state, same as A above
void doEncoderB(){
if ( rotating && ledState == HIGH) delay (1);
if( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if( B_set && !A_set && encoderPos > 0 && ledState == HIGH)
encoderPos -= 16;
rotating = false;
}
IMHO the Problem is in the SetUp part.
Does anybody have got an Idea what causes this problems?
Regards,
Richard