Comments (2)
I also found out I have one of the 4-steps per mechanical click encoders.
This was my simple solution, add some check for value change etc if needed.
enc_pos = myEnc.read() >> 2; // divide down to mechanical steps if needed
It would be nice if the lib supported some sort of robust divide down for these.
from encoder.
Here is a demo-code that uses two encoders and functions that are handy to use.
As I'm a native german and this demo-code was for a german user it is guess what ? written in german
// selbsterklärende Namen vergleichsweise lang
// aber dafür eben SELBST-erklärend Endung "_pin"
// zeigt an es ist ein IO_pin
const int Step_CLK_pin = 3;
const int Step_DT_pin = 4;
const int Speed_CLK_pin = 6;
const int Speed_DT_pin = 7;
long eingestellte_Steps = 0;
long eingestellte_Speed = 0;
Encoder Step_Eingabe (Step_DT_pin ,Step_CLK_pin);
Encoder Speed_Eingabe(Speed_DT_pin,Speed_CLK_pin);
void setup() {
Serial.begin(115200);
} // SETUP
// ACHTUNG ! BESONDERHEIT!! das "&"-Zeichen
// MUSS da stehen, weil der geänderte Variablenwert
// nach "oben" zurückgegeben werden soll !
boolean Step_Geandert(long &p_StepZahl ) {
boolean RueckgabeWert = false; // wenn NICHT gedreht wurde false zurückgebeb
static long neuStep;
static long lastStep;
neuStep = Step_Eingabe.read();
neuStep = neuStep >> 2; // bitshift 2 nach rechts ist geteilt durch 4
if (lastStep != neuStep) {
p_StepZahl = neuStep;
lastStep = neuStep;
RueckgabeWert = true; // wenn WIRKLICH gedreht wurde true zurückgeben
}
return RueckgabeWert;
}
boolean Speed_Geandert(long &p_Speed ) {
boolean RueckgabeWert = false; // wenn NICHT gedreht wurde false zurückgebeb
static long neuSpeed;
static long lastSpeed;
neuSpeed = Speed_Eingabe.read();
neuSpeed = neuSpeed >> 2; // bitshift 2 nach rechts ist geteilt durch 4
if (lastSpeed != neuSpeed) {
p_Speed = neuSpeed;
lastSpeed = neuSpeed;
RueckgabeWert = true; // wenn WIRKLICH gedreht wurde true zurückgeben
}
return RueckgabeWert;
}
void loop() {
// das hier muss sehr oftaufgerufen werden damit das PRG
// mitbekommt ob an einem Encoder gedreht wurde
if ( Step_Geandert (eingestellte_Steps) ) {
Serial.print("steps:");
Serial.println(eingestellte_Steps);
}
if ( Speed_Geandert (eingestellte_Speed) ) {
Serial.print("speed:");
Serial.println(eingestellte_Speed);
}
}
/* Erklärung des Codes
Das "Umrechnen" von Flanken in Encoder-Ticks wurde in
zwei functions "Step_Geandert" "Speed_Geandert
gepackt.
Diese functions geben true oder false zurück
Je nachdem ob wirklich am Encoder gedreht wurde oder nicht
Das kann man dann in If-Abfragen benutzen um nur dann eine Aktion
auszulösen wenn WIRKLICH am Encoder gedreht wurde
Es wurden zwei globale Variablen
"eingestellte_Steps" und "eingestellte_Speed"
definiert.
Diese Variablen werden vom Aufruf der functions
"Step_Geandert" "Speed_Geandert" aktualisiert
Diese Aktualisierung wird durch das "&"-Zeichen in der function
Defintion erreicht
*/```
best regards Stefan
from encoder.
Related Issues (20)
- Double counting pulse HOT 1
- ATMEGA 168P add interrupt pins mapping to interrupt_pins.h
- [ Feature Request ] : Seeeduino XIAO : support hardware interrupts
- Error trying to build SpeedTest.ino on Teensy 4.1 in Arduino 1.8.19: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile unsigned char*' in initialization
- No support for Arduino nano rp2040 connect HOT 4
- STM32H743ZI2 144 Nucleo compatibility issues HOT 1
- Raspberry Pico (RP2040) support? HOT 6
- RP Pico RP2040 ConfigurableFirmata Encoder Interrupts not working HOT 4
- Amazing library! Is there an example Windows serial interface anywhere?
- undercounting steps only in one direction, only when going slowly, with a linear encoder
- Internal pull-up HOT 3
- library.properties Still Says Version 1.4.2 HOT 20
- Encoder causes esp32 to crash when using platformIO HOT 3
- Attaching interrupt to another pin while using ENCODER_OPTIMIZE_INTERRUPTS
- Interrupts are unknown for this board, please add to this code
- Feature: getDirection() HOT 2
- Raspberry pi pico RP2040 Not supported HOT 1
- Arduino UNO R4 MINIMA not supported HOT 4
- wrong counting backwards HOT 4
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 encoder.