Pages: [1]   Go Down
Author Topic: [SOLVED] Rotary Encoder Interrupt  (Read 856 times)
0 Members and 1 Guest are viewing this topic.
Australia
Offline Offline
Full Member
***
Karma: 8
Posts: 161
You dont have to touch powerlines for them to kill you, even looking at them for too long will make them angry.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im using a rotary encoder to change the brightness of the Blinkm, the blinkm works, it sets to red fine, but the interrupt is not working. the encoder is pins 2 and 3 and common on the encoder is GND. Any ideas?

Code:
#include <Wire.h>

#define encoder0PinA  2
#define encoder0PinB  3

volatile unsigned int encoder0Pos = 0;
int value = 0;

void setup() {

  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor

  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
 Wire.begin();                // set up I2C on 4 and 5 analog
  Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
  Wire.send("o");    
 Wire.endTransmission();
  
  Wire.beginTransmission(0x09);
  Wire.send("c");
  Wire.send(0xff);             // value for red channel
  Wire.send(0x00);             // value for blue channel
  Wire.send(0x00);             // value for green channel
  Wire.endTransmission();      // leave I2C bus         // debug - remember to comment out
                                              // before final program run
}

void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}

void doEncoder(){
  if (digitalRead(encoder0PinA) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way
                                             // encoder is turning
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
    else {
      encoder0Pos = encoder0Pos + 1;         // CW
    }
  }
  else                                        // found a high-to-low 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
    }

  }
   if ( encoder0Pos < 0){
     encoder0Pos =  encoder0Pos + 255;
  }
  else if ( encoder0Pos > 255){
     encoder0Pos =  encoder0Pos -255;
  }
  value =  encoder0Pos;
  // set up I2C on 4 and 5 analog
  Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
  Wire.send("c");              // ‘c’ == fade to color
  Wire.send(value);             // value for red channel
  Wire.send(value);             // value for blue channel
  Wire.send(value);             // value for green channel
  Wire.endTransmission();      // leave I2C bus         // debug - remember to comment out
}

« Last Edit: February 07, 2011, 01:01:09 am by fliggygeek » Logged

"Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent di

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Why don't you try moving the below Wire statements out of the ISR function and put it into the loop() function. Then run it and see if that solves the problem.

Code:
// set up I2C on 4 and 5 analog
  Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
  Wire.send("c");              // ‘c’ == fade to color
  Wire.send(value);             // value for red channel
  Wire.send(value);             // value for blue channel
  Wire.send(value);             // value for green channel
  Wire.endTransmission();      // leave I2C bus         // debug - remember to comment out

Normally you don't put more into a ISR then you absolutly need to, short and sweet is the rule. Also all system interrupts are disable while inside a ISR so I'm not even sure those wire statements will work inside a isr. Easy to test and see.

Lefty
Logged

Australia
Offline Offline
Full Member
***
Karma: 8
Posts: 161
You dont have to touch powerlines for them to kill you, even looking at them for too long will make them angry.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You legend, works a treat. Karma awarded accordingly.
Logged

"Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent di

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You legend, works a treat. Karma awarded accordingly.

Even a blind squirrel finds a nut now and then.  smiley-grin
Logged

Pages: [1]   Go Up
Jump to: