-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathRAK14006_Rotary_Encoder.ino
67 lines (61 loc) · 1.42 KB
/
RAK14006_Rotary_Encoder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
@file RAK14006_Rotary_Encoder.ino
@author rakwireless.com
@brief Rotary Encoder Example
@version 0.1
@date 2021-7-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#define ENCODER_A_PIN WB_IO6 //clockwise
#define ENCODER_B_PIN WB_IO4 //anticlockwise
#define SWITCH_PIN WB_IO5 //press key
//position is 0 when start up
long position = 0;
bool press_flag = false;
bool clockwise_flag = false;
bool anticlockwise_flag = false;
void setup(){
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
//setup our pins
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT);
attachInterrupt(SWITCH_PIN, handle_1, FALLING);
attachInterrupt(ENCODER_A_PIN, handle_2, RISING);
attachInterrupt(ENCODER_B_PIN, handle_3, RISING);
//setup our serial
Serial.begin(115200);
Serial.println("You can press and twist the button!!");
}
void loop(){
if(press_flag==true)
{
press_flag = false;
Serial.println("press key");
}
if(clockwise_flag==true)
{
clockwise_flag = false;
position++;
Serial.print("position step is ");
Serial.println(position);
}
if(anticlockwise_flag==true)
{
anticlockwise_flag = false;
position--;
Serial.print("position step is ");
Serial.println(position);
}
}
void handle_1(){
press_flag = true;
}
void handle_2(){
clockwise_flag = true;
}
void handle_3(){
anticlockwise_flag = true;
}