Karl Nicolaus / Mbed 2 deprecated 4180_Lab1_P1238

Dependencies:   mbed DebounceIn PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 
00004 // THIS PROGRAM IS FOR DEMONSTRATING PROBLEMS 1 - 3 OF LAB #1
00005 // NOW INCLUDES PARTS 8
00006 
00007 // Input declarations
00008 // Input/output type yourvariable(physical pin)
00009 DigitalIn pb_1(p5);
00010 DigitalIn dip1(p8);
00011 DigitalIn dip2(p9);
00012 DigitalIn dip3(p10);
00013 PinDetect pb_2_up(p6);
00014 PinDetect pb_2_down(p7);
00015 PinDetect pb_3_up(p11);
00016 PinDetect pb_3_down(p12);
00017 AnalogIn pot8(p20);
00018 
00019 
00020 // Output declarations
00021 DigitalOut redLED(p30);
00022 DigitalOut mbedLED2(LED2);
00023 DigitalOut mbedLED3(LED3);
00024 DigitalOut mbedLED4(LED4);
00025 PwmOut pwmLED(LED1);
00026 PwmOut redPwm(p23);
00027 PwmOut greenPwm(p22);
00028 PwmOut bluePwm(p21);
00029 PwmOut greenLED(p24);
00030 //AnalogOut aout(p18);
00031 
00032 // Global variables for pwm changes
00033 // duty_cycle_2 ranges from 0.0f - 1.0f
00034 float volatile duty_cycle_2 = 0.5f;
00035 float volatile duty_cycle_3 = 0.5f;
00036 
00037 // Callback routine is interrupt activated by a debounced pb_2_up hit
00038 void pb2up_hit_callback (void) {
00039     if(duty_cycle_2 < 1.0f) {
00040         duty_cycle_2 += 0.1f;
00041     } else if(duty_cycle_2 > 1.0f) {
00042         duty_cycle_2 = 1.0f;
00043     }
00044 }
00045 // Callback routine is interrupt activated by a debounced pb_2_down hit
00046 void pb2down_hit_callback (void) {
00047     if(duty_cycle_2 > 0.0f) {
00048         duty_cycle_2 -= 0.1f;
00049     } else if(duty_cycle_2 < 0.0f) {
00050         duty_cycle_2 = 0.0f;
00051     }
00052 }
00053 // Callback routine is interrupt activated by a debounced pb_2_up hit
00054 void pb3up_hit_callback (void) {
00055     if(duty_cycle_3 < 1.0f) {
00056         duty_cycle_3 += 0.1f;
00057     } else if(duty_cycle_3 > 1.0f) {
00058         duty_cycle_3 = 1.0f;
00059     }
00060 }
00061 // Callback routine is interrupt activated by a debounced pb_2_down hit
00062 void pb3down_hit_callback (void) {
00063     if(duty_cycle_3 > 0.0f) {
00064         duty_cycle_3 -= 0.1f;
00065     } else if(duty_cycle_3 < 0.0f) {
00066         duty_cycle_3 = 0.0f;
00067     }
00068 }
00069 
00070 int main()  { 
00071     //duty_cycle_2 = 0.5f;
00072     
00073     // Variable Declarations
00074     pb_2_up.mode(PullUp);
00075     pb_2_down.mode(PullUp);
00076     pb_3_up.mode(PullUp);
00077     pb_3_down.mode(PullUp);
00078     dip1.mode(PullUp);
00079     dip2.mode(PullUp);
00080     dip3.mode(PullUp);
00081     
00082     wait(0.1);
00083     // Setup Interrupt callback functions for pb up and down
00084     pb_2_up.attach_deasserted(&pb2up_hit_callback);
00085     pb_2_down.attach_deasserted(&pb2down_hit_callback);
00086     pb_3_up.attach_deasserted(&pb3up_hit_callback);
00087     pb_3_down.attach_deasserted(&pb3down_hit_callback);
00088     //Start sampling pb up and down inputs using interrupts
00089     pb_2_up.setSampleFrequency();
00090     pb_2_down.setSampleFrequency();
00091     pb_3_up.setSampleFrequency();
00092     pb_3_down.setSampleFrequency();
00093     // pwmLED variable declarations
00094     
00095     while(1) {
00096         redLED = !pb_1;
00097         pwmLED = duty_cycle_2;
00098 
00099         if (!dip1) {
00100             redPwm = duty_cycle_3;
00101         } else {
00102             redPwm = 0.0f;
00103         }
00104 
00105         if (!dip2) {
00106             greenPwm = duty_cycle_3;
00107         } else {
00108             greenPwm = 0.0f;
00109         }
00110         if (!dip3) {
00111             bluePwm = duty_cycle_3;
00112         } else {
00113             bluePwm = 0.0f;
00114         }
00115         greenLED = pot8;
00116     }
00117 }