Advertisement
AsmodHacker

Arduino 3 rotary encoder's 0-255

Mar 17th, 2015
1,440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //Arduino 3 rotary encoder's 0-255 Created by Matthew AKA Asmodeus
  2.  
  3. //Outputs serial data Encoder1,Encoder2,Encoder3
  4.  
  5. //Rotary Encoder Max/Min
  6. int maximum = 255;
  7. int minimum = 0;
  8. //Max-Min are the only values you need to change
  9.  
  10. int encoderRPinA = 9; //clk //ENCODER1
  11. int encoderRPinB = 10; //dt //ENCODER1
  12. int encoderGPinA = 6; //clk //ENCODER2
  13. int encoderGPinB = 7; //dt //ENCODER2
  14. int encoderBPinA = 3; //clk //ENCODER3
  15. int encoderBPinB = 4; //dt //ENCODER3
  16.  
  17. int encoderRPinALast = LOW;
  18. int encoderGPinALast = LOW;
  19. int encoderBPinALast = LOW;
  20.  
  21. int n = LOW;
  22. int b = LOW;
  23. int m = LOW;
  24.  
  25. int encoderRPos = 0;
  26. int encoderGPos = 0;
  27. int encoderBPos = 0;
  28. int encoderRPosLast = 0;
  29. int encoderGPosLast = 0;
  30. int encoderBPosLast = 0;
  31.  
  32. //Buttons (NOT INTERGRATED)
  33. int Rpush = 8; //Encoder1 sw
  34. int Gpush = 5; //Encoder2 sw
  35. int Bpush = 2; //Encoder3 sw
  36.  
  37. void setup() {
  38. //Buttons (NOT INTERGRATED)
  39. pinMode (Rpush,INPUT);
  40. pinMode (Gpush,INPUT);
  41. pinMode (Bpush,INPUT);
  42. //Encoders
  43. pinMode (encoderRPinA,INPUT);
  44. pinMode (encoderRPinB,INPUT);
  45. pinMode (encoderGPinA,INPUT);
  46. pinMode (encoderGPinB,INPUT);
  47. pinMode (encoderBPinA,INPUT);
  48. pinMode (encoderBPinB,INPUT);
  49.  
  50. Serial.begin (115200);
  51. }
  52.  
  53. void loop() {
  54. n = digitalRead(encoderRPinA);
  55. b = digitalRead(encoderGPinA);
  56. m = digitalRead(encoderBPinA);
  57.  
  58. if ((encoderRPinALast == LOW) && (n == HIGH)) {
  59. if (digitalRead(encoderRPinB) == LOW) {
  60. if(encoderRPos <= minimum) {
  61. encoderRPos = minimum;
  62. }
  63. else {
  64. encoderRPos--;
  65. }
  66. }
  67. else {
  68. if(encoderRPos >= maximum) {
  69. encoderRPos = maximum;
  70. }
  71. else {
  72. encoderRPos++;
  73. }
  74. }
  75. }
  76. if ((encoderGPinALast == LOW) && (b == HIGH)) {
  77. if (digitalRead(encoderGPinB) == LOW) {
  78. if(encoderGPos <= minimum) {
  79. encoderGPos = minimum;
  80. }
  81. else {
  82. encoderGPos--;
  83. }
  84. }
  85. else {
  86. if(encoderGPos >= maximum) {
  87. encoderGPos = maximum;
  88. }
  89. else {
  90. encoderGPos++;
  91. }
  92. }
  93. }
  94. if ((encoderBPinALast == LOW) && (m == HIGH)) {
  95. if (digitalRead(encoderBPinB) == LOW) {
  96. if(encoderBPos <= minimum) {
  97. encoderBPos = minimum;
  98. }
  99. else {
  100. encoderBPos--;
  101. }
  102. }
  103. else {
  104. if(encoderBPos >= maximum) {
  105. encoderBPos = maximum;
  106. }
  107. else {
  108. encoderBPos++;
  109. }
  110. }
  111. }
  112.  
  113. if(encoderRPos != encoderRPosLast || encoderGPos != encoderGPosLast || encoderBPos != encoderBPosLast)
  114. {
  115. Serial.println ((String)encoderRPos + "," + (String)encoderGPos + "," + (String)encoderBPos);
  116. encoderRPosLast = encoderRPos;
  117. encoderGPosLast = encoderGPos;
  118. encoderBPosLast = encoderBPos;
  119. }
  120. encoderRPinALast = n;
  121. encoderGPinALast = b;
  122. encoderBPinALast = m;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement