Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Rotary Encoder example code, modified to
- * read 2 rotary encoders (A,B) and thier switches.
- * https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/
- */
- //Will need to change how images get drawn using the EthernatCleint,
- // Rotary Encoder Inputs
- #define CLKA 2
- #define DTA 3
- #define SWA 4
- #define CLKB 5
- #define DTB 6
- #define SWB 7
- int Acounter = 0;
- int Bcounter = 0;
- int AcurrentStateCLK;
- int BcurrentStateCLK;
- int AlastStateCLK;
- int BlastStateCLK;
- String AcurrentDir =""; //just easier than cleverly combining this string for seprate IO
- String BcurrentDir ="";
- unsigned long AlastButtonPress = 0;
- unsigned long BlastButtonPress = 0;
- /*Function Defs*/
- void rotorSense();
- void rotorA();
- void rotorB();
- void setup() {
- // Set encoder pins as inputs
- pinMode(CLKA,INPUT);
- pinMode(DTA,INPUT);
- pinMode(SWA, INPUT_PULLUP);
- pinMode(CLKB,INPUT);
- pinMode(DTB,INPUT);
- pinMode(SWB, INPUT_PULLUP);
- // Setup Serial Monitor
- Serial.begin(9600);
- // Read the initial state of CLK
- AlastStateCLK = digitalRead(CLKA);
- BlastStateCLK = digitalRead(CLKB);
- }
- void loop() {
- rotorSense();
- //rotorA();
- //rotorB();
- // Put in a slight delay to help debounce the reading
- delay(1);
- } //end loop
- void rotorA(){
- // Read the current state of CLK
- AcurrentStateCLK = digitalRead(CLKA);
- // If last and current state of CLK are different, then pulse occurred
- // React to only 1 state change to avoid double count
- if (AcurrentStateCLK != AlastStateCLK && AcurrentStateCLK == 1){
- // If the DT state is different than the CLK state then
- // the encoder is rotating CCW so decrement
- if (digitalRead(DTA) != AcurrentStateCLK) {
- Acounter --;
- AcurrentDir ="CCW";
- } else {
- // Encoder is rotating CW so increment
- Acounter ++;
- AcurrentDir ="CW";
- }
- Serial.print("A Direction: ");
- Serial.print(AcurrentDir);
- Serial.print(" | Counter: ");
- Serial.println(Acounter);
- }
- int AbtnState = digitalRead(SWA);
- //If we detect LOW signal, button is pressed
- if (AbtnState == LOW) {
- //if 50ms have passed since last LOW pulse, it means that the
- //button has been pressed, released and pressed again
- if (millis() - AlastButtonPress > 500) {
- Serial.println("Button A pressed!");
- }
- AlastButtonPress = millis();
- }
- // Remember last CLK state
- AlastStateCLK = AcurrentStateCLK;
- //endA
- }
- void rotorB(){
- BcurrentStateCLK = digitalRead(CLKB);
- // If last and current state of CLK are different, then pulse occurred
- // React to only 1 state change to avoid double count
- if (BcurrentStateCLK != BlastStateCLK && BcurrentStateCLK == 1){
- // If the DT state is different than the CLK state then
- // the encoder is rotating CCW so decrement
- if (digitalRead(DTB) != BcurrentStateCLK) {
- Bcounter --;
- BcurrentDir ="CCW";
- } else {
- // Encoder is rotating CW so increment
- Bcounter ++;
- BcurrentDir ="CW";
- }
- Serial.print("B Direction: ");
- Serial.print(BcurrentDir);
- Serial.print(" | Counter: ");
- Serial.println(Bcounter);
- }
- BlastStateCLK = BcurrentStateCLK;
- // Read the button state
- int BbtnState = digitalRead(SWB);
- if (BbtnState == LOW) {
- //if 50ms have passed since last LOW pulse, it means that the
- //button has been pressed, released and pressed again
- if (millis() - BlastButtonPress > 50) {
- Serial.println("Button B pressed!");
- }
- // Remember last button press event
- BlastButtonPress = millis();
- }
- //endB
- }
- void rotorSense(){
- rotorA();
- rotorB();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement