Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP23S17 PinDetect mbed
RGBLed.h
00001 #include "mbed.h" 00002 00003 //Class to control an RGB LED using three PWM pins 00004 class RGBLed 00005 { 00006 public: 00007 RGBLed(PinName redpin, PinName greenpin, PinName bluepin); 00008 void write(float red,float green, float blue); 00009 void write(LEDColor c); 00010 RGBLed operator = (LEDColor c) { 00011 write(c); 00012 return *this; 00013 }; 00014 private: 00015 PwmOut _redpin; 00016 PwmOut _greenpin; 00017 PwmOut _bluepin; 00018 }; 00019 00020 RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) 00021 : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) 00022 { 00023 //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) 00024 _redpin.period(0.0005); 00025 } 00026 00027 void RGBLed::write(float red,float green, float blue) 00028 { 00029 _redpin = red; 00030 _greenpin = green; 00031 _bluepin = blue; 00032 } 00033 void RGBLed::write(LEDColor c) 00034 { 00035 _redpin = c.red; 00036 _greenpin = c.green; 00037 _bluepin = c.blue; 00038 } 00039 00040 ////classes could be moved to include file 00041 // 00042 // 00043 ////Setup RGB led using PWM pins and class 00044 //RGBLed myRGBled(p23,p22,p21); //RGB PWM pins 00045 // 00046 ////setup some color objects in flash using const's 00047 //const LEDColor red(1.0,0.0,0.0); 00048 //const LEDColor green(0.0,0.2,0.0); 00049 ////brighter green LED is scaled down to same as red and 00050 ////blue LED outputs on Sparkfun RGBLED 00051 //const LEDColor blue(0.0,0.0,1.0); 00052 //const LEDColor yellow(1.0,0.2,0.0); 00053 //const LEDColor white(1.0,0.2,1.0); 00054 // 00055 //int main() 00056 //{ 00057 // while(1) { 00058 // myRGBled = red; 00059 // //myRGBled.write(1.0,0.0,0,0); //red 00060 // wait(2.0); 00061 // myRGBled = green; 00062 // //myRGBled.write(0.0,1.0,0.0); //green 00063 // wait(2.0); 00064 // myRGBled = blue; 00065 // //myRGBled.write(0.0,0.0,1.0); //blue 00066 // wait(2.0); 00067 // myRGBled = red + green; //yellow = red + green 00068 // wait(2.0); 00069 // //white with a slow fade to black dimming effect 00070 // for (float brightness=1.0; brightness>=0.0001; brightness*=0.99) { 00071 // myRGBled = white * brightness; 00072 // wait(0.005); 00073 // } 00074 // wait(2.0); 00075 // } 00076 //}
Generated on Sat Jul 23 2022 21:06:37 by
1.7.2