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.
Dependencies: mbed
main.cpp@3:9d006b6e1d04, 2021-03-27 (annotated)
- Committer:
- elab
- Date:
- Sat Mar 27 13:19:00 2021 +0000
- Revision:
- 3:9d006b6e1d04
- Parent:
- 2:e0faf9e57796
- Child:
- 4:972778a8aca8
1st commit;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| CSTritt | 0:2254358fce87 | 1 | /* |
| CSTritt | 0:2254358fce87 | 2 | Project: analogRead |
| CSTritt | 0:2254358fce87 | 3 | File: main.cpp |
| CSTritt | 0:2254358fce87 | 4 | |
| CSTritt | 0:2254358fce87 | 5 | Reads from analog input, streams ASCII text to std serial using printf and |
| CSTritt | 1:8e3c0c69a6ca | 6 | lights onboard LED. Also demonstrates use of floating point literal suffix |
| CSTritt | 1:8e3c0c69a6ca | 7 | toeliminate warning and int constants for HIGH and LOW. |
| CSTritt | 0:2254358fce87 | 8 | |
| CSTritt | 0:2254358fce87 | 9 | Written by: Dr. C. S. Tritt |
| CSTritt | 2:e0faf9e57796 | 10 | Created: 3/27/17 (v. 1.1) |
| CSTritt | 0:2254358fce87 | 11 | |
| CSTritt | 0:2254358fce87 | 12 | */ |
| CSTritt | 0:2254358fce87 | 13 | #include "mbed.h" |
| CSTritt | 0:2254358fce87 | 14 | |
| CSTritt | 0:2254358fce87 | 15 | const int HIGH = 1; // Optional, but makes code more readable. |
| CSTritt | 0:2254358fce87 | 16 | const int LOW = 0; // Optional, but makes code more readable. |
| CSTritt | 0:2254358fce87 | 17 | |
| elab | 3:9d006b6e1d04 | 18 | AnalogIn analog_value(A0); |
| CSTritt | 0:2254358fce87 | 19 | DigitalOut led(LED1); |
| elab | 3:9d006b6e1d04 | 20 | PwmOut servo(D6); |
| CSTritt | 0:2254358fce87 | 21 | |
| CSTritt | 0:2254358fce87 | 22 | int main() { |
| CSTritt | 0:2254358fce87 | 23 | float value; // Value to be read and sent to serial port. |
| elab | 3:9d006b6e1d04 | 24 | int i=0, angle; |
| elab | 3:9d006b6e1d04 | 25 | servo.period_ms(20); |
| CSTritt | 0:2254358fce87 | 26 | |
| CSTritt | 0:2254358fce87 | 27 | printf("\nAnalogIn example\n"); |
| CSTritt | 0:2254358fce87 | 28 | |
| CSTritt | 0:2254358fce87 | 29 | while(true) { |
| CSTritt | 0:2254358fce87 | 30 | value = analog_value.read(); // Read the analog input value (0 to 1) |
| CSTritt | 0:2254358fce87 | 31 | printf("Value = %f\n", value); // Send value as text via serial port. |
| elab | 3:9d006b6e1d04 | 32 | printf("Value_int = %d\n", int(value*180)); // Send value as text via serial port. |
| elab | 3:9d006b6e1d04 | 33 | angle = int(value*180); |
| elab | 3:9d006b6e1d04 | 34 | servo.pulsewidth_us(10*angle+500); |
| elab | 3:9d006b6e1d04 | 35 | wait_ms(10); |
| elab | 3:9d006b6e1d04 | 36 | |
| elab | 3:9d006b6e1d04 | 37 | |
| CSTritt | 0:2254358fce87 | 38 | if (value > 0.5f) { // Activate built-in LED. The f is optional. |
| CSTritt | 0:2254358fce87 | 39 | led.write(HIGH); |
| CSTritt | 0:2254358fce87 | 40 | } |
| CSTritt | 0:2254358fce87 | 41 | else { |
| CSTritt | 0:2254358fce87 | 42 | led.write(LOW); |
| CSTritt | 0:2254358fce87 | 43 | } |
| CSTritt | 2:e0faf9e57796 | 44 | printf("LED = %d\n", (int) led.read()); // Send LED state via serial. |
| CSTritt | 0:2254358fce87 | 45 | wait(0.25); // 250 ms |
| elab | 3:9d006b6e1d04 | 46 | |
| elab | 3:9d006b6e1d04 | 47 | // for (i=0; i<=200; i++){ |
| elab | 3:9d006b6e1d04 | 48 | // servo.pulsewidth_us(10*i+500); |
| elab | 3:9d006b6e1d04 | 49 | // wait_ms(10); |
| elab | 3:9d006b6e1d04 | 50 | // } |
| elab | 3:9d006b6e1d04 | 51 | // for (i=0; i<=200; i++){ |
| elab | 3:9d006b6e1d04 | 52 | // servo.pulsewidth_us(-10*i+2500); |
| elab | 3:9d006b6e1d04 | 53 | // wait_ms(10); |
| elab | 3:9d006b6e1d04 | 54 | // } |
| elab | 3:9d006b6e1d04 | 55 | |
| elab | 3:9d006b6e1d04 | 56 | |
| CSTritt | 0:2254358fce87 | 57 | } |
| CSTritt | 0:2254358fce87 | 58 | } |