Simple example of using a rotary encoder to drive an RGB LED. At first, you can control the brightness of the red LED. Push the encoder shaft in and you can then control green. Push again to control blue. Then it repeats.

Dependencies:   mRotaryEncoder mbed

Committer:
wd5gnr
Date:
Thu Dec 29 06:01:32 2016 +0000
Revision:
0:3e6a4dfbcb88
Child:
1:126964580159
Initial comming

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wd5gnr 0:3e6a4dfbcb88 1 #include "mbed.h"
wd5gnr 0:3e6a4dfbcb88 2 #include "mRotaryEncoder.h"
wd5gnr 0:3e6a4dfbcb88 3
wd5gnr 0:3e6a4dfbcb88 4 // Simple demo for Hackaday
wd5gnr 0:3e6a4dfbcb88 5 // Al Williams
wd5gnr 0:3e6a4dfbcb88 6
wd5gnr 0:3e6a4dfbcb88 7 // This require two Keyes modules (or equivalent)
wd5gnr 0:3e6a4dfbcb88 8 // A KY-040 rotary encoder is connected to D7, D8 and the switch to D2
wd5gnr 0:3e6a4dfbcb88 9 // D8 connects to CLK and D7 connects to DT
wd5gnr 0:3e6a4dfbcb88 10 // if the rotation is backwards switch the wiring or swap the definitions in
wd5gnr 0:3e6a4dfbcb88 11 // software
wd5gnr 0:3e6a4dfbcb88 12
wd5gnr 0:3e6a4dfbcb88 13 // There is also a KY-016 RGB LED with integrated resistors
wd5gnr 0:3e6a4dfbcb88 14 // This board plugs in with the ground pin next to D13
wd5gnr 0:3e6a4dfbcb88 15 // then the other pins naturally hit D13, D12, and D11
wd5gnr 0:3e6a4dfbcb88 16
wd5gnr 0:3e6a4dfbcb88 17 PwmOut blueled(D13);
wd5gnr 0:3e6a4dfbcb88 18 PwmOut greenled(D12);
wd5gnr 0:3e6a4dfbcb88 19 PwmOut redled(D11);
wd5gnr 0:3e6a4dfbcb88 20
wd5gnr 0:3e6a4dfbcb88 21 // RGB values for LEDS
wd5gnr 0:3e6a4dfbcb88 22 PwmOut *leds[]={&redled, &greenled, &blueled};
wd5gnr 0:3e6a4dfbcb88 23 float rgb[]={0.0, 0,0, 0.0};
wd5gnr 0:3e6a4dfbcb88 24 int sel=0; // which component are we changing?
wd5gnr 0:3e6a4dfbcb88 25
wd5gnr 0:3e6a4dfbcb88 26 DigitalIn mybutton(USER_BUTTON); // not used here
wd5gnr 0:3e6a4dfbcb88 27
wd5gnr 0:3e6a4dfbcb88 28 // Here's the encoder object
wd5gnr 0:3e6a4dfbcb88 29 mRotaryEncoder enc(D7,D8, D2,PullNone);
wd5gnr 0:3e6a4dfbcb88 30
wd5gnr 0:3e6a4dfbcb88 31
wd5gnr 0:3e6a4dfbcb88 32 // Helper function to set the PWM values
wd5gnr 0:3e6a4dfbcb88 33 void setleds()
wd5gnr 0:3e6a4dfbcb88 34 {
wd5gnr 0:3e6a4dfbcb88 35 for (int i=0;i<sizeof(leds)/sizeof(leds[0]);i++) leds[i]->write(rgb[i]);
wd5gnr 0:3e6a4dfbcb88 36 }
wd5gnr 0:3e6a4dfbcb88 37
wd5gnr 0:3e6a4dfbcb88 38
wd5gnr 0:3e6a4dfbcb88 39 // Library calls here when you go clockwise
wd5gnr 0:3e6a4dfbcb88 40 void cw()
wd5gnr 0:3e6a4dfbcb88 41 {
wd5gnr 0:3e6a4dfbcb88 42 // modify the selected RGB component
wd5gnr 0:3e6a4dfbcb88 43 rgb[sel]+=0.1;
wd5gnr 0:3e6a4dfbcb88 44 if (rgb[sel]>1.0) rgb[sel]=1.0;
wd5gnr 0:3e6a4dfbcb88 45 setleds();
wd5gnr 0:3e6a4dfbcb88 46 }
wd5gnr 0:3e6a4dfbcb88 47
wd5gnr 0:3e6a4dfbcb88 48 // Library calls here when you go anticlockwise
wd5gnr 0:3e6a4dfbcb88 49 void ccw()
wd5gnr 0:3e6a4dfbcb88 50 {
wd5gnr 0:3e6a4dfbcb88 51 // modify the selected RGB component
wd5gnr 0:3e6a4dfbcb88 52 rgb[sel]-=0.1;
wd5gnr 0:3e6a4dfbcb88 53 if (rgb[sel]<0.0) rgb[sel]=0.0;
wd5gnr 0:3e6a4dfbcb88 54 setleds();
wd5gnr 0:3e6a4dfbcb88 55 }
wd5gnr 0:3e6a4dfbcb88 56
wd5gnr 0:3e6a4dfbcb88 57 // Library calls here when you push in on the encoder shaft
wd5gnr 0:3e6a4dfbcb88 58 void btn()
wd5gnr 0:3e6a4dfbcb88 59 {
wd5gnr 0:3e6a4dfbcb88 60 // change selected component (0, 1, 2)
wd5gnr 0:3e6a4dfbcb88 61 if (++sel>2) sel=0;
wd5gnr 0:3e6a4dfbcb88 62 }
wd5gnr 0:3e6a4dfbcb88 63
wd5gnr 0:3e6a4dfbcb88 64 int main() {
wd5gnr 0:3e6a4dfbcb88 65 // Set up encoder callbacks
wd5gnr 0:3e6a4dfbcb88 66 enc.attachROTCW(cw);
wd5gnr 0:3e6a4dfbcb88 67 enc.attachROTCCW(ccw);
wd5gnr 0:3e6a4dfbcb88 68 enc.attachSW(btn);
wd5gnr 0:3e6a4dfbcb88 69 while (true); // nothing else to do but wait
wd5gnr 0:3e6a4dfbcb88 70 }