Display text on screen.
Dependencies: TextLCD mbed MaxSonar RTC-DS1307
Fork of TextLCD_HelloWorld by
main.cpp@6:fa30f8383b99, 2017-05-24 (annotated)
- Committer:
- aueangpanit
- Date:
- Wed May 24 14:08:30 2017 +0000
- Revision:
- 6:fa30f8383b99
- Parent:
- 5:2f13ec8efe0b
- Child:
- 7:f00ddf85cacc
two working distance sensor displaying 2 distances on 2 seperate screens;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 1:7418a52375a0 | 1 | // Hello World! for the TextLCD |
simon | 1:7418a52375a0 | 2 | |
simon | 0:334327d1a416 | 3 | #include "mbed.h" |
aueangpanit | 4:c669026b6f6e | 4 | #include <string> |
simon | 0:334327d1a416 | 5 | #include "TextLCD.h" |
aueangpanit | 5:2f13ec8efe0b | 6 | #include "MaxSonar.h" |
aueangpanit | 5:2f13ec8efe0b | 7 | using std::string; |
simon | 0:334327d1a416 | 8 | |
aueangpanit | 3:5e0ba6e35849 | 9 | TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7 |
aueangpanit | 3:5e0ba6e35849 | 10 | |
aueangpanit | 5:2f13ec8efe0b | 11 | DigitalOut questionScreen(PTB8); |
aueangpanit | 4:c669026b6f6e | 12 | DigitalOut screen1(PTE5); |
aueangpanit | 4:c669026b6f6e | 13 | DigitalOut screen2(PTE4); |
aueangpanit | 4:c669026b6f6e | 14 | |
aueangpanit | 4:c669026b6f6e | 15 | void UpdateScreen(DigitalOut screen, string text); |
aueangpanit | 5:2f13ec8efe0b | 16 | void UpdateScreen(DigitalOut screen, char text[1024]); |
simon | 0:334327d1a416 | 17 | |
simon | 0:334327d1a416 | 18 | int main() { |
aueangpanit | 5:2f13ec8efe0b | 19 | UpdateScreen(screen1, "Hello!"); |
aueangpanit | 5:2f13ec8efe0b | 20 | |
aueangpanit | 5:2f13ec8efe0b | 21 | MaxSonar *range1; |
aueangpanit | 5:2f13ec8efe0b | 22 | float r1; |
aueangpanit | 6:fa30f8383b99 | 23 | |
aueangpanit | 6:fa30f8383b99 | 24 | MaxSonar *range2; |
aueangpanit | 6:fa30f8383b99 | 25 | float r2; |
aueangpanit | 5:2f13ec8efe0b | 26 | |
aueangpanit | 5:2f13ec8efe0b | 27 | // Create and configure object for 3.3V powered LV-series device, |
aueangpanit | 5:2f13ec8efe0b | 28 | // accessed with analog reads (in cm) on p16, triggered by p7. |
aueangpanit | 5:2f13ec8efe0b | 29 | range1 = new MaxSonar(MS_LV, MS_ANALOG, PTB8, PTC2); |
aueangpanit | 5:2f13ec8efe0b | 30 | range1->setVoltage(3.3); |
aueangpanit | 5:2f13ec8efe0b | 31 | range1->setUnits(MS_CM); |
aueangpanit | 5:2f13ec8efe0b | 32 | |
aueangpanit | 6:fa30f8383b99 | 33 | range2 = new MaxSonar(MS_LV, MS_ANALOG, PTB9, PTB3); |
aueangpanit | 6:fa30f8383b99 | 34 | range2->setVoltage(3.3); |
aueangpanit | 6:fa30f8383b99 | 35 | range2->setUnits(MS_CM); |
aueangpanit | 5:2f13ec8efe0b | 36 | |
aueangpanit | 5:2f13ec8efe0b | 37 | while(1) { |
aueangpanit | 5:2f13ec8efe0b | 38 | // Trigger read, wait 49ms until ranger finder has |
aueangpanit | 5:2f13ec8efe0b | 39 | // finished, then read. |
aueangpanit | 5:2f13ec8efe0b | 40 | range1->triggerRead(); |
aueangpanit | 5:2f13ec8efe0b | 41 | wait_ms(49); |
aueangpanit | 5:2f13ec8efe0b | 42 | r1 = range1->read(); |
aueangpanit | 6:fa30f8383b99 | 43 | |
aueangpanit | 6:fa30f8383b99 | 44 | range2->triggerRead(); |
aueangpanit | 6:fa30f8383b99 | 45 | wait_ms(49); |
aueangpanit | 6:fa30f8383b99 | 46 | r2 = range2->read(); |
aueangpanit | 5:2f13ec8efe0b | 47 | |
aueangpanit | 5:2f13ec8efe0b | 48 | // Print and delay 0.5s. |
aueangpanit | 6:fa30f8383b99 | 49 | char range1_char_array[1024]; |
aueangpanit | 6:fa30f8383b99 | 50 | char range2_char_array[1024]; |
aueangpanit | 6:fa30f8383b99 | 51 | sprintf (range1_char_array, "Range: %.3f cm\n", r1); |
aueangpanit | 6:fa30f8383b99 | 52 | sprintf (range2_char_array, "Range: %.3f cm\n", r2); |
aueangpanit | 6:fa30f8383b99 | 53 | UpdateScreen(screen1, range1_char_array); |
aueangpanit | 6:fa30f8383b99 | 54 | UpdateScreen(screen2, range2_char_array); |
aueangpanit | 5:2f13ec8efe0b | 55 | |
aueangpanit | 5:2f13ec8efe0b | 56 | wait(0.5); |
aueangpanit | 5:2f13ec8efe0b | 57 | } |
aueangpanit | 4:c669026b6f6e | 58 | } |
aueangpanit | 4:c669026b6f6e | 59 | |
aueangpanit | 4:c669026b6f6e | 60 | void UpdateScreen(DigitalOut screen, string text) |
aueangpanit | 4:c669026b6f6e | 61 | { |
aueangpanit | 4:c669026b6f6e | 62 | //disable all E pin for all screens |
aueangpanit | 4:c669026b6f6e | 63 | screen1 = 0; |
aueangpanit | 4:c669026b6f6e | 64 | screen2 = 0; |
aueangpanit | 4:c669026b6f6e | 65 | |
aueangpanit | 4:c669026b6f6e | 66 | //enable E pin for the scrren that we want to update |
aueangpanit | 4:c669026b6f6e | 67 | screen = 1; |
aueangpanit | 4:c669026b6f6e | 68 | |
aueangpanit | 5:2f13ec8efe0b | 69 | //convert text to char array |
aueangpanit | 5:2f13ec8efe0b | 70 | char text_char_array[1024]; |
aueangpanit | 5:2f13ec8efe0b | 71 | strcpy(text_char_array, text.c_str()); |
aueangpanit | 4:c669026b6f6e | 72 | //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly |
aueangpanit | 5:2f13ec8efe0b | 73 | for(int i = 0; i < 10; i++) |
aueangpanit | 4:c669026b6f6e | 74 | { |
aueangpanit | 3:5e0ba6e35849 | 75 | lcd.cls(); |
aueangpanit | 4:c669026b6f6e | 76 | lcd.printf(text_char_array); |
aueangpanit | 3:5e0ba6e35849 | 77 | } |
aueangpanit | 5:2f13ec8efe0b | 78 | } |
aueangpanit | 5:2f13ec8efe0b | 79 | |
aueangpanit | 5:2f13ec8efe0b | 80 | void UpdateScreen(DigitalOut screen, char text[1024]) |
aueangpanit | 5:2f13ec8efe0b | 81 | { |
aueangpanit | 5:2f13ec8efe0b | 82 | //disable all E pin for all screens |
aueangpanit | 5:2f13ec8efe0b | 83 | screen1 = 0; |
aueangpanit | 5:2f13ec8efe0b | 84 | screen2 = 0; |
aueangpanit | 4:c669026b6f6e | 85 | |
aueangpanit | 5:2f13ec8efe0b | 86 | //enable E pin for the scrren that we want to update |
aueangpanit | 5:2f13ec8efe0b | 87 | screen = 1; |
aueangpanit | 4:c669026b6f6e | 88 | |
aueangpanit | 5:2f13ec8efe0b | 89 | //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly |
aueangpanit | 5:2f13ec8efe0b | 90 | for(int i = 0; i < 10; i++) |
aueangpanit | 5:2f13ec8efe0b | 91 | { |
aueangpanit | 5:2f13ec8efe0b | 92 | lcd.cls(); |
aueangpanit | 5:2f13ec8efe0b | 93 | lcd.printf(text); |
aueangpanit | 5:2f13ec8efe0b | 94 | } |
simon | 0:334327d1a416 | 95 | } |