source: t29-www/physical-computing/encoder-mit-interrupt/encoder-mit-interrupt.ino @ 1172

Last change on this file since 1172 was 1172, checked in by heribert, 7 years ago

Encoder-Programm korrigiert

File size: 1.2 KB
Line 
1/*
2 * Rotary-Encoder Sketch mit Interrupt Nr. 2
3 * beachte: Bei schnellen Vorgaengen verlangsamt der Serielle-Monitor den Sketch!
4 * Wenn der Button (Switch) zum Rueckstellen ins Programm eingebaut wird, muss
5 * man den INPUT mit PULLUP versehen!
6 *
7 * read a rotary encoder with interrupts
8   Encoder hooked up with common to GROUND,
9   encoder0PinA to pin 2, encoder0PinB to pin 3
10   it doesn't matter which encoder pin you use for A or B 
11
12   uses Arduino pull-ups on A & B channel outputs
13   turning on the pull-ups saves having to hook up resistors
14   to the A & B channel outputs
15*/
16
17#define encoder0PinA  2
18#define encoder0PinB  3
19
20volatile int encoder0Pos = 0;
21
22void setup() {
23 
24  pinMode(encoder0PinA, INPUT);
25  pinMode(encoder0PinB, INPUT);
26 
27  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
28  Serial.begin (9600);
29  Serial.println("Start des Programms");               
30}
31
32void loop(){   
33
34}
35}
36void doEncoder() {
37  /* If pinA and pinB are both high or both low, it is spinning
38   * forward. If they're different, it's going backward.
39   */
40  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
41    encoder0Pos++;
42  } else {
43    encoder0Pos--;
44  }
45   Serial.print("    ");
46  Serial.println (encoder0Pos);
47}
48
49 
50
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License