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

Committer:
chapfohn
Date:
Tue Oct 08 11:07:07 2013 +0000
Revision:
5:d751707daaf7
Parent:
4:62b61875eab5
Setup as two-way table.; All cells labelled.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:d1fc4eddceab 1 #include "mbed.h"
chris 0:d1fc4eddceab 2 #include "Servo.h"
chapfohn 1:08d46aff0a08 3 #include "C12832_lcd.h"
chris 0:d1fc4eddceab 4
chapfohn 1:08d46aff0a08 5 Servo s1(p21); //Application board servo is on p21, attach PAN to header
chapfohn 1:08d46aff0a08 6 Servo s2(p22); //Application board servo is on p21, attach TILT to header
chapfohn 1:08d46aff0a08 7
chapfohn 1:08d46aff0a08 8 C12832_LCD lcd;
chapfohn 1:08d46aff0a08 9
chapfohn 1:08d46aff0a08 10 AnalogIn p1(p19); //Left potentiometer
chapfohn 1:08d46aff0a08 11 AnalogIn p2(p20); //Right potentiometer
chapfohn 1:08d46aff0a08 12
chapfohn 1:08d46aff0a08 13 const int n = 10; //number of readings to be averaged, change globally here
chapfohn 1:08d46aff0a08 14 float panT =0, tiltT = 0; //Total holders for summation from axis arrays
chapfohn 1:08d46aff0a08 15
chapfohn 1:08d46aff0a08 16 float PANa, TILTa; //averaged values for each axis over n
chapfohn 2:85693b6a99fa 17 float pan[n], tilt[n]; //arrays to hold n readings for each axis
chapfohn 1:08d46aff0a08 18 int i, j; //indexing variables
chris 0:d1fc4eddceab 19
chapfohn 1:08d46aff0a08 20 int main()
chapfohn 1:08d46aff0a08 21 {
chapfohn 1:08d46aff0a08 22 while(1) {
chapfohn 1:08d46aff0a08 23 for (i = 0; i < n; i = i + 1) { //read n values into each axis array
chapfohn 1:08d46aff0a08 24 pan[i] = p1.read();
chapfohn 1:08d46aff0a08 25 tilt[i] = p2.read();
chapfohn 1:08d46aff0a08 26 }
chapfohn 1:08d46aff0a08 27 panT = 0; //reset Totala
chapfohn 1:08d46aff0a08 28 tiltT = 0; //reset Totala
chapfohn 1:08d46aff0a08 29 for (j = 0; j < n; j = j + 1) { //summation of the contents of each array into axis Totals
chapfohn 1:08d46aff0a08 30 panT = panT+pan[j];
chapfohn 1:08d46aff0a08 31 tiltT = tiltT+tilt[j];
chapfohn 1:08d46aff0a08 32 }
chapfohn 1:08d46aff0a08 33 PANa = panT/n; //axis average over n
chris 0:d1fc4eddceab 34
chapfohn 1:08d46aff0a08 35 TILTa = tiltT/n; //axis average over n
chapfohn 1:08d46aff0a08 36
chapfohn 1:08d46aff0a08 37 lcd.cls(); //Clear screen for new cycle
chapfohn 5:d751707daaf7 38 lcd.locate(30,3); //Screen location
chapfohn 5:d751707daaf7 39 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 40 lcd.locate(43,3); //Screen location
chapfohn 5:d751707daaf7 41 lcd.printf("Pan"); //Column heading
chapfohn 5:d751707daaf7 42 lcd.locate(76,3); //Screen location
chapfohn 5:d751707daaf7 43 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 44 lcd.locate(91,3); //Screen location
chapfohn 5:d751707daaf7 45 lcd.printf("Tilt"); //Column heading
chapfohn 1:08d46aff0a08 46 lcd.locate(3,12); //Screen location
chapfohn 5:d751707daaf7 47 lcd.printf("Norm"); //column seperator
chapfohn 5:d751707daaf7 48 lcd.locate(30,12); //Screen location
chapfohn 5:d751707daaf7 49 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 50 lcd.locate(43,12); //Screen location
chapfohn 1:08d46aff0a08 51 lcd.printf("%.2f\n",PANa); //Print PAN
chapfohn 5:d751707daaf7 52 lcd.locate(76,12); //Screen location
chapfohn 5:d751707daaf7 53 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 54 lcd.locate(91,12); //Screen location
chapfohn 1:08d46aff0a08 55 lcd.printf("%.2f\n",TILTa); //Print TILT
chapfohn 4:62b61875eab5 56 lcd.locate(3,21); //Screen location
chapfohn 5:d751707daaf7 57 lcd.printf("Angle"); //column seperator
chapfohn 5:d751707daaf7 58 lcd.locate(30,21); //Screen location
chapfohn 5:d751707daaf7 59 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 60 lcd.locate(43,21); //Screen location
chapfohn 4:62b61875eab5 61 lcd.printf("%.2f\n",PANa*270); //Print PAN
chapfohn 5:d751707daaf7 62 lcd.locate(76,21); //Screen location
chapfohn 5:d751707daaf7 63 lcd.printf("|"); //column seperator
chapfohn 5:d751707daaf7 64 lcd.locate(91,21); //Screen location
chapfohn 4:62b61875eab5 65 lcd.printf("%.2f\n",TILTa*270); //Print TILT
chapfohn 1:08d46aff0a08 66 s1=p1; //Set PAN servo to PAN potentiometer reading
chapfohn 1:08d46aff0a08 67 s2=p2; //Set PAN servo to PAN potentiometer reading
chapfohn 3:58ae2860725e 68 wait(0.1); //Wait for servos to make position
chris 0:d1fc4eddceab 69 }
chris 0:d1fc4eddceab 70 }
chapfohn 1:08d46aff0a08 71
chapfohn 1:08d46aff0a08 72