====== Encodeur ====== Capteur optique qui permet de connaître la position, l'angle, la vitesse d'un moteur ou d'un capteur rotatif.([[https://en.wikipedia.org/wiki/Rotary_encoder|wikipedia]]) ===== Encodeur rotatif ===== * Source : [[http://bildr.org/2012/08/rotary-encoder-arduino/|/rotary-encoder-arduino]] * Une bibliothèque "Encoder" est disponible sur Arduino {{:materiel:encoder:rotary_encoder_arduino_hookup-400x335.png?400|}} {{:materiel:encoder:rotary_encoder_switch_arduino_hookup.png?400|}} Avec un bouton ===== Encodeur rotatif Keyes KY-040 ===== * Source : [[http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/|keyes-ky-040-arduino-rotary-encoder]] {{:materiel:encoder:keyes-ky-040-rotary-encoder-arduino-connections.png?400|}} 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 ===== * Source : http://www.bristolwatch.com/arduino/arduino2.htm /* 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); } }