![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Program for testing the mRoteryEncoder as well as Switch ISR
Dependencies: mRotaryEncoder mbed
main.cpp@0:b2d0595444a4, 2016-03-06 (annotated)
- Committer:
- ostlerkd13
- Date:
- Sun Mar 06 06:58:22 2016 +0000
- Revision:
- 0:b2d0595444a4
Test program with function for Min and Max value returned from encoder.; ISR for switch-press that that toggles resolution from 1X to 10X.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ostlerkd13 | 0:b2d0595444a4 | 1 | /**************************************************************************************/ |
ostlerkd13 | 0:b2d0595444a4 | 2 | /**************************************************************************************/ |
ostlerkd13 | 0:b2d0595444a4 | 3 | /*SMART Remote Controller |
ostlerkd13 | 0:b2d0595444a4 | 4 | |
ostlerkd13 | 0:b2d0595444a4 | 5 | Kevin D. Ostler copyright 2016 */ |
ostlerkd13 | 0:b2d0595444a4 | 6 | /**************************************************************************************/ |
ostlerkd13 | 0:b2d0595444a4 | 7 | /**************************************************************************************/ |
ostlerkd13 | 0:b2d0595444a4 | 8 | #include "mbed.h" |
ostlerkd13 | 0:b2d0595444a4 | 9 | #include "mRotaryEncoder.h" |
ostlerkd13 | 0:b2d0595444a4 | 10 | #define MAX 100 |
ostlerkd13 | 0:b2d0595444a4 | 11 | #define MIN 0 |
ostlerkd13 | 0:b2d0595444a4 | 12 | |
ostlerkd13 | 0:b2d0595444a4 | 13 | mRotaryEncoder adjust_enc(p9, p10, p11);//pinA,pinB,pinSW |
ostlerkd13 | 0:b2d0595444a4 | 14 | |
ostlerkd13 | 0:b2d0595444a4 | 15 | float resolution = 1.0; |
ostlerkd13 | 0:b2d0595444a4 | 16 | float adjust_set = 0.0; |
ostlerkd13 | 0:b2d0595444a4 | 17 | float adjust_last = 0.0; |
ostlerkd13 | 0:b2d0595444a4 | 18 | bool button = 1; |
ostlerkd13 | 0:b2d0595444a4 | 19 | |
ostlerkd13 | 0:b2d0595444a4 | 20 | float AdjustVal(float adjust_val){ |
ostlerkd13 | 0:b2d0595444a4 | 21 | adjust_val += (adjust_enc.Get() * resolution); |
ostlerkd13 | 0:b2d0595444a4 | 22 | if( adjust_val >= MAX){ |
ostlerkd13 | 0:b2d0595444a4 | 23 | adjust_val = MAX; |
ostlerkd13 | 0:b2d0595444a4 | 24 | }else if(adjust_val <= MIN){ |
ostlerkd13 | 0:b2d0595444a4 | 25 | adjust_val = MIN; |
ostlerkd13 | 0:b2d0595444a4 | 26 | } |
ostlerkd13 | 0:b2d0595444a4 | 27 | adjust_enc.Set(0); |
ostlerkd13 | 0:b2d0595444a4 | 28 | return adjust_val; |
ostlerkd13 | 0:b2d0595444a4 | 29 | } |
ostlerkd13 | 0:b2d0595444a4 | 30 | void Set_Resolution(void){ |
ostlerkd13 | 0:b2d0595444a4 | 31 | button = !button; |
ostlerkd13 | 0:b2d0595444a4 | 32 | if( resolution == 1.0 ){ |
ostlerkd13 | 0:b2d0595444a4 | 33 | resolution = 0.1; |
ostlerkd13 | 0:b2d0595444a4 | 34 | }else{ |
ostlerkd13 | 0:b2d0595444a4 | 35 | resolution = 1.0; |
ostlerkd13 | 0:b2d0595444a4 | 36 | } |
ostlerkd13 | 0:b2d0595444a4 | 37 | } |
ostlerkd13 | 0:b2d0595444a4 | 38 | |
ostlerkd13 | 0:b2d0595444a4 | 39 | DigitalOut myled(LED1); |
ostlerkd13 | 0:b2d0595444a4 | 40 | Serial pc(USBTX, USBRX); // tx, rx |
ostlerkd13 | 0:b2d0595444a4 | 41 | |
ostlerkd13 | 0:b2d0595444a4 | 42 | int main() { |
ostlerkd13 | 0:b2d0595444a4 | 43 | pc.baud(921600); |
ostlerkd13 | 0:b2d0595444a4 | 44 | pc.format(8,Serial::None,1); |
ostlerkd13 | 0:b2d0595444a4 | 45 | wait(0.1); |
ostlerkd13 | 0:b2d0595444a4 | 46 | adjust_enc.attachSW( &Set_Resolution); |
ostlerkd13 | 0:b2d0595444a4 | 47 | while(1) { |
ostlerkd13 | 0:b2d0595444a4 | 48 | myled = 1; |
ostlerkd13 | 0:b2d0595444a4 | 49 | adjust_set = AdjustVal(adjust_set); |
ostlerkd13 | 0:b2d0595444a4 | 50 | //adjust_last = adjust_set; |
ostlerkd13 | 0:b2d0595444a4 | 51 | pc.printf("Adjust Level = %f Resolution = %f Button = %d\n\r" ,adjust_set/100, resolution, button); |
ostlerkd13 | 0:b2d0595444a4 | 52 | wait(0.2); |
ostlerkd13 | 0:b2d0595444a4 | 53 | myled = 0; |
ostlerkd13 | 0:b2d0595444a4 | 54 | pc.printf("Adjust Level = %f Resolution = %f Button = %d\n\r" ,adjust_set/100, resolution, button); |
ostlerkd13 | 0:b2d0595444a4 | 55 | wait(0.2); |
ostlerkd13 | 0:b2d0595444a4 | 56 | } |
ostlerkd13 | 0:b2d0595444a4 | 57 | } |