Pages: [1]   Go Down
Author Topic: Rotary Encoder, A different approach using one analog Pin  (Read 663 times)
0 Members and 1 Guest are viewing this topic.
Greece
Offline Offline
Newbie
*
Karma: 1
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I connected a rotary encoders grey code Com (center pin) to A0, grey code pin 1 to arduino GRD and a 64k resistor to encoders gray code pins 1 and 2.
then I enabled A0 internal pullup resistor and wrote a code to determine whether the encoder turned left or write and used timer one to check for input changes It seems to be working well
this is the code I used it requires timerone library http://playground.arduino.cc/code/timer1

#include <TimerOne.h>
boolean gotHigh=false; //Used to check if it went to High value
boolean gotLow=false; //Used to Check if it went to Low value
boolean valueHigh=false; //Used to check if the last value is high or low to determine the rotation
boolean hasTurned=false;

void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP); //Set the internal pullup resistor
Timer1.initialize(1000);  //Set the interval of the checks
Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}
 
void loop()
{
  // Your code here
  

}
 

void timerIsr(){
int sensorValue = analogRead(A0);    
  if (sensorValue<950){
    if (sensorValue>400){valueHigh=true;
      gotHigh=true;
    }
    else {
    valueHigh=false;
    gotLow=true;
    }
  hasTurned=true;  
  }
  else if (hasTurned==true){
  hasTurned=false;
    if ((valueHigh==true) && (gotLow==true)){
      Serial.println("Left"); //you code here for left turns eg. Myval--
    }
    else if (gotHigh==true){
      Serial.println("Right");//you code here for left turns eg. Myval++
    }
  gotHigh=false;
  gotLow=false;
  }
}


* DSC_0313.jpg (1495.19 KB, 2592x1944 - viewed 42 times.)

* DSC_0314.jpg (1304.22 KB, 2592x1944 - viewed 34 times.)

* Rotary.jpg (34.23 KB, 846x658 - viewed 28 times.)
« Last Edit: July 16, 2013, 04:14:10 pm by billmax3 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool.  I'm playing with a pec11 right now.  I saw a youtube article about wiring it up with a debounce circuit and it was not working.  I'm using a 328 so my inputs are limited.  I will be watching your work here and trying something similar my self.

Cheers
Logged

Greece
Offline Offline
Newbie
*
Karma: 1
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a picture
https://docs.google.com/file/d/0B7psmNlbHxTGR2F1a3o1VW92RlE/edit?usp=sharing
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Neat! You made a 2-bit digital-to-analog converter.
Logged


Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2892
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Neat! You made a 2-bit digital-to-analog converter comparator..
Two analog values 3V or zero.. assuming a 20K pull-up resistor.

Doc
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Pages: [1]   Go Up
Jump to: