test 1
Dependencies: mbed DebounceIn PinDetect
main.cpp@0:a331bf280a04, 2019-02-23 (annotated)
- Committer:
- kanicolaus
- Date:
- Sat Feb 23 20:35:51 2019 +0000
- Revision:
- 0:a331bf280a04
1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kanicolaus | 0:a331bf280a04 | 1 | #include "mbed.h" |
kanicolaus | 0:a331bf280a04 | 2 | #include "PinDetect.h" |
kanicolaus | 0:a331bf280a04 | 3 | |
kanicolaus | 0:a331bf280a04 | 4 | // THIS PROGRAM IS FOR DEMONSTRATING PROBLEMS 1 - 3 OF LAB #1 |
kanicolaus | 0:a331bf280a04 | 5 | // NOW INCLUDES PARTS 8 |
kanicolaus | 0:a331bf280a04 | 6 | |
kanicolaus | 0:a331bf280a04 | 7 | // Input declarations |
kanicolaus | 0:a331bf280a04 | 8 | // Input/output type yourvariable(physical pin) |
kanicolaus | 0:a331bf280a04 | 9 | DigitalIn pb_1(p5); |
kanicolaus | 0:a331bf280a04 | 10 | DigitalIn dip1(p8); |
kanicolaus | 0:a331bf280a04 | 11 | DigitalIn dip2(p9); |
kanicolaus | 0:a331bf280a04 | 12 | DigitalIn dip3(p10); |
kanicolaus | 0:a331bf280a04 | 13 | PinDetect pb_2_up(p6); |
kanicolaus | 0:a331bf280a04 | 14 | PinDetect pb_2_down(p7); |
kanicolaus | 0:a331bf280a04 | 15 | PinDetect pb_3_up(p11); |
kanicolaus | 0:a331bf280a04 | 16 | PinDetect pb_3_down(p12); |
kanicolaus | 0:a331bf280a04 | 17 | AnalogIn pot8(p20); |
kanicolaus | 0:a331bf280a04 | 18 | |
kanicolaus | 0:a331bf280a04 | 19 | |
kanicolaus | 0:a331bf280a04 | 20 | // Output declarations |
kanicolaus | 0:a331bf280a04 | 21 | DigitalOut redLED(p30); |
kanicolaus | 0:a331bf280a04 | 22 | DigitalOut mbedLED2(LED2); |
kanicolaus | 0:a331bf280a04 | 23 | DigitalOut mbedLED3(LED3); |
kanicolaus | 0:a331bf280a04 | 24 | DigitalOut mbedLED4(LED4); |
kanicolaus | 0:a331bf280a04 | 25 | PwmOut pwmLED(LED1); |
kanicolaus | 0:a331bf280a04 | 26 | PwmOut redPwm(p23); |
kanicolaus | 0:a331bf280a04 | 27 | PwmOut greenPwm(p22); |
kanicolaus | 0:a331bf280a04 | 28 | PwmOut bluePwm(p21); |
kanicolaus | 0:a331bf280a04 | 29 | PwmOut greenLED(p24); |
kanicolaus | 0:a331bf280a04 | 30 | //AnalogOut aout(p18); |
kanicolaus | 0:a331bf280a04 | 31 | |
kanicolaus | 0:a331bf280a04 | 32 | // Global variables for pwm changes |
kanicolaus | 0:a331bf280a04 | 33 | // duty_cycle_2 ranges from 0.0f - 1.0f |
kanicolaus | 0:a331bf280a04 | 34 | float volatile duty_cycle_2 = 0.5f; |
kanicolaus | 0:a331bf280a04 | 35 | float volatile duty_cycle_3 = 0.5f; |
kanicolaus | 0:a331bf280a04 | 36 | |
kanicolaus | 0:a331bf280a04 | 37 | // Callback routine is interrupt activated by a debounced pb_2_up hit |
kanicolaus | 0:a331bf280a04 | 38 | void pb2up_hit_callback (void) { |
kanicolaus | 0:a331bf280a04 | 39 | if(duty_cycle_2 < 1.0f) { |
kanicolaus | 0:a331bf280a04 | 40 | duty_cycle_2 += 0.1f; |
kanicolaus | 0:a331bf280a04 | 41 | } else if(duty_cycle_2 > 1.0f) { |
kanicolaus | 0:a331bf280a04 | 42 | duty_cycle_2 = 1.0f; |
kanicolaus | 0:a331bf280a04 | 43 | } |
kanicolaus | 0:a331bf280a04 | 44 | } |
kanicolaus | 0:a331bf280a04 | 45 | // Callback routine is interrupt activated by a debounced pb_2_down hit |
kanicolaus | 0:a331bf280a04 | 46 | void pb2down_hit_callback (void) { |
kanicolaus | 0:a331bf280a04 | 47 | if(duty_cycle_2 > 0.0f) { |
kanicolaus | 0:a331bf280a04 | 48 | duty_cycle_2 -= 0.1f; |
kanicolaus | 0:a331bf280a04 | 49 | } else if(duty_cycle_2 < 0.0f) { |
kanicolaus | 0:a331bf280a04 | 50 | duty_cycle_2 = 0.0f; |
kanicolaus | 0:a331bf280a04 | 51 | } |
kanicolaus | 0:a331bf280a04 | 52 | } |
kanicolaus | 0:a331bf280a04 | 53 | // Callback routine is interrupt activated by a debounced pb_2_up hit |
kanicolaus | 0:a331bf280a04 | 54 | void pb3up_hit_callback (void) { |
kanicolaus | 0:a331bf280a04 | 55 | if(duty_cycle_3 < 1.0f) { |
kanicolaus | 0:a331bf280a04 | 56 | duty_cycle_3 += 0.1f; |
kanicolaus | 0:a331bf280a04 | 57 | } else if(duty_cycle_3 > 1.0f) { |
kanicolaus | 0:a331bf280a04 | 58 | duty_cycle_3 = 1.0f; |
kanicolaus | 0:a331bf280a04 | 59 | } |
kanicolaus | 0:a331bf280a04 | 60 | } |
kanicolaus | 0:a331bf280a04 | 61 | // Callback routine is interrupt activated by a debounced pb_2_down hit |
kanicolaus | 0:a331bf280a04 | 62 | void pb3down_hit_callback (void) { |
kanicolaus | 0:a331bf280a04 | 63 | if(duty_cycle_3 > 0.0f) { |
kanicolaus | 0:a331bf280a04 | 64 | duty_cycle_3 -= 0.1f; |
kanicolaus | 0:a331bf280a04 | 65 | } else if(duty_cycle_3 < 0.0f) { |
kanicolaus | 0:a331bf280a04 | 66 | duty_cycle_3 = 0.0f; |
kanicolaus | 0:a331bf280a04 | 67 | } |
kanicolaus | 0:a331bf280a04 | 68 | } |
kanicolaus | 0:a331bf280a04 | 69 | |
kanicolaus | 0:a331bf280a04 | 70 | int main() { |
kanicolaus | 0:a331bf280a04 | 71 | //duty_cycle_2 = 0.5f; |
kanicolaus | 0:a331bf280a04 | 72 | |
kanicolaus | 0:a331bf280a04 | 73 | // Variable Declarations |
kanicolaus | 0:a331bf280a04 | 74 | pb_2_up.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 75 | pb_2_down.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 76 | pb_3_up.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 77 | pb_3_down.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 78 | dip1.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 79 | dip2.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 80 | dip3.mode(PullUp); |
kanicolaus | 0:a331bf280a04 | 81 | |
kanicolaus | 0:a331bf280a04 | 82 | wait(0.1); |
kanicolaus | 0:a331bf280a04 | 83 | // Setup Interrupt callback functions for pb up and down |
kanicolaus | 0:a331bf280a04 | 84 | pb_2_up.attach_deasserted(&pb2up_hit_callback); |
kanicolaus | 0:a331bf280a04 | 85 | pb_2_down.attach_deasserted(&pb2down_hit_callback); |
kanicolaus | 0:a331bf280a04 | 86 | pb_3_up.attach_deasserted(&pb3up_hit_callback); |
kanicolaus | 0:a331bf280a04 | 87 | pb_3_down.attach_deasserted(&pb3down_hit_callback); |
kanicolaus | 0:a331bf280a04 | 88 | //Start sampling pb up and down inputs using interrupts |
kanicolaus | 0:a331bf280a04 | 89 | pb_2_up.setSampleFrequency(); |
kanicolaus | 0:a331bf280a04 | 90 | pb_2_down.setSampleFrequency(); |
kanicolaus | 0:a331bf280a04 | 91 | pb_3_up.setSampleFrequency(); |
kanicolaus | 0:a331bf280a04 | 92 | pb_3_down.setSampleFrequency(); |
kanicolaus | 0:a331bf280a04 | 93 | // pwmLED variable declarations |
kanicolaus | 0:a331bf280a04 | 94 | |
kanicolaus | 0:a331bf280a04 | 95 | while(1) { |
kanicolaus | 0:a331bf280a04 | 96 | redLED = !pb_1; |
kanicolaus | 0:a331bf280a04 | 97 | pwmLED = duty_cycle_2; |
kanicolaus | 0:a331bf280a04 | 98 | |
kanicolaus | 0:a331bf280a04 | 99 | if (!dip1) { |
kanicolaus | 0:a331bf280a04 | 100 | redPwm = duty_cycle_3; |
kanicolaus | 0:a331bf280a04 | 101 | } else { |
kanicolaus | 0:a331bf280a04 | 102 | redPwm = 0.0f; |
kanicolaus | 0:a331bf280a04 | 103 | } |
kanicolaus | 0:a331bf280a04 | 104 | |
kanicolaus | 0:a331bf280a04 | 105 | if (!dip2) { |
kanicolaus | 0:a331bf280a04 | 106 | greenPwm = duty_cycle_3; |
kanicolaus | 0:a331bf280a04 | 107 | } else { |
kanicolaus | 0:a331bf280a04 | 108 | greenPwm = 0.0f; |
kanicolaus | 0:a331bf280a04 | 109 | } |
kanicolaus | 0:a331bf280a04 | 110 | if (!dip3) { |
kanicolaus | 0:a331bf280a04 | 111 | bluePwm = duty_cycle_3; |
kanicolaus | 0:a331bf280a04 | 112 | } else { |
kanicolaus | 0:a331bf280a04 | 113 | bluePwm = 0.0f; |
kanicolaus | 0:a331bf280a04 | 114 | } |
kanicolaus | 0:a331bf280a04 | 115 | greenLED = pot8; |
kanicolaus | 0:a331bf280a04 | 116 | } |
kanicolaus | 0:a331bf280a04 | 117 | } |