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

Last change on this file since 1171 was 1171, checked in by heribert, 5 years ago

PCR Ergänzungen und Termin Industrie 4.0 gestrichen

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 * 
5 * read a rotary encoder with interrupts
6   Encoder hooked up with common to GROUND,
7   encoder0PinA to pin 2, encoder0PinB to pin 3
8   it doesn't matter which encoder pin you use for A or B 
9
10   uses Arduino pull-ups on A & B channel outputs
11   turning on the pull-ups saves having to hook up resistors
12   to the A & B channel outputs
13*/
14
15#define encoder0PinA  2
16#define encoder0PinB  3
17#define buttonPin 4
18
19volatile int encoder0Pos = 0;
20
21void setup() {
22 
23  pinMode(encoder0PinA, INPUT);
24  pinMode(encoder0PinB, INPUT);
25  pinMode(buttonPin, INPUT_PULLUP);
26
27  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
28  Serial.begin (9600);
29  Serial.println("Start");               
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  // you don't want serial slowing down your program if not needed
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