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:37:32 2016 +0000
Revision:
10:37cbc2888e09
Parent:
9:4bd820ca8300
Added unsigned int; Added 16-bit unsigned int; Added made max_degree constant;

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