Page 130 - ARM Based Microcontroller Projects Using MBED
P. 130

116                       7. USING THE Mbed WITH SIMPLE PROJECTS

                    /***************************************************************************

                                                    RGB LED CONTROL
                                                    ===============
                    In this project an RGB LED is connected to the Nucleo board. Additionally,
                    3 buttons named R,G, and B are conencted to the board. When a button is
                    pressed the corresponding colour is displayed by the RGB LED. Pressing the
                    same button again removes that colour from the display.
                    The buttons are pulled-up in software and are connected as follows:
                    R to PC_0
                    G to PC_1
                    B to PC_2
                    The RGB LED pins are connected as follows:
                    Red   to PC_3
                    Green to PC_4
                    Blue  to PC_5
                    Author: Dogan Ibrahim
                    Date  : August 2018
                    File  : RGB
                    *****************************************************************************/
                    #include "mbed.h"

                    BusOut RGB(PC_3, PC_4, PC_5);                       // Group the LED pins
                    DigitalIn R(PC_0, PullUp);                          // R button
                    DigitalIn G(PC_1, PullUp);                          // G button
                    DigitalIn B(PC_2, PullUp);                          // B button

                    int main()
                    {
                        RGB = 0;                                        // LEDs OFF at beginning

                        while(1)                                        // Do forever
                        {
                            if(R == 0)                                  // If R pressed
                            {
                                if(RGB & 1 == 1)                        // If Red is active
                                    RGB = RGB & 6;                      // Turn OFF Red
                                else                                    // Otherwise
                                    RGB = RGB | 1;                      // Turn ON Red
                                while(R == 0);                          // Wait until released
                            }
                            else if(G == 0)                             // If G pressed
                            {
                                if((RGB & 2) == 2)                      //If Green is active
                                    RGB = RGB & 5;                      // Turn OFF Green
                                else                                    // Otherwise
                                    RGB = RGB | 2;                      // Turn ON Green
                                while(G == 0);                          // Wait until released
                            }
                 FIG. 7.59  Program listing.
                                                                                          (Continued)
   125   126   127   128   129   130   131   132   133   134   135