Forum: Mikrocontroller und Digitale Elektronik Sketch Arduino Ky-040 auslesen


von Sebastian Z. (xhamster)


Lesenswert?

In dem Sketch Arduino Ky 040 auslesen ist die Code Zeile (d=c^d);.

Was sagt diese Zeile?

1
int clock = 2;             // Define encoder pin A
2
int data = 3;              // Define encoder pin B
3
int count = 0;             // pre-init the count to zero
4
int c = LOW;               // pre-init the state of pin A low
5
int cLast = LOW;           // and make its last val the same - ie no change
6
int d = LOW;               // and make the data val low as well
7
 
8
void setup() {
9
  pinMode (clock,INPUT);  // setup the pins as inputs
10
  pinMode (data,INPUT);
11
  Serial.begin (9600);    // and give some serial debugging
12
}
13
 
14
void loop() {
15
  c = digitalRead(clock); // read pin A as clock
16
  d = digitalRead(data);  // read pin B as data
17
 
18
  if (c != cLast) {       // clock pin has changed value... now we can do stuff
19
    d = c^d;              // work out direction using an XOR
20
    if ( d ) {
21
      count--;            // non-zero is Anti-clockwise
22
    }else{
23
      count++;            // zero is therefore anti-clockwise
24
    }
25
    Serial.print ("Jog:: count:");
26
    Serial.println(count);
27
    cLast = c;            // store current clock state for next pass
28
  }
29
}

: Verschoben durch User
von Sebastian R. (sebastian_r569)


Lesenswert?

Das ist d = c XOR d, also Exklusiv-Oder.

Dadurch lässt sich bei einem Encoder die Drehrichtung erkennen.

von Sebastian Z. (xhamster)


Lesenswert?

Sebastian R. schrieb:
> Das ist d = c XOR d, also Exklusiv-Oder.
>
> Dadurch lässt sich bei einem Encoder die Drehrichtung erkennen.

Danke.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.