![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
7 Segment disp
Dependencies: mbed
Fork of Part4 by
main.cpp@1:82e1e022a6d5, 2015-02-08 (annotated)
- Committer:
- jaredwil
- Date:
- Sun Feb 08 16:00:09 2015 +0000
- Revision:
- 1:82e1e022a6d5
- Parent:
- 0:288c00e3c1ae
commented;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaredwil | 1:82e1e022a6d5 | 1 | //Part 4 |
jaredwil | 1:82e1e022a6d5 | 2 | //This code is similar to part 3 but displays the dot |
jaredwil | 1:82e1e022a6d5 | 3 | //or dash on a seven segment display. Again dot or |
jaredwil | 1:82e1e022a6d5 | 4 | //dash is determined by length of input by user |
jaredwil | 1:82e1e022a6d5 | 5 | |
psahay | 0:288c00e3c1ae | 6 | #include "mbed.h" |
psahay | 0:288c00e3c1ae | 7 | |
jaredwil | 1:82e1e022a6d5 | 8 | //Initialize Interrupt |
psahay | 0:288c00e3c1ae | 9 | InterruptIn pound(p25); |
jaredwil | 1:82e1e022a6d5 | 10 | //Initialize Digital I/O |
psahay | 0:288c00e3c1ae | 11 | DigitalOut myled(LED1); |
psahay | 0:288c00e3c1ae | 12 | DigitalOut dot(p20); |
psahay | 0:288c00e3c1ae | 13 | DigitalOut dash(p19); |
jaredwil | 1:82e1e022a6d5 | 14 | //Initialize Timers |
psahay | 0:288c00e3c1ae | 15 | Timer t1; |
psahay | 0:288c00e3c1ae | 16 | Timer t2; |
psahay | 0:288c00e3c1ae | 17 | |
jaredwil | 1:82e1e022a6d5 | 18 | //Serial Interface for Debugging |
psahay | 0:288c00e3c1ae | 19 | Serial pc(USBTX, USBRX); |
psahay | 0:288c00e3c1ae | 20 | |
jaredwil | 1:82e1e022a6d5 | 21 | //Function to be called when button is pressed |
psahay | 0:288c00e3c1ae | 22 | void pPress () { |
jaredwil | 1:82e1e022a6d5 | 23 | t2.stop(); //stop space timer |
jaredwil | 1:82e1e022a6d5 | 24 | if(t2.read_ms() > 400)//if the length is greater than 400ms |
jaredwil | 1:82e1e022a6d5 | 25 | dot = 1; //turn of dot |
jaredwil | 1:82e1e022a6d5 | 26 | dash= 1; //turn of dash |
jaredwil | 1:82e1e022a6d5 | 27 | pc.printf(" "); //space is added |
jaredwil | 1:82e1e022a6d5 | 28 | t1.start(); //start dot/dash timer |
jaredwil | 1:82e1e022a6d5 | 29 | myled = 1; //turn on led for debugging |
psahay | 0:288c00e3c1ae | 30 | } |
jaredwil | 1:82e1e022a6d5 | 31 | |
jaredwil | 1:82e1e022a6d5 | 32 | //Function to be called when button is released |
psahay | 0:288c00e3c1ae | 33 | void pRelease() { |
jaredwil | 1:82e1e022a6d5 | 34 | t1.stop(); //stop dot/dash timer |
psahay | 0:288c00e3c1ae | 35 | if(t1.read_ms() > 30 && t1.read_ms() <= 200){ |
jaredwil | 1:82e1e022a6d5 | 36 | dot = 0; //turn on dot |
psahay | 0:288c00e3c1ae | 37 | pc.printf("."); |
psahay | 0:288c00e3c1ae | 38 | } |
psahay | 0:288c00e3c1ae | 39 | else if (t1.read_ms() > 200) { |
jaredwil | 1:82e1e022a6d5 | 40 | pc.printf("-"); //register dash don't turn on because already on |
psahay | 0:288c00e3c1ae | 41 | } |
jaredwil | 1:82e1e022a6d5 | 42 | //delay for stability |
psahay | 0:288c00e3c1ae | 43 | wait(0.1); |
jaredwil | 1:82e1e022a6d5 | 44 | |
jaredwil | 1:82e1e022a6d5 | 45 | //reset timers |
psahay | 0:288c00e3c1ae | 46 | t1.reset(); |
psahay | 0:288c00e3c1ae | 47 | t2.reset(); |
psahay | 0:288c00e3c1ae | 48 | |
jaredwil | 1:82e1e022a6d5 | 49 | dot = 1; //turn dot off |
jaredwil | 1:82e1e022a6d5 | 50 | dash = 1; //turn dash off |
jaredwil | 1:82e1e022a6d5 | 51 | myled = 0; //turn off led |
jaredwil | 1:82e1e022a6d5 | 52 | //start space timer |
psahay | 0:288c00e3c1ae | 53 | t2.start(); |
psahay | 0:288c00e3c1ae | 54 | } |
psahay | 0:288c00e3c1ae | 55 | int main() { |
jaredwil | 1:82e1e022a6d5 | 56 | //Int State for LEDs |
psahay | 0:288c00e3c1ae | 57 | dot=1; |
psahay | 0:288c00e3c1ae | 58 | dash=1; |
psahay | 0:288c00e3c1ae | 59 | myled = 0; |
jaredwil | 1:82e1e022a6d5 | 60 | //Initialize Inturrupt funct rise/fall edge |
psahay | 0:288c00e3c1ae | 61 | pound.rise(&pPress); |
psahay | 0:288c00e3c1ae | 62 | pound.fall(&pRelease); |
jaredwil | 1:82e1e022a6d5 | 63 | t2.start(); //start space timer |
psahay | 0:288c00e3c1ae | 64 | while(1) { |
jaredwil | 1:82e1e022a6d5 | 65 | if(t1.read_ms()> 200) { //if this is true then button is depressed still |
jaredwil | 1:82e1e022a6d5 | 66 | dash = 0; //dash is registered |
jaredwil | 1:82e1e022a6d5 | 67 | wait(0.1); //wait short duration for stability |
jaredwil | 1:82e1e022a6d5 | 68 | dash = 1; //turn off then back on if still depressed |
psahay | 0:288c00e3c1ae | 69 | } |
psahay | 0:288c00e3c1ae | 70 | } |
psahay | 0:288c00e3c1ae | 71 | } |