Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:a06d2b459821, 2018-10-16 (annotated)
- Committer:
- iveitch
- Date:
- Tue Oct 16 17:54:19 2018 +0000
- Revision:
- 0:a06d2b459821
Lab 4 Ex 4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| iveitch | 0:a06d2b459821 | 1 | /************************************************************** |
| iveitch | 0:a06d2b459821 | 2 | / ME21001 Group m-nn |
| iveitch | 0:a06d2b459821 | 3 | / |
| iveitch | 0:a06d2b459821 | 4 | / Lab 04 Exercise 4 |
| iveitch | 0:a06d2b459821 | 5 | / |
| iveitch | 0:a06d2b459821 | 6 | / This program reads an analogue input and displays the results |
| iveitch | 0:a06d2b459821 | 7 | / to the PC |
| iveitch | 0:a06d2b459821 | 8 | / |
| iveitch | 0:a06d2b459821 | 9 | */ |
| iveitch | 0:a06d2b459821 | 10 | #include "mbed.h" |
| iveitch | 0:a06d2b459821 | 11 | Serial pc(USBTX, USBRX); // USB serial interface |
| iveitch | 0:a06d2b459821 | 12 | |
| iveitch | 0:a06d2b459821 | 13 | AnalogIn A_in(p17); // analogue input pin for reference values |
| iveitch | 0:a06d2b459821 | 14 | PwmOut led1(LED1); |
| iveitch | 0:a06d2b459821 | 15 | PwmOut led2(LED2); |
| iveitch | 0:a06d2b459821 | 16 | PwmOut led3(LED3); |
| iveitch | 0:a06d2b459821 | 17 | PwmOut led4(LED4); |
| iveitch | 0:a06d2b459821 | 18 | int main() { |
| iveitch | 0:a06d2b459821 | 19 | |
| iveitch | 0:a06d2b459821 | 20 | float analogue_val=0.0; // variable to hold the analogue input |
| iveitch | 0:a06d2b459821 | 21 | // values as decimals |
| iveitch | 0:a06d2b459821 | 22 | // set the USB serial interface baud rate |
| iveitch | 0:a06d2b459821 | 23 | led1.period(0.020); |
| iveitch | 0:a06d2b459821 | 24 | led2.period(0.020); |
| iveitch | 0:a06d2b459821 | 25 | led3.period(0.020); |
| iveitch | 0:a06d2b459821 | 26 | led4.period(0.020); |
| iveitch | 0:a06d2b459821 | 27 | |
| iveitch | 0:a06d2b459821 | 28 | |
| iveitch | 0:a06d2b459821 | 29 | while(1) { // repeat indefinitely |
| iveitch | 0:a06d2b459821 | 30 | |
| iveitch | 0:a06d2b459821 | 31 | // read the voltage on the analogue input pin and |
| iveitch | 0:a06d2b459821 | 32 | // convert it to a |
| iveitch | 0:a06d2b459821 | 33 | // decimal number with 12-bit resolution |
| iveitch | 0:a06d2b459821 | 34 | |
| iveitch | 0:a06d2b459821 | 35 | analogue_val = A_in.read(); // export the readings to a terminal program |
| iveitch | 0:a06d2b459821 | 36 | // via the USB cable |
| iveitch | 0:a06d2b459821 | 37 | if( analogue_val<0.25){ |
| iveitch | 0:a06d2b459821 | 38 | led1=(analogue_val/0.25); |
| iveitch | 0:a06d2b459821 | 39 | led2=led3=led4=0; |
| iveitch | 0:a06d2b459821 | 40 | } |
| iveitch | 0:a06d2b459821 | 41 | else if (analogue_val<0.5){ |
| iveitch | 0:a06d2b459821 | 42 | led1=1; |
| iveitch | 0:a06d2b459821 | 43 | led2=((analogue_val-0.25)/0.25); |
| iveitch | 0:a06d2b459821 | 44 | led3=led4=0; |
| iveitch | 0:a06d2b459821 | 45 | } |
| iveitch | 0:a06d2b459821 | 46 | else if (analogue_val<0.75){ |
| iveitch | 0:a06d2b459821 | 47 | led1=led2=1; |
| iveitch | 0:a06d2b459821 | 48 | led3=((analogue_val-0.5)/0.25); |
| iveitch | 0:a06d2b459821 | 49 | led4=0; |
| iveitch | 0:a06d2b459821 | 50 | } |
| iveitch | 0:a06d2b459821 | 51 | else{ |
| iveitch | 0:a06d2b459821 | 52 | led1=led2=led3=1; |
| iveitch | 0:a06d2b459821 | 53 | led4=((analogue_val-0.75)/0.25); |
| iveitch | 0:a06d2b459821 | 54 | } |
| iveitch | 0:a06d2b459821 | 55 | wait(0.l); |
| iveitch | 0:a06d2b459821 | 56 | } |
| iveitch | 0:a06d2b459821 | 57 | } |
| iveitch | 0:a06d2b459821 | 58 | |
| iveitch | 0:a06d2b459821 | 59 |