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:13:10 2016 +0000
Revision:
9:4bd820ca8300
Parent:
7:10554a04a34f
Child:
10:37cbc2888e09
Added max_degree parameter;

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 9:4bd820ca8300 17 const int max_degree = 120;
Dogcatfee 6:a64d79286726 18 const int size=20;
Dogcatfee 6:a64d79286726 19 unsigned int reading_array[size];
Dogcatfee 6:a64d79286726 20 int position_array[size];
Dogcatfee 6:a64d79286726 21 //uint16_t largest = 0;
Dogcatfee 6:a64d79286726 22 unsigned int largest = 0;
ziadeldebri 4:b3a93554fedf 23 int largesti = 0;
ziadeldebri 4:b3a93554fedf 24 int i;
ziadeldebri 3:b787aa49b900 25
Dogcatfee 6:a64d79286726 26 //Serial pc(USBTX, USBRX);
Dogcatfee 6:a64d79286726 27
Dogcatfee 6:a64d79286726 28 int main()
Dogcatfee 6:a64d79286726 29 {
Dogcatfee 6:a64d79286726 30
Dogcatfee 6:a64d79286726 31 for (i=0; i<size;i++)
Dogcatfee 6:a64d79286726 32 {
ziadeldebri 3:b787aa49b900 33
Dogcatfee 9:4bd820ca8300 34 position_array[i] = i*max_degree/size +20 ;
Dogcatfee 6:a64d79286726 35
Dogcatfee 6:a64d79286726 36 }
Dogcatfee 6:a64d79286726 37
Dogcatfee 6:a64d79286726 38 while (1)
Dogcatfee 6:a64d79286726 39 {
Dogcatfee 6:a64d79286726 40 for (i = 0; i < size; i++)
Dogcatfee 6:a64d79286726 41 {
ziadeldebri 0:61b18b631f94 42
Dogcatfee 6:a64d79286726 43 //set position
Dogcatfee 6:a64d79286726 44 my_servo.set(position_array[i]);
Dogcatfee 6:a64d79286726 45
Dogcatfee 6:a64d79286726 46 //pc.printf(" Reading array %d %ld\n ", i , reading_array[i]);
Dogcatfee 6:a64d79286726 47
Dogcatfee 6:a64d79286726 48 //check position
Dogcatfee 6:a64d79286726 49
Dogcatfee 6:a64d79286726 50 while(1)
Dogcatfee 6:a64d79286726 51 {
Dogcatfee 6:a64d79286726 52 //one but not both, XOR
Dogcatfee 6:a64d79286726 53 if((my_servo.get()-position_array[i] > -25 ) ^ (my_servo.get() - position_array[i] <25))
Dogcatfee 6:a64d79286726 54 {
ziadeldebri 3:b787aa49b900 55
Dogcatfee 6:a64d79286726 56 break;
Dogcatfee 6:a64d79286726 57 }
Dogcatfee 6:a64d79286726 58
Dogcatfee 6:a64d79286726 59 else
Dogcatfee 6:a64d79286726 60 {
Dogcatfee 6:a64d79286726 61 my_servo.set(position_array[i]);
Dogcatfee 6:a64d79286726 62 wait(.2);
Dogcatfee 6:a64d79286726 63 }
Dogcatfee 6:a64d79286726 64
Dogcatfee 6:a64d79286726 65
Dogcatfee 6:a64d79286726 66 }
Dogcatfee 6:a64d79286726 67
Dogcatfee 6:a64d79286726 68 //write analog values (integers) to array
Dogcatfee 6:a64d79286726 69 reading_array[i] = Spanel.read()*10000;
Dogcatfee 6:a64d79286726 70 }
ziadeldebri 4:b3a93554fedf 71
ziadeldebri 4:b3a93554fedf 72
ziadeldebri 4:b3a93554fedf 73
Dogcatfee 6:a64d79286726 74 for (i = 0; i < size; i++)
Dogcatfee 6:a64d79286726 75 {
Dogcatfee 6:a64d79286726 76 if (reading_array[i] > largest)
Dogcatfee 6:a64d79286726 77 {
ziadeldebri 3:b787aa49b900 78
Dogcatfee 6:a64d79286726 79 largest = reading_array[i];
Dogcatfee 6:a64d79286726 80 largesti = i;
Dogcatfee 6:a64d79286726 81
Dogcatfee 6:a64d79286726 82 }
Dogcatfee 6:a64d79286726 83
ziadeldebri 4:b3a93554fedf 84 }
Dogcatfee 6:a64d79286726 85
Dogcatfee 6:a64d79286726 86 //pc.printf(" servo pos %d %ld\n ", 0 , my_servo.get());
Dogcatfee 6:a64d79286726 87 //pc.printf(" Largest degree %d %ld\n ", position_array[largesti], reading_array[largesti]);
Dogcatfee 6:a64d79286726 88 my_servo.set(position_array[largesti]);
Dogcatfee 6:a64d79286726 89
Dogcatfee 6:a64d79286726 90 wait(8);
Dogcatfee 6:a64d79286726 91
ziadeldebri 4:b3a93554fedf 92 }
Dogcatfee 6:a64d79286726 93 }