Build upon app-board-Servo. Print servo readings, scaled to [0.0, 1.0], to LCD on mbed Application Board. Scale reading to [0 - 270] range of typical servo. Print servo angles to LCD on mbed Application Board.

Dependencies:   C12832_lcd Servo mbed

Fork of app-board-Servo by Chris Styles

Revision:
1:08d46aff0a08
Parent:
0:d1fc4eddceab
Child:
2:85693b6a99fa
--- a/main.cpp	Mon Oct 15 12:43:06 2012 +0000
+++ b/main.cpp	Tue Oct 08 03:56:11 2013 +0000
@@ -1,16 +1,52 @@
 #include "mbed.h"
 #include "Servo.h"
+#include "C12832_lcd.h"
 
-Servo s1(p21);
-Servo s2(p22);
+Servo s1(p21);                              //Application board servo is on p21, attach PAN  to header
+Servo s2(p22);                              //Application board servo is on p21, attach TILT to header
+
+C12832_LCD lcd;
+
+AnalogIn p1(p19);                           //Left  potentiometer
+AnalogIn p2(p20);                           //Right potentiometer
+
+const   int n = 10;                         //number of readings to be averaged, change globally here
+float       panT =0, tiltT = 0;             //Total holders for summation from axis arrays
+
+float       PANa, TILTa;                    //averaged values for each axis over n
+float       pan[n], tilt[n];               //arrays to hold n readings for each axis
+int         i, j;                           //indexing variables
 
-AnalogIn p1(p19);
-AnalogIn p2(p20);
+int main()
+{
+    while(1) {
+        for (i = 0; i < n; i = i + 1) {     //read n values into each axis array
+            pan[i] = p1.read();
+            tilt[i] = p2.read();
+        }
+        panT = 0;                           //reset Totala
+        tiltT = 0;                          //reset Totala
+        for (j = 0; j < n; j = j + 1) {     //summation of the contents of each array into axis Totals
+            panT = panT+pan[j];
+            tiltT = tiltT+tilt[j];
+        }
+        PANa = panT/n;                      //axis average over n
 
-int main() {
-    while(1) {
-        s1=p1;
-        s2=p2;        
-        wait(0.1);
+        TILTa = tiltT/n;                    //axis average over n
+
+        lcd.cls();                          //Clear screen for new cycle
+        lcd.locate(3,3);                    //Screen location
+        lcd.printf("pan");                  //Column heading
+        lcd.locate(40,3);                   //Screen location
+        lcd.printf("tilt");                 //Column heading
+        lcd.locate(3,12);                   //Screen location
+        lcd.printf("%.2f\n",PANa);          //Print PAN
+        lcd.locate(40,12);                  //Screen location
+        lcd.printf("%.2f\n",TILTa);         //Print TILT
+        s1=p1;                              //Set PAN servo to PAN potentiometer reading
+        s2=p2;                              //Set PAN servo to PAN potentiometer reading
+        //wait(0.1);                        //Wait for servos to make position
     }
 }
+
+