Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

Revision:
8:8ce2ab8191cf
Parent:
7:f00ddf85cacc
Child:
9:39190ed25585
--- a/main.cpp	Wed May 24 14:40:21 2017 +0000
+++ b/main.cpp	Thu May 25 09:15:49 2017 +0000
@@ -14,9 +14,22 @@
 
 void UpdateScreen(DigitalOut screen, string text);
 void UpdateScreen(DigitalOut screen, char text[1024]);
+void DisplayNewQuestion();
+bool CheckCorrect(int screenNumber);
+
+string questions[4] = {"What is 2*2?", 
+                        "What is 5*2?", 
+                        "what is 2+2?",
+                        "What is 5*4?"};
+
+string screen1Answers[4] = {"4    ", "10    ", "5    ", "18    "};
+string screen2Answers[4] = {"5    ", "8    ", "4    ", "20    "};
+int correctScreen[4] = {1, 1, 2, 2}; //1 = screen1; 2 = screen2;
+
+int questionCount = 0;
 
 int main() {
-    UpdateScreen(questionScreen, "Hello!");
+    UpdateScreen(screen2, "Hello!");
     
     MaxSonar *range1;
     float r1;
@@ -45,6 +58,38 @@
         wait_ms(49);
         r2 = range2->read();
       
+        if(r1 < 20)
+        {
+            if(CheckCorrect(1))
+            {
+                UpdateScreen(questionScreen, "Correct!");
+                wait(1);
+                DisplayNewQuestion();
+            }
+            else
+            {
+                UpdateScreen(questionScreen, "Try Again!");
+                wait(1);
+                UpdateScreen(questionScreen, questions[questionCount]);
+            }
+        }
+        if(r2 < 20)
+        {
+            if(CheckCorrect(2))
+            {
+                UpdateScreen(questionScreen, "Correct!");
+                wait(1);
+                DisplayNewQuestion();
+            }
+            else
+            {
+                UpdateScreen(questionScreen, "Try Again!");
+                wait(1);
+                UpdateScreen(questionScreen, questions[questionCount]);
+            }
+        }    
+        
+        /* 
         // Print and delay 0.5s.
         char range1_char_array[1024];
         char range2_char_array[1024];
@@ -52,14 +97,44 @@
         sprintf (range2_char_array, "Range: %.3f cm\n", r2);
         UpdateScreen(questionScreen, range1_char_array);
         UpdateScreen(screen2, range2_char_array);
+        */
         
         wait(0.5);
     }
 }
 
+void DisplayNewQuestion()
+{
+    if(questionCount >= 3)
+    {
+        questionCount = 0;    
+    }
+    else
+    {
+        questionCount++;    
+    }
+    
+    UpdateScreen(questionScreen, questions[questionCount]);
+    UpdateScreen(screen1, screen1Answers[questionCount]);
+    UpdateScreen(screen2, screen2Answers[questionCount]);
+}
+
+bool CheckCorrect(int screenNumber)
+{
+    if(screenNumber == correctScreen[questionCount])
+    {
+        return true;    
+    }
+    else
+    {
+        return false;    
+    }
+}
+
 void UpdateScreen(DigitalOut screen, string text)
 {
     //disable all E pin for all screens
+    questionScreen = 0;
     screen1 = 0;
     screen2 = 0;
         
@@ -80,6 +155,7 @@
 void UpdateScreen(DigitalOut screen, char text[1024])
 {
     //disable all E pin for all screens
+    questionScreen = 0;
     screen1 = 0;
     screen2 = 0;