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

main.cpp

Committer:
chapfohn
Date:
2013-10-08
Revision:
1:08d46aff0a08
Parent:
0:d1fc4eddceab
Child:
2:85693b6a99fa

File content as of revision 1:08d46aff0a08:

#include "mbed.h"
#include "Servo.h"
#include "C12832_lcd.h"

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

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

        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
    }
}