Coder Social home page Coder Social logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
Unfortunately, the Mega 2560 is not supported by PinChangeInt.  That said, I 
don't see why it wouldn't work.  The  Interrupt code is supposed to be the 
same.  Can you go into the code, #define DEBUG, and send me the output?

Also, post your sketch.

My email is [email protected]

Thanks.

Original comment by [email protected] on 24 Sep 2011 at 4:02

from adaencoder.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
Thank you for responding.

Yes indeed, I saw later PinChangeInt that does not work with Mega2560...
So I developed myself the reading of the encoder. It work like your code but 
use CHANGE PIN triggered external interrupts

Here is my code :

#include <digitalWriteFast.h>

#define encoderPinA 2
#define encoderPinB 3
#define encoderPinBtn 4
#define encoderPinSwt 5

#define spkPin 12

volatile int16_t encoderPosition = 0;
volatile int8_t encoderState = 0;
int16_t oldEncoderPosition = 0;

void setup() {
 pinMode(encoderPinA, INPUT);
 pinMode(encoderPinB, INPUT);
 pinMode(encoderPinBtn, INPUT);
 pinMode(encoderPinSwt, INPUT);

 pinMode(spkPin, OUTPUT);

 attachInterrupt(0, encoderChange, CHANGE);
 attachInterrupt(1, encoderChange, CHANGE);

 Serial.begin(115200);
}

void loop() {
 input();

 Serial.print(encoderState, DEC);
 Serial.print(" ");
 Serial.println(encoderPosition, DEC);

 delay(10);
}

void input() {
 int16_t encoderDifference = encoderPosition - oldEncoderPosition;
 bool encoderBtn = digitalReadFast(encoderPinBtn);
 static bool oldEncoderBtn = 0;
 static uint8_t encoderStateReset = 0;

 if(encoderDifference) {
  tone(spkPin, encoderPosition * 100);
  encoderStateReset = 0;
 } else {
  encoderStateReset++;
  if(encoderStateReset == 50) {
   encoderState = 0;
   encoderStateReset = 0;
  }
 }

 if(encoderBtn and !oldEncoderBtn) {
  tone(spkPin, 1000, 10);
 }

 oldEncoderPosition = encoderPosition;
 oldEncoderBtn = encoderBtn;
}

void encoderChange() {
 static uint8_t newAB = 3;
 static uint8_t oldAB;
 static const int8_t encoderStates[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};

 oldAB = newAB;
 newAB = PINE >> 4;
 //newAB = digitalReadFast(encoderPinB) * 2 + digitalReadFast(encoderPinA);
 encoderState += encoderStates[oldAB * 4 + newAB];

 if(encoderState > 3) {
  encoderPosition++;
  encoderState = 0;
 } else if(encoderState < -3) {
  encoderPosition--;
  encoderState = 0;
 }
}


Original comment by [email protected] on 24 Sep 2011 at 4:41

from adaencoder.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
Be aware of the assumptions built into the encoderChange code.  I believe it 
does not intrinsically account for switch bounce.  The program works because 
the loop has some built-in debounce features, in that it takes some miliseconds 
between tests of the encoder state.  You can certainly depend on it as long as 
you realize that it takes advantage of the relatively long duration between 
loop iterations.

Original comment by [email protected] on 3 Dec 2011 at 6:43

from adaencoder.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
I don't have a Arduino Mega so cannot configure/test this code on it.  I 
apologize.

Original comment by [email protected] on 4 Feb 2012 at 5:52

  • Changed state: WontFix

from adaencoder.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.