Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Arduino 3 rotary encoder's 0-255 Created by Matthew AKA Asmodeus
- //Outputs serial data Encoder1,Encoder2,Encoder3
- //Rotary Encoder Max/Min
- int maximum = 255;
- int minimum = 0;
- //Max-Min are the only values you need to change
- int encoderRPinA = 9; //clk //ENCODER1
- int encoderRPinB = 10; //dt //ENCODER1
- int encoderGPinA = 6; //clk //ENCODER2
- int encoderGPinB = 7; //dt //ENCODER2
- int encoderBPinA = 3; //clk //ENCODER3
- int encoderBPinB = 4; //dt //ENCODER3
- int encoderRPinALast = LOW;
- int encoderGPinALast = LOW;
- int encoderBPinALast = LOW;
- int n = LOW;
- int b = LOW;
- int m = LOW;
- int encoderRPos = 0;
- int encoderGPos = 0;
- int encoderBPos = 0;
- int encoderRPosLast = 0;
- int encoderGPosLast = 0;
- int encoderBPosLast = 0;
- //Buttons (NOT INTERGRATED)
- int Rpush = 8; //Encoder1 sw
- int Gpush = 5; //Encoder2 sw
- int Bpush = 2; //Encoder3 sw
- void setup() {
- //Buttons (NOT INTERGRATED)
- pinMode (Rpush,INPUT);
- pinMode (Gpush,INPUT);
- pinMode (Bpush,INPUT);
- //Encoders
- pinMode (encoderRPinA,INPUT);
- pinMode (encoderRPinB,INPUT);
- pinMode (encoderGPinA,INPUT);
- pinMode (encoderGPinB,INPUT);
- pinMode (encoderBPinA,INPUT);
- pinMode (encoderBPinB,INPUT);
- Serial.begin (115200);
- }
- void loop() {
- n = digitalRead(encoderRPinA);
- b = digitalRead(encoderGPinA);
- m = digitalRead(encoderBPinA);
- if ((encoderRPinALast == LOW) && (n == HIGH)) {
- if (digitalRead(encoderRPinB) == LOW) {
- if(encoderRPos <= minimum) {
- encoderRPos = minimum;
- }
- else {
- encoderRPos--;
- }
- }
- else {
- if(encoderRPos >= maximum) {
- encoderRPos = maximum;
- }
- else {
- encoderRPos++;
- }
- }
- }
- if ((encoderGPinALast == LOW) && (b == HIGH)) {
- if (digitalRead(encoderGPinB) == LOW) {
- if(encoderGPos <= minimum) {
- encoderGPos = minimum;
- }
- else {
- encoderGPos--;
- }
- }
- else {
- if(encoderGPos >= maximum) {
- encoderGPos = maximum;
- }
- else {
- encoderGPos++;
- }
- }
- }
- if ((encoderBPinALast == LOW) && (m == HIGH)) {
- if (digitalRead(encoderBPinB) == LOW) {
- if(encoderBPos <= minimum) {
- encoderBPos = minimum;
- }
- else {
- encoderBPos--;
- }
- }
- else {
- if(encoderBPos >= maximum) {
- encoderBPos = maximum;
- }
- else {
- encoderBPos++;
- }
- }
- }
- if(encoderRPos != encoderRPosLast || encoderGPos != encoderGPosLast || encoderBPos != encoderBPosLast)
- {
- Serial.println ((String)encoderRPos + "," + (String)encoderGPos + "," + (String)encoderBPos);
- encoderRPosLast = encoderRPos;
- encoderGPosLast = encoderGPos;
- encoderBPosLast = encoderBPos;
- }
- encoderRPinALast = n;
- encoderGPinALast = b;
- encoderBPinALast = m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement