Lab 1 Task 1

Dependencies:   mbed m3pi

Fork of 3pi_Lab1_Task2 by Craig Evans

Revision:
2:5055dddfbee2
Parent:
1:3143ad629ed8
--- a/main.cpp	Thu Jun 22 13:18:32 2017 +0000
+++ b/main.cpp	Mon Jun 26 10:31:22 2017 +0000
@@ -1,4 +1,4 @@
-/* 3pi Example 2
+/* 3pi Lab 1 Example 1
 
 (c) Dr Craig A. Evans, University of Leeds
 
@@ -11,7 +11,13 @@
 
 // API objects
 m3pi robot;
-AnalogIn pot_P(p15);
+DigitalIn button_enter(p24);
+AnalogIn pot_S(p20);
+BusOut leds(LED4,LED3,LED2,LED1);
+
+// Global Variables
+const char g_song[] = "!T240 L8 a gafaeada c+adaeafa >aa>bac#ada c#adaeaf4";
+float speed = 0.0;
 
 // Function Prototypes
 void init();
@@ -19,31 +25,52 @@
 // Main Function
 int main()
 {
+    // call initialisation function - must do this first
     init();
-    
+
     // move cursor to position (0,0) - top-left
     robot.lcd_goto_xy(0,0);
-    // print on the LCD - the number 5 is the number of characters in the string
-    robot.lcd_print("Ex. 2",5);
-    
-    // we will update the motors 50 times per second
-    float dt = 1.0/50.0;
+    robot.lcd_print("Lab 1",5); // 5 is number of characters in message (max 8)
+    robot.lcd_goto_xy(0,1);
+    robot.lcd_print("Task 1",6);
+
+    // play song then leave a small delay for it to finish playing
+    robot.play_music(g_song,sizeof(g_song));
+    wait(5.0);
+
+    // loop through 0 to 15, displaying the value in binary on the leds
+    for (int value = 0; value < 16; value++) {
+        leds = value;
+        wait(0.1);
+    }
+    leds = 0;  // turn them all off
+
+    // keep looping until button has been pressed
+    while (button_enter.read() == 1) {
+
+    }
 
     // main loop - this runs forever
     while(1) {
 
-        // this returns a value in the range 0.0 to 1.0
-        float pot_P_val = pot_P.read();
+        // clear the screen
+        robot.lcd_clear();
+        // array to store message in
+        char buffer[8];
+
+        // check if enter button is pressed
+        if (button_enter.read() == 0) { 
+            speed = pot_S.read();  // if so, read speed potentiometer and store in variable
+            sprintf(buffer,"S=%.3f",speed); // create message and store in array .3 means three decimal places
+        }
         
-        // change to -1.0 to 1.0
-        float motor_speed = 2.0*pot_P_val-1.0;
-        // this gives full-speed backward (-1.0) to full-speed forward
-        
-        // set the speed of the left and right motors
-        robot.motors(motor_speed,motor_speed);
-        
-        // wait for a short time before repeating the loop
-        wait(dt);
+        // go to bottom line of LCD
+        robot.lcd_goto_xy(0,1);
+        // print the message
+        robot.lcd_print(buffer,7);
+
+        // small delay
+        wait(0.1);
 
     }
 }
@@ -51,5 +78,11 @@
 // Functions
 void init()
 {
+    // initialise the robot
     robot.init();
+
+    // turn on the pull-up resistor on the button
+    // the button will read 1 when not pressed and 0 when pressed
+    button_enter.mode(PullUp);
+
 }
\ No newline at end of file