Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

materiel:encoder:accueil

Encodeur

Capteur optique qui permet de connaître la position, l'angle, la vitesse d'un moteur ou d'un capteur rotatif.(wikipedia)

Encodeur rotatif

Avec un bouton

Encodeur rotatif Keyes KY-040

int pinA = 3;  // Connected to CLK on KY-040
int pinB = 4;  // Connected to DT on KY-040
int encoderPosCount = 0; 
int pinALast;  
int aVal;
boolean bCW;
 
void setup() { 
  pinMode (pinA,INPUT);
  pinMode (pinB,INPUT);
  /* Read Pin A
  Whatever state it's in will reflect the last position   
  */
  pinALast = digitalRead(pinA);   
  Serial.begin (9600);
} 
 
void loop() { 
  aVal = digitalRead(pinA);
  if (aVal != pinALast){ // Means the knob is rotating
    // if the knob is rotating, we need to determine direction
    // We do that by reading pin B.
    if (digitalRead(pinB) != aVal) {  // Means pin A Changed first - We're Rotating Clockwise
      encoderPosCount ++;
      bCW = true;
    } else {// Otherwise B changed first and we're moving CCW
      bCW = false;
      encoderPosCount--;
    }
    Serial.print ("Rotated: ");
    if (bCW){
      Serial.println ("clockwise");
    }else{
      Serial.println("counterclockwise");
    }
    Serial.print("Encoder Position: ");
    Serial.println(encoderPosCount);   
  } 
  pinALast = aVal;
 } 

Moteur Encodeur QEDS9500

/* 
Demonstrates use of rotary encoder for motor direction and distance.
*/
 
#define CHA 2
#define CHB 3
#define CW_LED 8
#define CCW_LED 7
 
volatile int master_count = 0; // universal count
volatile byte INTFLAG1 = 0; // interrupt status flag
 
void setup() { 
  pinMode(CHA, INPUT);
  pinMode(CHB, INPUT);
  pinMode(CW_LED, OUTPUT); // LED connected to pin to ground
  pinMode(CCW_LED, OUTPUT); // LED connected to pin to ground
 
  Serial.begin(9600); 
  Serial.println(master_count);
 
  attachInterrupt(0, flag, RISING);  
  // interrupt 0 digital pin 2 positive edge trigger
}
 
void loop() {
 
  if (INTFLAG1)   {
       Serial.println(master_count);
       delay(20);
     INTFLAG1 = 0; // clear flag
} // end if
 
 
} // end loop
 
 
void flag() {
  INTFLAG1 = 1;
  // add 1 to count for CW
  if (digitalRead(CHA) && !digitalRead(CHB)) {
    master_count++ ;
    digitalWrite(CW_LED, HIGH);
    digitalWrite(CCW_LED, LOW);
  }
  // subtract 1 from count for CCW
  if (digitalRead(CHA) && digitalRead(CHB)) {
    master_count-- ;
    digitalWrite(CW_LED, LOW);
    digitalWrite(CCW_LED, HIGH);
  } 
}
/home/resonancg/www/wiki/data/pages/materiel/encoder/accueil.txt · Dernière modification: 2017/03/28 15:20 de resonance