Skip to content

Commit c08475e

Browse files
committed
Start doing some simple PWM on the RGB LED
1 parent 58e8087 commit c08475e

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

rotary_encoder/rotary_encoder.ino

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
* Driver for incremental rotary encoder w/support for pushbutton RGB LED.
2+
* Driver for incremental rotary encoder w/built-in pushbutton and RGB LED.
3+
*
34
*
45
* Objective:
56
*
@@ -46,7 +47,8 @@
4647
* - Encoder pin 3 to Arduino pin 4 with a 10K pull-down resistor
4748
* (pushbutton input).
4849
* - Encoder pins 4, 2 and 1 through current limiting resistors and on to
49-
* Arduino pins 9, 10 and 11, respectively (PWM outputs for RGB LED).
50+
* Arduino pins 9, 10 and 11, respectively (PWM outputs for RGB LED
51+
* hooked up to PB1/OC1A, PB2/OC1B and PB3/OC2A, respectively).
5052
*
5153
* Diagram:
5254
*
@@ -65,14 +67,19 @@
6567
* | 5|----------|Vcc |
6668
* --- -----
6769
*
68-
* R1-R3: Current-limiting resistors, e.g. 330Ω
70+
* R1-R3: Current-limiting resistors, e.g. 220Ω
6971
* R4: Pull-down resistor, e.g. 10KΩ
7072
*
73+
*
74+
* Mode of operation:
75+
*
7176
* In the Arduino, the two rotary encoder inputs and the pusbutton input
72-
* trigger interrupts whose handler merely forwards the current state of
73-
* the inputs to the main loop by using a simple ring buffer. This keeps
74-
* the ISR very short and fast. The three PWM outputs are driven using
75-
* analogWrite() from the main loop.
77+
* trigger a pin change interrupt (PCINT2). The corresponding ISR merely
78+
* forwards the current state of the input pins to the main loop by using
79+
* a simple ring buffer. This keeps the ISR very short and fast, and
80+
* ensures that we miss as few interrupts as possible.
81+
*
82+
* The three PWM outputs are driven using analogWrite() from the main loop.
7683
*
7784
* Author: Johan Herland <johan@herland.net>
7885
* License: GNU GPL v2 or later
@@ -85,7 +92,8 @@ byte rb_read; // current read position in ringbuffer
8592
byte rot_state = 0;
8693
bool button_state = 0;
8794

88-
int rot_value = 0;
95+
byte rot_value = 0;
96+
const byte rot_increment = 4;
8997

9098
void setup(void)
9199
{
@@ -95,14 +103,17 @@ void setup(void)
95103

96104
// Set up input pins (Arduino pins 2/3/4 == PORTD pins 2/3/4).
97105
// Set PD2-4 as inputs:
98-
DDRD &= ~(_BV(DDD2) | _BV(DDD3) | _BV(DDD4));
106+
DDRD &= ~B00011100;
99107
// Enable PD2-3 internal pull-up resistors
100-
PORTD |= _BV(PORTD2) | _BV(PORTD3);
108+
PORTD |= B00001100;
101109

102110
// Set up PCINT18..20 interrupt to trigger on changing pins 2/3/4.
103111
PCICR = B00000100; // - - - - - PCIE2 PCIE1 PCIE0
104112
PCMSK2 = B00011100; // PCINT23 .. PCINT16
105113

114+
// Set up pins 9/10/11 (PB1..3) as output (for PWM)
115+
DDRB |= B00001110;
116+
106117
sei(); // Re-enable interrupts
107118

108119
Serial.println(F("Ready"));
@@ -111,7 +122,7 @@ void setup(void)
111122
/*
112123
* PCINT2 interrupt vector
113124
*
114-
* Append the current values of the relevant input pins to the ring buffer.
125+
* Append the current values of the relevant input port to the ring buffer.
115126
*/
116127
ISR(PCINT2_vect)
117128
{
@@ -179,13 +190,20 @@ void loop(void)
179190
Serial.println(F("^"));
180191

181192
if (events & ROT_CW) {
182-
rot_value++;
193+
rot_value += rot_increment;
183194
Serial.print(F("-> "));
184195
Serial.println(rot_value);
185196
}
186197
else if (events & ROT_CCW) {
187-
rot_value--;
198+
rot_value -= rot_increment;
188199
Serial.print(F("<- "));
189200
Serial.println(rot_value);
190201
}
202+
203+
// TODO: New CL resistors
204+
// TODO: R/G/B modes
205+
// TODO: acceleration in rotation
206+
analogWrite( 9, 0xff - rot_value);
207+
analogWrite(10, 0xff - rot_value);
208+
analogWrite(11, 0xff - rot_value);
191209
}

0 commit comments

Comments
 (0)