Reading sensors

Dependencies:   mbed m3pi

Fork of 3pi_Lab1_Task2 by Craig Evans

Revision:
2:970ef3f79ac5
Parent:
1:3143ad629ed8
Child:
3:57544dc71dfc
--- a/main.cpp	Thu Jun 22 13:18:32 2017 +0000
+++ b/main.cpp	Mon Jun 26 11:51:05 2017 +0000
@@ -1,4 +1,4 @@
-/* 3pi Example 2
+/* 3pi Lab 1 Example 2
 
 (c) Dr Craig A. Evans, University of Leeds
 
@@ -11,7 +11,6 @@
 
 // API objects
 m3pi robot;
-AnalogIn pot_P(p15);
 
 // Function Prototypes
 void init();
@@ -20,28 +19,52 @@
 int main()
 {
     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);
+    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 3",6);
+
+    wait(2.0);
+
+    // reset previous calibration values and run auto-calibrate routine
+    robot.reset_calibration();
+    // robot must be placed over a black line
+    robot.auto_calibrate();
+
+    // an array to store each of the sensor values
+    unsigned int values[5]= {0};
     
-    // we will update the motors 50 times per second
-    float dt = 1.0/50.0;
+    
+    
+    float dt = 1.0/50.0;  // we'll read the sensor 50 times a second
 
     // main loop - this runs forever
     while(1) {
+        
+        // clear the LCD so we can draw new information on every loop
+        robot.lcd_clear();
 
-        // this returns a value in the range 0.0 to 1.0
-        float pot_P_val = pot_P.read();
+        // read the sensor values and store in array
+        // these values are 0 to 1000 (0 is white/1000 is black)
+        robot.get_calibrated_values(values);
         
-        // 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);
-        
+        // display a bar graph on the LCD
+        robot.display_sensor_values(values,1);
+
+        // in range -1 to 1, no error when line is at 0.0
+        // number is negative when the line is to the left
+        // number is positive when the line is to the right
+        float line_position = robot.calc_line_position(values);
+
+        // empty buffer to store values
+        char buffer[6];
+        sprintf(buffer,"%+.3f",line_position); // create message and store in array .3 means three decimal places
+        // print line position on LCD
+        robot.lcd_goto_xy(1,0);
+        robot.lcd_print(buffer,6);
+
         // wait for a short time before repeating the loop
         wait(dt);