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 03:56:11 2013 +0000
Revision:
1:08d46aff0a08
Parent:
0:d1fc4eddceab
Child:
2:85693b6a99fa
Initial commit branch, print servo readings to LCD.

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 1:08d46aff0a08 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 1:08d46aff0a08 38 lcd.locate(3,3); //Screen location
chapfohn 1:08d46aff0a08 39 lcd.printf("pan"); //Column heading
chapfohn 1:08d46aff0a08 40 lcd.locate(40,3); //Screen location
chapfohn 1:08d46aff0a08 41 lcd.printf("tilt"); //Column heading
chapfohn 1:08d46aff0a08 42 lcd.locate(3,12); //Screen location
chapfohn 1:08d46aff0a08 43 lcd.printf("%.2f\n",PANa); //Print PAN
chapfohn 1:08d46aff0a08 44 lcd.locate(40,12); //Screen location
chapfohn 1:08d46aff0a08 45 lcd.printf("%.2f\n",TILTa); //Print TILT
chapfohn 1:08d46aff0a08 46 s1=p1; //Set PAN servo to PAN potentiometer reading
chapfohn 1:08d46aff0a08 47 s2=p2; //Set PAN servo to PAN potentiometer reading
chapfohn 1:08d46aff0a08 48 //wait(0.1); //Wait for servos to make position
chris 0:d1fc4eddceab 49 }
chris 0:d1fc4eddceab 50 }
chapfohn 1:08d46aff0a08 51
chapfohn 1:08d46aff0a08 52