author

ChuckM43

Inbox View Profile
2Comments

Tell us about yourself!

  • Rotary Encoder Using Arduino Nano

    Hello All,The lack of details was bugging me... So, I made a sketch that will work for this annoying post. Hope it helps someone!!! (FYI - This is my first Arduino project so I am assuming there are flaws, but it works.)//SKETCH STARTS BELOWint pinA = 6;int pinB = 7;int pinALast;int counter = 0; //Min of 0 and Max of 19 due to one full encoder turnunsigned long currentTime;unsigned long loopTime;unsigned char encoder_A;unsigned char encoder_B;unsigned char encoder_A_prev=0;void setup() { pinMode (pinA,INPUT); pinMode (pinB,INPUT); pinALast = digitalRead(pinA); Serial.begin (9600); currentTime = millis(); loopTime = currentTime; Serial.print("Let's get started - Rotary Encoder is at position "); Serial.print(counter); Serial.println(); }void loop() { // get the cur…

    see more »

    Hello All,The lack of details was bugging me... So, I made a sketch that will work for this annoying post. Hope it helps someone!!! (FYI - This is my first Arduino project so I am assuming there are flaws, but it works.)//SKETCH STARTS BELOWint pinA = 6;int pinB = 7;int pinALast;int counter = 0; //Min of 0 and Max of 19 due to one full encoder turnunsigned long currentTime;unsigned long loopTime;unsigned char encoder_A;unsigned char encoder_B;unsigned char encoder_A_prev=0;void setup() { pinMode (pinA,INPUT); pinMode (pinB,INPUT); pinALast = digitalRead(pinA); Serial.begin (9600); currentTime = millis(); loopTime = currentTime; Serial.print("Let's get started - Rotary Encoder is at position "); Serial.print(counter); Serial.println(); }void loop() { // get the current elapsed time currentTime = millis(); if(currentTime >= (loopTime + 5)){ encoder_A = digitalRead(pinA); // Read encoder pins encoder_B = digitalRead(pinB); if((!encoder_A) && (encoder_A_prev)){ // A has gone from high to low if(encoder_B) { // B is high so counter-clockwise counter --; if(counter < 0){ counter = 20; Serial.print("Turned Counter Clockwise: Position reseting to "); Serial.print(counter); Serial.println(); } else { Serial.print("Turned Counter Clockwise: Position decreased to "); Serial.print(counter); Serial.println(); } } else { // B is low so clockwise counter ++; if(counter > 19){ counter = 0; Serial.print("Turned Clockwise: Position reseting to "); Serial.print(counter); Serial.println(); } else { Serial.print("Turned Clockwise: Position increased to "); Serial.print(counter); Serial.println(); } } } encoder_A_prev = encoder_A; // Store value of A for next time loopTime = currentTime; // Updates loopTime }}

    View Instructable »