Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

Revision:
5:2f13ec8efe0b
Parent:
4:c669026b6f6e
Child:
6:fa30f8383b99
--- a/main.cpp	Wed May 24 12:02:38 2017 +0000
+++ b/main.cpp	Wed May 24 13:51:44 2017 +0000
@@ -3,25 +3,46 @@
 #include "mbed.h"
 #include <string>
 #include "TextLCD.h"
+#include "MaxSonar.h"
+using std::string;
 
 TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7
 
+DigitalOut questionScreen(PTB8);
 DigitalOut screen1(PTE5);
 DigitalOut screen2(PTE4);
 
 void UpdateScreen(DigitalOut screen, string text);
-
-int i = 0;
+void UpdateScreen(DigitalOut screen, char text[1024]);
 
 int main() {
-    screen1 = 1;
-    screen2 = 1;
-    wait(1);
+    UpdateScreen(screen1, "Hello!");
+    
+    MaxSonar *range1;
+    float r1;
+ 
+    // Create and configure object for 3.3V powered LV-series device, 
+    // accessed with analog reads (in cm) on p16, triggered by p7.
+    range1 = new MaxSonar(MS_LV, MS_ANALOG, PTB8, PTC2);
+    range1->setVoltage(3.3);
+    range1->setUnits(MS_CM);
+    
     
-    UpdateScreen(screen2, "Screen2");
-    
-    UpdateScreen(screen1, "Screen1");
-    
+ 
+    while(1) {
+        // Trigger read, wait 49ms until ranger finder has
+        // finished, then read. 
+        range1->triggerRead();
+        wait_ms(49);
+        r1 = range1->read();
+      
+        // Print and delay 0.5s.
+        char str[1024];
+        sprintf (str, "Range: %.3f cm\n", r1);
+        UpdateScreen(screen2, str);
+        
+        wait(0.5);
+    }
 }
 
 void UpdateScreen(DigitalOut screen, string text)
@@ -33,14 +54,30 @@
     //enable E pin for the scrren that we want to update
     screen = 1;
     
+    //convert text to char array
+    char text_char_array[1024];
+    strcpy(text_char_array, text.c_str());
     //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
-    for(i = 0; i < 10; i++)
+    for(int i = 0; i < 10; i++)
     {
         lcd.cls();
-        char text_char_array[1024];
-        strcpy(text_char_array, text.c_str());
         lcd.printf(text_char_array);
     }
+}
+
+void UpdateScreen(DigitalOut screen, char text[1024])
+{
+    //disable all E pin for all screens
+    screen1 = 0;
+    screen2 = 0;
         
+    //enable E pin for the scrren that we want to update
+    screen = 1;
     
+    //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
+    for(int i = 0; i < 10; i++)
+    {
+        lcd.cls();
+        lcd.printf(text);
+    }
 }