Added various bits, main reformatted. Added .get to class Servo to allow waiting for rotation before recording information.

Dependencies:   SLCD mbed

Fork of Lab6_Basic by ECE 111 At Oregon State University

Committer:
Dogcatfee
Date:
Sat Nov 19 09:06:16 2016 +0000
Revision:
7:10554a04a34f
Parent:
6:a64d79286726
Child:
9:4bd820ca8300
Re calibrated for the smaller potentionometer ( I'm using the bigger one)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ziadeldebri 0:61b18b631f94 1 #include "mbed.h"
ziadeldebri 3:b787aa49b900 2 #include "Motor.h"
ziadeldebri 3:b787aa49b900 3 #include "Servo.h"
Dogcatfee 6:a64d79286726 4 /************************************* Lab5 Motor to servo **********************************/
Dogcatfee 6:a64d79286726 5 /* */
Dogcatfee 6:a64d79286726 6 /* File: main.cpp */
Dogcatfee 6:a64d79286726 7 /* Author: Ziad Eldebri */
Dogcatfee 6:a64d79286726 8 /* Date Created: 9/19/2016 */
Dogcatfee 6:a64d79286726 9 /* Description: */
Dogcatfee 6:a64d79286726 10 /* Edit: Matthew Sessions */
Dogcatfee 6:a64d79286726 11 /* */
Dogcatfee 6:a64d79286726 12 /*********************************************************************************************/
ziadeldebri 0:61b18b631f94 13
ziadeldebri 3:b787aa49b900 14 Servo my_servo(PTB3,PTE19,PTE18,PTE31);
ziadeldebri 4:b3a93554fedf 15 AnalogIn Spanel(PTB2);
Dogcatfee 6:a64d79286726 16 //uint16_t reading_array[3];
Dogcatfee 6:a64d79286726 17 const int size=20;
Dogcatfee 6:a64d79286726 18 unsigned int reading_array[size];
Dogcatfee 6:a64d79286726 19 int position_array[size];
Dogcatfee 6:a64d79286726 20 //uint16_t largest = 0;
Dogcatfee 6:a64d79286726 21 unsigned int largest = 0;
ziadeldebri 4:b3a93554fedf 22 int largesti = 0;
ziadeldebri 4:b3a93554fedf 23 int i;
ziadeldebri 3:b787aa49b900 24
Dogcatfee 6:a64d79286726 25 //Serial pc(USBTX, USBRX);
Dogcatfee 6:a64d79286726 26
Dogcatfee 6:a64d79286726 27 int main()
Dogcatfee 6:a64d79286726 28 {
Dogcatfee 6:a64d79286726 29
Dogcatfee 6:a64d79286726 30 for (i=0; i<size;i++)
Dogcatfee 6:a64d79286726 31 {
ziadeldebri 3:b787aa49b900 32
Dogcatfee 7:10554a04a34f 33 position_array[i] = i*120/size +20 ;
Dogcatfee 6:a64d79286726 34
Dogcatfee 6:a64d79286726 35 }
Dogcatfee 6:a64d79286726 36
Dogcatfee 6:a64d79286726 37 while (1)
Dogcatfee 6:a64d79286726 38 {
Dogcatfee 6:a64d79286726 39 for (i = 0; i < size; i++)
Dogcatfee 6:a64d79286726 40 {
ziadeldebri 0:61b18b631f94 41
Dogcatfee 6:a64d79286726 42 //set position
Dogcatfee 6:a64d79286726 43 my_servo.set(position_array[i]);
Dogcatfee 6:a64d79286726 44
Dogcatfee 6:a64d79286726 45 //pc.printf(" Reading array %d %ld\n ", i , reading_array[i]);
Dogcatfee 6:a64d79286726 46
Dogcatfee 6:a64d79286726 47 //check position
Dogcatfee 6:a64d79286726 48
Dogcatfee 6:a64d79286726 49 while(1)
Dogcatfee 6:a64d79286726 50 {
Dogcatfee 6:a64d79286726 51 //one but not both, XOR
Dogcatfee 6:a64d79286726 52 if((my_servo.get()-position_array[i] > -25 ) ^ (my_servo.get() - position_array[i] <25))
Dogcatfee 6:a64d79286726 53 {
ziadeldebri 3:b787aa49b900 54
Dogcatfee 6:a64d79286726 55 break;
Dogcatfee 6:a64d79286726 56 }
Dogcatfee 6:a64d79286726 57
Dogcatfee 6:a64d79286726 58 else
Dogcatfee 6:a64d79286726 59 {
Dogcatfee 6:a64d79286726 60 my_servo.set(position_array[i]);
Dogcatfee 6:a64d79286726 61 wait(.2);
Dogcatfee 6:a64d79286726 62 }
Dogcatfee 6:a64d79286726 63
Dogcatfee 6:a64d79286726 64
Dogcatfee 6:a64d79286726 65 }
Dogcatfee 6:a64d79286726 66
Dogcatfee 6:a64d79286726 67 //write analog values (integers) to array
Dogcatfee 6:a64d79286726 68 reading_array[i] = Spanel.read()*10000;
Dogcatfee 6:a64d79286726 69 }
ziadeldebri 4:b3a93554fedf 70
ziadeldebri 4:b3a93554fedf 71
ziadeldebri 4:b3a93554fedf 72
Dogcatfee 6:a64d79286726 73 for (i = 0; i < size; i++)
Dogcatfee 6:a64d79286726 74 {
Dogcatfee 6:a64d79286726 75 if (reading_array[i] > largest)
Dogcatfee 6:a64d79286726 76 {
ziadeldebri 3:b787aa49b900 77
Dogcatfee 6:a64d79286726 78 largest = reading_array[i];
Dogcatfee 6:a64d79286726 79 largesti = i;
Dogcatfee 6:a64d79286726 80
Dogcatfee 6:a64d79286726 81 }
Dogcatfee 6:a64d79286726 82
ziadeldebri 4:b3a93554fedf 83 }
Dogcatfee 6:a64d79286726 84
Dogcatfee 6:a64d79286726 85 //pc.printf(" servo pos %d %ld\n ", 0 , my_servo.get());
Dogcatfee 6:a64d79286726 86 //pc.printf(" Largest degree %d %ld\n ", position_array[largesti], reading_array[largesti]);
Dogcatfee 6:a64d79286726 87 my_servo.set(position_array[largesti]);
Dogcatfee 6:a64d79286726 88
Dogcatfee 6:a64d79286726 89 wait(8);
Dogcatfee 6:a64d79286726 90
ziadeldebri 4:b3a93554fedf 91 }
Dogcatfee 6:a64d79286726 92 }