Arduino开发实例-旋转编码器RGB-LED调光

旋转编码器RGB-LED调光

在本文中,将使用 Arduino 和旋转编码器进行 RGB LED 颜色控制。 我们将旋转旋转编码器来分配值。 红色、绿色和蓝色将合并以显示基于该值的全新颜色。 在这里,使用具有红色、绿色和蓝色的单 RGB 颜色 LED,也可以使用长 RGB LED 灯条。 此外,还可以使用任何 RGB 颜色 LED 来使用此程序代码调节颜色。

1、旋转编码器介绍

旋转编码器也称为轴编码器。 它是一种机电设备,可将轴或轴的位置或运动转换为模拟或数字输出信号。 旋转编码器主要有两种:

  • 绝对编码器
  • 增量编码器

在这里插入图片描述

绝对编码器的输出显示轴位置,使其成为角度传感器。 增量编码器的输出提供有关轴运动的信息,这些信息通常在其他地方处理成位置、速度和距离等信息。

旋转编码器如何工作

编码器具有一个带有均匀间隔接触区域的圆盘,该圆盘连接到公共引脚 C 和另外两个单独的触点引脚 A 和 B,如下所示:

在这里插入图片描述

当圆盘开始逐步转动时,A、B脚开始接触公共脚,从而分别产生两个方波输出信号。

如果我们只计算信号的脉冲数,则两个输出中的任何一个都将用于确定旋转位置。 但是,如果我们也想演示旋转方向,我们想同时考虑两个信号。

我们可以认识到,它使两个输出信号彼此相差 90 度。 如果编码器顺时针旋转输出,A 将在输出 B 之前。

在这里插入图片描述

因此,如果我们每次信号变化时计算步数,从高到低或从低到高,我们会注意到此时两个输出信号具有相反的值。 反之亦然,如果编码器逆时针旋转,则输出信号具有相等的值。 因此,看到这个,我们可以轻松地对控制器进行编程以读取编码器位置和旋转方向。

关于旋转编码器的驱动,在前面的文章中做了详细的介绍,请参考:

2、硬件接线

本次实例的硬件接线如下:

在这里插入图片描述

本次实例的硬件列表如下:

序号 配件名称 数量
1 Arduino NANO 开发板 1
2 360度旋转编码器 1
3 5MM RGB LED 1
4 连接线 10
5 面包板 1

3、代码实现

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
int encPin1 = 2; //Left turn of encoder to digital pin 2
int encPin2 = 3; //Right turn of encoder to digital pin 3
int SencPin = 4; //Push Encoder switch to digital pin 4
int ledPin = 13; // onboard LED for validation
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
// Define Variables
int stateNum = 0; // current State number
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int RGBVal = 0; //Value for RGB Blend
static boolean moving = false;
volatile int encVal = 0;
unsigned int lastEncVal = 1;
boolean enc1 = false;
boolean enc2 = false;

void setup()
{
//Define Inputs
pinMode(ledPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
pinMode(encPin1, INPUT);
pinMode(encPin2, INPUT);
pinMode(SencPin, INPUT);
//Turn on resistors for encoder/switch
digitalWrite(encPin1, HIGH);
digitalWrite(encPin2, HIGH);
digitalWrite(SencPin, HIGH);
Serial.begin(9600); //Start Logging
//encoder interrupts
attachInterrupt(0, intrEncChange1, CHANGE);
attachInterrupt(1, intrEncChange2, CHANGE);
}

void intrEncChange1() //Read on interrupt Right turn - Fast +4
{
if(moving)
delay(1);
if(digitalRead(encPin1) == enc1)
return;
enc1 = !enc1;
if(enc1 && !enc2)
encVal += 4;
moving = false;
}

void intrEncChange2() //Read on interrupt Left turn - Slow -2
{
if(moving)
delay(1);
if(digitalRead(encPin2) == enc2)
return;
enc2 = !enc2;
if(enc2 && !enc1)
encVal -= 2;
moving = false;
}

void loop()
{
{
buttonState = digitalRead(SencPin); //Read Button state
if (buttonState != lastButtonState) //Compare to last state
{
if (buttonState == HIGH) //If button is pushed, incriment stateNum and blink onboard LED
{
stateNum++;
if (stateNum > 8) stateNum = 0; //Defines and loops Number of possible states
Serial.println("on");
Serial.print("State Number: ");
Serial.println(stateNum);
digitalWrite(ledPin, HIGH);
}
else
{
Serial.println("off");
digitalWrite(ledPin, LOW);
}

lastButtonState = buttonState;
} //update button state to loop
//delay(20); //Delay to avoid bounce
}
if (encVal > 255) encVal = 0; //Loop Values
if (encVal < 0) encVal = 255;
if (encVal != lastEncVal) //Compare to previous pot value
{
lastEncVal = encVal; //Update encVal to reflect new value
Serial.print("Encoder Value: ");
Serial.println(encVal);
digitalWrite(ledPin, HIGH); //Blink onboard LED
}
else
{
digitalWrite(ledPin, LOW);
//delay(100); //Delay to avoid bounce
}

//Begin States section

if (stateNum == 0) //All LEDs make white, off to brightest
{
Serial.println("0 White");
grnVal = 255 - encVal; //Common Anode LED means inverse values
bluVal = 255 - encVal;
redVal = 255 - encVal;
}
else if (stateNum == 1) //Blend Green to Blue
{
Serial.println("1 Green to Blue");
grnVal = encVal;
bluVal = 255 - encVal;
redVal = 255;
}
else if (stateNum == 2) //Blend Blue to Red
{
Serial.println("2 Blue to Red");
bluVal = encVal;
redVal = 255 - encVal;
grnVal = 255;
}
else if (stateNum == 3) //Blend Red to Green
{
Serial.println("3 Red to Green");
redVal = encVal;
grnVal = 255 - encVal;
bluVal = 255;
}
else if (stateNum == 4) //Blend RGB
{
Serial.println("4 Blend All R-G-B-R");
if (encVal < 86) // Lowest third of range (0-85)
{
RGBVal = (encVal \* 3) ; // Normalize to 0-255

redVal = RGBVal; // Red from full to off
grnVal = 255 - RGBVal; // Green from off to full
bluVal = 255; // Blue off
}
else if (encVal < 171) // Middle third of range (86-170)
{
RGBVal = ( (encVal - 86) \* 3); // Normalize to 0-255

redVal = 255; // Red off
grnVal = RGBVal; // Green from full to off
bluVal = 255 - RGBVal; // Blue from off to full
}
else // Upper third of range (171-255)
{
RGBVal = ( (encVal - 171) \* 3); // Normalize to 0-255

redVal = 255 - RGBVal; // Red from off to full
grnVal = 255; // Green off
bluVal = RGBVal; // Blue from full to off
}
}
else if (stateNum == 5) //Red Dim
{
Serial.println("5 Red Dim");
redVal = 255 - encVal;
grnVal = 255;
bluVal = 255;
}
else if (stateNum == 6) //Green Dim
{
Serial.println("6 Green Dim");
redVal = 255;
grnVal = 255 - encVal;
bluVal = 255;
}
else if (stateNum == 7) //Blue Dim
{
Serial.println("7 Blue Dim");
redVal = 255;
grnVal = 255;
bluVal = 255 - encVal;
}
else if (stateNum == 8) //Purple Dim
{
Serial.println("8 Purple Dim");
redVal = 255 - encVal;
grnVal = 255;
bluVal = 255 - encVal;
}

//Send results to LEDs
analogWrite(redPin, redVal);
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
}

运行结果:

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/128130027