Hi Everyone,
I've decided to take my first stab at making a library for my project. Thought it would help me keep my main code neater. Below is the Header for the library
#ifndef Encoder_h
#define Encoder_h
#include <Arduino.h>
class Encoder{
public:
Encoder(int CLK, int DT);
int count(int CLK, int DT);
private:
};
class pressLength{
public:
pressLength(int button);
unsigned long duration(int button);
};
#endif
Below is the .cpp for the library
#include <Arduino.h>
#include "Encoder.h"
int currentStateCLK;
int previousStateCLK;
int counter;
int buttonState;
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long _duration = 0;
Encoder::Encoder(int CLK, int DT){
previousStateCLK = digitalRead(CLK);
}
pressLength::pressLength(int button){
}
int Encoder::count(int CLK, int DT){
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != previousStateCLK){
if (digitalRead(DT) != currentStateCLK) {
counter = counter - 1;
} else {
counter = counter + 1;
}
}
previousStateCLK = currentStateCLK;
return(counter/2);
}
unsigned long pressLength::duration(int button){
buttonState = digitalRead(button);
if(buttonState == HIGH){
startTime = millis();
}
else{
endTime = millis();
_duration = endTime - startTime;
return _duration;
}
}
I've tried to make a simple library so far with 2 functions. One to time how long a button has been pressed, which seems to work well, and another to read a rotary encoder and return a counter.
For some reason the rotary encoder library doesn't work. I think I know what some of the issues may be but not how to fix them. The library seems to work ok when I am using only one Encoder. I've used the simulation below to demonstrate.
See coder below.
#include "Encoder.h"
#define SW 13
#define inputDTL 12
#define inputCLKL 11
#define inputDTR 9
#define inputCLKR 8
Encoder encl(inputCLKL, inputDTL);
// Encoder encr(inputCLKR, inputDTR);
pressLength bt(SW);
int counterL = 0;
int counterR = 0;
void setup() {
Serial.begin(115200);
pinMode(SW, INPUT_PULLUP);
pinMode(inputDTL, INPUT);
pinMode(inputCLKL, INPUT);
// pinMode(inputDTR, INPUT);
// pinMode(inputCLKR, INPUT);
}
void loop() {
Serial.println(encl.count(inputCLKL, inputDTL));
// Serial.print(" / ");
// Serial.println(encr.count(inputCLKR, inputDTR));
}
Adding more than one encoder is where things get a bit messy. One thing that is clearly happening is that the same counter is used for both encoders so each time one increments, so does the other. The intervals are also not +/-1, it seems to be random when using two encoders with the library. I've tried solving this by adding a counter argument into the function so that it'll increment each counter individually. See below alteration I made to do this
Header
int count(int CLK, int DT);
.cpp
int Encoder::count(int CLK, int DT, int counter){
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != previousStateCLK){
if (digitalRead(DT) != currentStateCLK) {
counter = counter - 1;
} else {
counter = counter + 1;
}
}
previousStateCLK = currentStateCLK;
return(counter/2);
}
Of course in the .cpp I removed the counter variable I created too.
I created a counter variable in the main code and used it to call the function but this resulting in no changes to the counter despite rotating the encoder in either direction.
Any advice on how I can work around the counter issue and suggestions as to why I seem to be getting arbitrary values when using more than one encoder would be fantastic.
Thanks
A