
Serial communication program
Serial.cpp@0:2f8376278c8a, 2020-08-01 (annotated)
- Committer:
- T00209563
- Date:
- Sat Aug 01 15:37:18 2020 +0000
- Revision:
- 0:2f8376278c8a
Serial communication program to terra term
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
T00209563 | 0:2f8376278c8a | 1 | #include "mbed.h" |
T00209563 | 0:2f8376278c8a | 2 | |
T00209563 | 0:2f8376278c8a | 3 | Serial pc(USBTX,USBRX); //defines an object called pc of serial class, and points it at the usb port |
T00209563 | 0:2f8376278c8a | 4 | PwmOut led(LED1); //pulse width modulation |
T00209563 | 0:2f8376278c8a | 5 | float brightness = 0.0; //brightness ia a variable of type float |
T00209563 | 0:2f8376278c8a | 6 | |
T00209563 | 0:2f8376278c8a | 7 | |
T00209563 | 0:2f8376278c8a | 8 | int main() |
T00209563 | 0:2f8376278c8a | 9 | { |
T00209563 | 0:2f8376278c8a | 10 | pc.printf ("Press 'u' = brighter, 'd' = dimmer\n\r"); //outputs a message at the start |
T00209563 | 0:2f8376278c8a | 11 | |
T00209563 | 0:2f8376278c8a | 12 | while(1) { |
T00209563 | 0:2f8376278c8a | 13 | |
T00209563 | 0:2f8376278c8a | 14 | char c = pc.getc (); |
T00209563 | 0:2f8376278c8a | 15 | if (( c =='u') && (brightness < 0.5)) { |
T00209563 | 0:2f8376278c8a | 16 | brightness += 0.01; |
T00209563 | 0:2f8376278c8a | 17 | pc.putc('^'); // when U is pressed ^ is printed to the screen |
T00209563 | 0:2f8376278c8a | 18 | led = brightness; |
T00209563 | 0:2f8376278c8a | 19 | } |
T00209563 | 0:2f8376278c8a | 20 | if (( c =='d') && (brightness > 0.0)) { |
T00209563 | 0:2f8376278c8a | 21 | brightness -= 0.01; |
T00209563 | 0:2f8376278c8a | 22 | pc.putc('V'); // when d is pressed V is printed to the screen |
T00209563 | 0:2f8376278c8a | 23 | led = brightness; |
T00209563 | 0:2f8376278c8a | 24 | } |
T00209563 | 0:2f8376278c8a | 25 | |
T00209563 | 0:2f8376278c8a | 26 | } |
T00209563 | 0:2f8376278c8a | 27 | } |