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 10:21:50 2013 +0000
Revision:
4:62b61875eab5
Parent:
3:58ae2860725e
Child:
5:d751707daaf7
Added conversion of potentiometer position to servo position range.; [0.0, 1.0] - [0 - 270].

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