Initial commit
This is just the 2018 code configured for PlatformIO
This commit is contained in:
51
lib/RotaryEncoder/RotaryEncoder.cpp
Normal file
51
lib/RotaryEncoder/RotaryEncoder.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "RotaryEncoder.h"
|
||||
|
||||
RotaryEncoder::RotaryEncoder(int ENC_A, int ENC_B, int multiplier, int stepSize, int pauseLength) {
|
||||
_ENC_A = ENC_A;
|
||||
_ENC_B = ENC_B;
|
||||
_multiplier = multiplier;
|
||||
_stepSize = stepSize;
|
||||
_pauseLength = pauseLength;
|
||||
_oldENC_A = 0;
|
||||
_oldENC_B = 0;
|
||||
_lastENCread = 0;
|
||||
_ENCcounter = 0;
|
||||
_lastENCreadTime = micros();
|
||||
//define pin modes
|
||||
pinMode(_ENC_A, INPUT);
|
||||
pinMode(_ENC_B, INPUT);
|
||||
digitalWrite(_ENC_A,HIGH); //these pins do not have pull up resistors on an attiny...
|
||||
digitalWrite(_ENC_B,HIGH); //you must pull them up on the board.
|
||||
}
|
||||
int RotaryEncoder::readEncoder() {
|
||||
int a0Pin = digitalRead(_ENC_A);
|
||||
int a1Pin = digitalRead(_ENC_B);
|
||||
int changevalue = 1;
|
||||
int returnVal = 0;
|
||||
if(a0Pin != _oldENC_A || a1Pin != _oldENC_B) {
|
||||
if(_oldENC_A == a1Pin && _oldENC_B != a0Pin) {
|
||||
returnVal = 1;
|
||||
} else if (_oldENC_B == a0Pin && _oldENC_A != a1Pin) {
|
||||
returnVal = -1;
|
||||
}
|
||||
_oldENC_A = a0Pin;
|
||||
_oldENC_B = a1Pin;
|
||||
|
||||
if(returnVal != 0) {
|
||||
if(returnVal == _lastENCread) {
|
||||
_ENCcounter++;
|
||||
if((micros() - _lastENCreadTime) < _pauseLength) {
|
||||
changevalue = max((_ENCcounter/_stepSize)*_multiplier,1);
|
||||
}
|
||||
_lastENCreadTime = micros();
|
||||
} else {
|
||||
_ENCcounter=0;
|
||||
}
|
||||
_lastENCread = returnVal;
|
||||
}
|
||||
|
||||
return returnVal*changevalue;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
35
lib/RotaryEncoder/RotaryEncoder.h
Normal file
35
lib/RotaryEncoder/RotaryEncoder.h
Normal file
@ -0,0 +1,35 @@
|
||||
/***********************************
|
||||
Written by Joshua Oster-Morris/craftycoder.
|
||||
BSD license, check license.txt for more information
|
||||
All text above must be included in any redistribution
|
||||
|
||||
I strongly urge hardware debouncing with rotary encoders.
|
||||
A 10000pF cap between both the A and B pins to GND (the C pin)
|
||||
is suffcient to offer very consistent reads.
|
||||
|
||||
*ENC_A* is the arduino pin the A pin of the encoder is plugged in to.
|
||||
*ENC_B* is the arduino pin the B pin of the encoder is plugged in to.
|
||||
*multiplier* is what is sounds like
|
||||
*stepSize* is used to find the *multiplicand*
|
||||
*pauseLength* is the maximum amount of time in microseconds a user can pause before the number of steps resets
|
||||
Equations of interest.
|
||||
*multiplicand* = (consecutive steps in one direction with less than a *pauseLength* between them)/*stepSize*
|
||||
*direction* = 1 for clockwise or -1 for counterclockwise
|
||||
readEncoder() returns *multiplicand*x*multiplier*x*direction*
|
||||
****************************************/
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
class RotaryEncoder {
|
||||
public:
|
||||
RotaryEncoder(int ENC_A, int ENC_B, int multiplier, int stepSize, int pauseLength);
|
||||
int readEncoder();
|
||||
private:
|
||||
int _ENC_A, _ENC_B, _oldENC_A, _oldENC_B, _lastENCread, _ENCcounter, _pauseLength, _multiplier, _stepSize;
|
||||
long _lastENCreadTime;
|
||||
};
|
||||
|
||||
20
lib/RotaryEncoder/examples/VelocityTest/VelocityTest.ino
Normal file
20
lib/RotaryEncoder/examples/VelocityTest/VelocityTest.ino
Normal file
@ -0,0 +1,20 @@
|
||||
#include <RotaryEncoder.h>;
|
||||
int val = 0;
|
||||
RotaryEncoder encoder(A0,A2,5,6,3000);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(57600);
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
int enc = encoder.readEncoder();
|
||||
int changevalue = 1;
|
||||
if(enc != 0) {
|
||||
val = val + (enc);
|
||||
val = min(val,4095);
|
||||
val = max(val,0);
|
||||
Serial.println(val);
|
||||
}
|
||||
delayMicroseconds(5);
|
||||
}
|
||||
|
||||
26
lib/RotaryEncoder/license.txt
Normal file
26
lib/RotaryEncoder/license.txt
Normal file
@ -0,0 +1,26 @@
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2012, Joshua Oster-Morris
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
Reference in New Issue
Block a user