Initial import
[experiments/Hall_effect_rotary_encoder.git] / show_quadrature_states / show_quadrature_states.ino
1 /*
2  * Show quadrature states of two Hall effect sensors.
3  *
4  * Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
5  * SPDX-License-Identitier: WTFPL
6  */
7
8 #define HALL_CHANNEL_A 2
9 #define HALL_CHANNEL_B 3
10 #define LED_PIN 13
11
12 int hall_state_A = 0;
13 int hall_state_B = 0;
14
15 int old_hall_state_A = -1;
16 int old_hall_state_B = -1;
17
18 void setup()
19 {
20         pinMode(HALL_CHANNEL_A, INPUT);
21         pinMode(HALL_CHANNEL_B, INPUT);
22         pinMode(LED_PIN, OUTPUT);
23         Serial.begin(115200);
24 }
25
26 void loop()
27 {
28         hall_state_A = digitalRead(HALL_CHANNEL_A);
29         hall_state_B = digitalRead(HALL_CHANNEL_B);
30
31         if (hall_state_A != old_hall_state_A ||
32             hall_state_B != old_hall_state_B)
33         {
34                 digitalWrite(LED_PIN, hall_state_A);
35
36                 old_hall_state_A = hall_state_A;
37                 old_hall_state_B = hall_state_B;
38
39                 Serial.print(hall_state_A);
40                 Serial.print(" ");
41                 Serial.print(hall_state_B);
42                 Serial.print("\r\n");
43         }
44 }