Comments (4)
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.
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.
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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adaencoder.