Kotachi Liu
/
HW1Q2S1
LED 1-4 light based on switch states
main.cpp@0:105a5c050cb4, 2019-09-30 (annotated)
- Committer:
- kotachi
- Date:
- Mon Sep 30 17:17:23 2019 +0000
- Revision:
- 0:105a5c050cb4
- Child:
- 1:ad968bf98003
1st working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotachi | 0:105a5c050cb4 | 1 | #include "mbed.h" |
kotachi | 0:105a5c050cb4 | 2 | |
kotachi | 0:105a5c050cb4 | 3 | DigitalIn halls (p8); |
kotachi | 0:105a5c050cb4 | 4 | DigitalIn pushb (p20); |
kotachi | 0:105a5c050cb4 | 5 | DigitalOut l1(LED1); |
kotachi | 0:105a5c050cb4 | 6 | DigitalOut l2(LED2); |
kotachi | 0:105a5c050cb4 | 7 | DigitalOut l3(LED3); |
kotachi | 0:105a5c050cb4 | 8 | DigitalOut l4(LED4); |
kotachi | 0:105a5c050cb4 | 9 | Serial pc(USBTX,USBRX); |
kotachi | 0:105a5c050cb4 | 10 | |
kotachi | 0:105a5c050cb4 | 11 | int main() { |
kotachi | 0:105a5c050cb4 | 12 | while(1) { |
kotachi | 0:105a5c050cb4 | 13 | wait(1); |
kotachi | 0:105a5c050cb4 | 14 | int hall; |
kotachi | 0:105a5c050cb4 | 15 | int push; |
kotachi | 0:105a5c050cb4 | 16 | halls.read(); |
kotachi | 0:105a5c050cb4 | 17 | pushb.read(); |
kotachi | 0:105a5c050cb4 | 18 | hall=!halls; |
kotachi | 0:105a5c050cb4 | 19 | push=pushb; |
kotachi | 0:105a5c050cb4 | 20 | int button=hall+push; |
kotachi | 0:105a5c050cb4 | 21 | switch(button){ |
kotachi | 0:105a5c050cb4 | 22 | case 0: |
kotachi | 0:105a5c050cb4 | 23 | l1=1; |
kotachi | 0:105a5c050cb4 | 24 | l2=0; |
kotachi | 0:105a5c050cb4 | 25 | l3=0; |
kotachi | 0:105a5c050cb4 | 26 | l4=0; |
kotachi | 0:105a5c050cb4 | 27 | break; |
kotachi | 0:105a5c050cb4 | 28 | case 1: |
kotachi | 0:105a5c050cb4 | 29 | if (push==0) { |
kotachi | 0:105a5c050cb4 | 30 | l1=0; |
kotachi | 0:105a5c050cb4 | 31 | l2=1; |
kotachi | 0:105a5c050cb4 | 32 | l3=0; |
kotachi | 0:105a5c050cb4 | 33 | l4=0; |
kotachi | 0:105a5c050cb4 | 34 | } |
kotachi | 0:105a5c050cb4 | 35 | else { |
kotachi | 0:105a5c050cb4 | 36 | l1=0; |
kotachi | 0:105a5c050cb4 | 37 | l2=0; |
kotachi | 0:105a5c050cb4 | 38 | l3=1; |
kotachi | 0:105a5c050cb4 | 39 | l4=0; |
kotachi | 0:105a5c050cb4 | 40 | } |
kotachi | 0:105a5c050cb4 | 41 | break; |
kotachi | 0:105a5c050cb4 | 42 | case 2: |
kotachi | 0:105a5c050cb4 | 43 | l1=0; |
kotachi | 0:105a5c050cb4 | 44 | l2=0; |
kotachi | 0:105a5c050cb4 | 45 | l3=0; |
kotachi | 0:105a5c050cb4 | 46 | l4=1; |
kotachi | 0:105a5c050cb4 | 47 | break; |
kotachi | 0:105a5c050cb4 | 48 | } |
kotachi | 0:105a5c050cb4 | 49 | } |
kotachi | 0:105a5c050cb4 | 50 | } |