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.
Fork of aReadConditional by
main.cpp@6:48052cf53bf2, 2017-10-08 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Oct 08 03:39:44 2017 +0000
- Revision:
- 6:48052cf53bf2
- Parent:
- 5:a08ed9b4c671
Reading of DigitalOut states made explicit. Comments cleaned up.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:2254358fce87 | 1 | /* |
CSTritt | 5:a08ed9b4c671 | 2 | Project: mySmrtNL_2(for My Smart Nightlight) |
CSTritt | 0:2254358fce87 | 3 | File: main.cpp |
CSTritt | 5:a08ed9b4c671 | 4 | |
CSTritt | 0:2254358fce87 | 5 | Reads from analog input, streams ASCII text to std serial using printf and |
CSTritt | 5:a08ed9b4c671 | 6 | DigitalOut control of external LEDs. Default serial configuration is 9600 |
CSTritt | 5:a08ed9b4c671 | 7 | baud, 8 data bits, no parity and 1 stop bit. |
CSTritt | 5:a08ed9b4c671 | 8 | |
CSTritt | 5:a08ed9b4c671 | 9 | Requires manual calibration. Enter max. and min. values below, recompile |
CSTritt | 5:a08ed9b4c671 | 10 | and send to Nucleo board. |
CSTritt | 5:a08ed9b4c671 | 11 | |
CSTritt | 0:2254358fce87 | 12 | Written by: Dr. C. S. Tritt |
CSTritt | 5:a08ed9b4c671 | 13 | Created: 10/7/17 (v. 2.0) |
CSTritt | 5:a08ed9b4c671 | 14 | |
CSTritt | 0:2254358fce87 | 15 | */ |
CSTritt | 0:2254358fce87 | 16 | #include "mbed.h" |
CSTritt | 0:2254358fce87 | 17 | |
CSTritt | 0:2254358fce87 | 18 | const int HIGH = 1; // Optional, but makes code more readable. |
CSTritt | 0:2254358fce87 | 19 | const int LOW = 0; // Optional, but makes code more readable. |
CSTritt | 5:a08ed9b4c671 | 20 | |
CSTritt | 3:0756601ff19e | 21 | AnalogIn analog_value(A0); // Construct AnalogIn object called analog_value. |
CSTritt | 5:a08ed9b4c671 | 22 | |
CSTritt | 5:a08ed9b4c671 | 23 | DigitalOut red(D2); // Construct DigitalOut object called red. |
CSTritt | 5:a08ed9b4c671 | 24 | DigitalOut grn(D3); // Construct DigitalOut object called grn. |
CSTritt | 5:a08ed9b4c671 | 25 | DigitalOut blu(D4); // Construct DigitalOut object called blu. |
CSTritt | 0:2254358fce87 | 26 | |
CSTritt | 5:a08ed9b4c671 | 27 | int main() |
CSTritt | 5:a08ed9b4c671 | 28 | { |
CSTritt | 5:a08ed9b4c671 | 29 | const float v_max = 1.0f; // Change to your max. serial value. |
CSTritt | 5:a08ed9b4c671 | 30 | const float v_min = 0.0f; // Change to your min. serial value. |
CSTritt | 5:a08ed9b4c671 | 31 | const float step = (v_max - v_min)/4.0f; // Calibration step size. |
CSTritt | 5:a08ed9b4c671 | 32 | const float g_off_r_on = v_max - step; // Green off, red on value. |
CSTritt | 5:a08ed9b4c671 | 33 | const float b_off_g_on = v_max - 2.0f*step; // Blue off, green on value. |
CSTritt | 5:a08ed9b4c671 | 34 | const float b_on = v_max - 3.0f*step; // Blue on value. |
CSTritt | 5:a08ed9b4c671 | 35 | |
CSTritt | 0:2254358fce87 | 36 | float value; // Value to be read and sent to serial port. |
CSTritt | 5:a08ed9b4c671 | 37 | |
CSTritt | 3:0756601ff19e | 38 | printf("\nAnalogIn example\n"); // Identify program. |
CSTritt | 5:a08ed9b4c671 | 39 | |
CSTritt | 0:2254358fce87 | 40 | while(true) { |
CSTritt | 0:2254358fce87 | 41 | value = analog_value.read(); // Read the analog input value (0 to 1) |
CSTritt | 0:2254358fce87 | 42 | printf("Value = %f\n", value); // Send value as text via serial port. |
CSTritt | 5:a08ed9b4c671 | 43 | if (value > g_off_r_on) { // Activate red for darkest conditions. |
CSTritt | 5:a08ed9b4c671 | 44 | red.write(HIGH); |
CSTritt | 5:a08ed9b4c671 | 45 | grn.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 46 | blu.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 47 | } else if (value > b_off_g_on) { // Activate green for dim light. |
CSTritt | 5:a08ed9b4c671 | 48 | red.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 49 | grn.write(HIGH); |
CSTritt | 5:a08ed9b4c671 | 50 | blu.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 51 | } else if (value > b_on) {// Activate blue for brighter light. |
CSTritt | 5:a08ed9b4c671 | 52 | red.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 53 | grn.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 54 | blu.write(HIGH); |
CSTritt | 5:a08ed9b4c671 | 55 | } else { // All junctions off for brightest light. |
CSTritt | 5:a08ed9b4c671 | 56 | red.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 57 | grn.write(LOW); |
CSTritt | 5:a08ed9b4c671 | 58 | blu.write(LOW); |
CSTritt | 0:2254358fce87 | 59 | } |
CSTritt | 6:48052cf53bf2 | 60 | // Read and send LED state via serial. |
CSTritt | 6:48052cf53bf2 | 61 | printf("RGB State: %d %d %d\n", red.read(), grn.read(), blu.read()); |
CSTritt | 3:0756601ff19e | 62 | wait(0.5); // Half a second (500 mS). |
CSTritt | 0:2254358fce87 | 63 | } |
CSTritt | 4:acc6be2bbe21 | 64 | } |