segunda-feira, 25 de janeiro de 2016

Encoder EC16-2 - Arquivo 008

Referência:
http://bildr.org/2012/08/rotary-encoder-arduino/
http://www.multiwingspan.co.uk/arduino.php?page=rotary2

Lista de Materiais:
IDE Arduino 1.0.6
1x  Board Arduino UNO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-B male 
1x  Conj. de fios jumper p/ protoboard
1x  Encoder EC16-2 NHBY

Montagem:





Firmware 01 - Com utilização de Interrupção:
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;

volatile long encoderValue = 0;

long lastencoderValue = 0;


int lastMSB = 0;

int lastLSB = 0;
//**************************************************************************** 
void setup() {
  Serial.begin (9600);

  pinMode(encoderPin1, INPUT); 

  pinMode(encoderPin2, INPUT);

  digitalWrite(encoderPin1, HIGH);                //turn pullup resistor on

  digitalWrite(encoderPin2, HIGH);                //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen

  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 
  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);
}
//**************************************************************************** 
void loop(){ 
  Serial.println(encoderValue);
  delay(500); 
}
//**************************************************************************** 
void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2);  //LSB = least significant bit

  int encoded = (MSB << 1) |LSB;        //converting the 2 pin value to single number

  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;

  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

  lastEncoded = encoded;                      //store this value for next time

}

Firmware 02:
int reading = 0;
int lowest = -12;
int highest = 12;
int changeamnt = 1;

unsigned long currentTime;
unsigned long lastTime;

const int pinA = 2;
const int pinB = 3;

boolean encA;
boolean encB;
boolean lastA = false;
//****************************************************************************
void setup() {
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  currentTime = millis();
  lastTime = currentTime; 
  Serial.begin(9600);

//****************************************************************************
void loop() {
  currentTime = millis(); 
  if(currentTime >= (lastTime + 5)){
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    if ((!encA) && (lastA)){
      if (encB){
        if (reading + changeamnt <= highest){
          reading = reading + changeamnt; 
        }
      } else {
        if (reading - changeamnt >= lowest){
          reading = reading - changeamnt; 
        }
      }
      Serial.println(reading);
    }
    lastA = encA;
    lastTime = currentTime;
  }

}

Download: não necessário

Nenhum comentário:

Postar um comentário