Devices & Components
Arduino Uno Rev3
Rotary Encoder with Push-Button
Software & Tools
Arduino IDE
Project description
Code
CODE
arduino
1/************** 2 * VCC to 5V * 3 * GND to GND * 4 * CLK to D3 5 * 6 * CLK to D4 * 7 **************/ 8int pinA = 3; 9int pinB = 4; 10int 11 encoderPosCount = 0; 12int pinALast; 13int aVal; 14boolean bCW; 15 16 void 17 setup() 18 { 19 //SET pinA and pinB and input 20 pinMode (pinA,INPUT); 21 22 pinMode (pinB,INPUT); 23 pinALast = digitalRead(pinA);//Read Pin A 24 Serial.begin 25 (9600); 26 Serial.println("BEGIN"); 27 Serial.println(); 28 } 29 30 31 void loop() 32 { 33 aVal = digitalRead(pinA); 34 if (aVal != pinALast) 35 36 { 37 if (digitalRead(pinB) != aVal) //We're Rotating Clockwise 38 { 39 40 encoderPosCount ++; 41 bCW = true; 42 } 43 44 else 45 46 47 { 48 bCW = false; 49 encoderPosCount--; 50 } 51 52 53 if (bCW) 54 { 55 Serial.println ("Rotate Clockwise"); 56 57 } 58 59 else 60 61 { 62 Serial.println("Rotate 63 Counterclockwise"); 64 } 65 66 Serial.print("Encoder Count: "); 67 68 Serial.println(encoderPosCount); 69 Serial.println(); 70 } 71 72 73 pinALast = aVal; 74 }
CODE
arduino
1/************** 2 * VCC to 5V * 3 * GND to GND * 4 * CLK to D3 * 5 * CLK to D4 * 6 **************/ 7int pinA = 3; 8int pinB = 4; 9int encoderPosCount = 0; 10int pinALast; 11int aVal; 12boolean bCW; 13 14 void setup() 15 { 16 //SET pinA and pinB and input 17 pinMode (pinA,INPUT); 18 pinMode (pinB,INPUT); 19 pinALast = digitalRead(pinA);//Read Pin A 20 Serial.begin (9600); 21 Serial.println("BEGIN"); 22 Serial.println(); 23 } 24 25 void loop() 26 { 27 aVal = digitalRead(pinA); 28 if (aVal != pinALast) 29 { 30 if (digitalRead(pinB) != aVal) //We're Rotating Clockwise 31 { 32 encoderPosCount ++; 33 bCW = true; 34 } 35 36 else 37 38 { 39 bCW = false; 40 encoderPosCount--; 41 } 42 43 if (bCW) 44 { 45 Serial.println ("Rotate Clockwise"); 46 } 47 48 else 49 50 { 51 Serial.println("Rotate Counterclockwise"); 52 } 53 54 Serial.print("Encoder Count: "); 55 Serial.println(encoderPosCount); 56 Serial.println(); 57 } 58 59 pinALast = aVal; 60 }
Downloadable files
CONNECTIONS
CONNECTIONS

Documentation
CONNECTIONS
CONNECTIONS

CONNECTIONS
CONNECTIONS

Comments
Only logged in users can leave comments