Lab 1 Part 3

Dependencies:   mbed PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  //  4180 Lab 1, Part 3
00002 //  Gregory Lanier
00003 
00004 #include "mbed.h"
00005 #include "PinDetect.h"
00006 
00007 /*
00008     This program uses PWM with two buttons and a DIP switch to control the color and brightness of an RGB LED.
00009 */
00010 
00011 // Global Vars
00012 float brightness = 0.5f;   // Initial brightness 1/2 power
00013 
00014 // I/O
00015 PinDetect   dimButton(p8);      // Make LED dimmer with p8 button
00016 PinDetect   brightButton(p9);   // Make LED brighter with p9 button
00017 DigitalIn   rDIP(p15);  // Switch 1 for Red LED with p15
00018 DigitalIn   gDIP(p16);  // Switch 2 for Green LED with p16
00019 DigitalIn   bDIP(p17);  // Switch 3 for Blue LED with p17
00020 PwmOut  rLED(p21);  // Light up Red LED with p21
00021 PwmOut  gLED(p22);  // Light up Green LED with p22
00022 PwmOut  bLED(p23);  // Light up Blue LED with p23
00023 
00024 // Dim Callback
00025 void dim_callback(void) {
00026     brightness -= 0.1f;
00027 }
00028 
00029 // Brighten Callback
00030 void bright_callback(void) {
00031     brightness += 0.1f;
00032 }
00033 
00034 int main()
00035 {
00036     // Button Mode Set
00037     dimButton.mode(PullUp);    
00038     brightButton.mode(PullUp);
00039     wait(0.01);
00040     
00041     // Switch Mode Set
00042     rDIP.mode(PullUp);
00043     gDIP.mode(PullUp);
00044     bDIP.mode(PullUp);
00045     
00046     // Setuo button callbacks
00047     dimButton.attach_deasserted(&dim_callback);    
00048     brightButton.attach_deasserted(&bright_callback);
00049     
00050     // Start sampling button inputs using interrupts
00051     dimButton.setSampleFrequency();
00052     brightButton.setSampleFrequency();
00053     
00054     // Logic Loop
00055     while(1) {
00056 
00057         if (!rDIP){
00058             rLED = brightness;
00059             }
00060         else{
00061             rLED = 0;
00062             }
00063         
00064         if (!gDIP){
00065             gLED = brightness;
00066             }
00067         else{
00068             gLED = 0;
00069             }
00070             
00071         if (!bDIP){
00072             bLED = brightness;
00073             }
00074         else{
00075             bLED = 0;
00076             }
00077         }
00078 }