ECE 4180 Final Project

Dependencies:   mbed PinDetect

Committer:
amitchell41
Date:
Wed Dec 12 17:11:24 2018 +0000
Revision:
3:660252b9fa29
Parent:
2:59677142d3dd
Final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
amitchell41 0:eff94d72c9f4 1 #include "mbed.h"
amitchell41 0:eff94d72c9f4 2 #include "Servo.h"
amitchell41 0:eff94d72c9f4 3 #include "MMA8452.h"
amitchell41 0:eff94d72c9f4 4 #include "PinDetect.h"
amitchell41 2:59677142d3dd 5
amitchell41 3:660252b9fa29 6 Serial pc(USBTX,USBRX);
amitchell41 3:660252b9fa29 7 DigitalOut myLed(LED1);
amitchell41 3:660252b9fa29 8 PinDetect pb1(p26);
amitchell41 3:660252b9fa29 9
amitchell41 3:660252b9fa29 10
amitchell41 2:59677142d3dd 11
amitchell41 3:660252b9fa29 12 DigitalOut Red4(p5);
amitchell41 3:660252b9fa29 13 DigitalOut Red3(p6);
amitchell41 3:660252b9fa29 14 DigitalOut Yel2(p7);
amitchell41 3:660252b9fa29 15 DigitalOut Grn1(p8);
amitchell41 3:660252b9fa29 16
amitchell41 0:eff94d72c9f4 17
amitchell41 3:660252b9fa29 18 Servo _base(p21);
amitchell41 3:660252b9fa29 19 Servo _shoulder(p22);
amitchell41 3:660252b9fa29 20 Servo _elbow(p23);
amitchell41 3:660252b9fa29 21 Servo _sword(p24);
amitchell41 3:660252b9fa29 22
amitchell41 2:59677142d3dd 23
amitchell41 2:59677142d3dd 24 void countdown(){
amitchell41 2:59677142d3dd 25 Red4 = 1;
amitchell41 3:660252b9fa29 26 wait(.75);
amitchell41 2:59677142d3dd 27 Red4 = 0;
amitchell41 2:59677142d3dd 28 Red3 = 1;
amitchell41 3:660252b9fa29 29 wait(.75);
amitchell41 2:59677142d3dd 30 Red3 = 0;
amitchell41 2:59677142d3dd 31 Yel2 = 1;
amitchell41 3:660252b9fa29 32 wait(.75);
amitchell41 2:59677142d3dd 33 Red4 = 1;
amitchell41 2:59677142d3dd 34 Red3 = 1;
amitchell41 2:59677142d3dd 35 Grn1 = 1;
amitchell41 3:660252b9fa29 36 wait(.75);
amitchell41 2:59677142d3dd 37 Red4 = 0;
amitchell41 2:59677142d3dd 38 Red3 = 0;
amitchell41 2:59677142d3dd 39 Yel2 = 0;
amitchell41 2:59677142d3dd 40 Grn1 = 0;
amitchell41 2:59677142d3dd 41 }
amitchell41 0:eff94d72c9f4 42
amitchell41 3:660252b9fa29 43
amitchell41 0:eff94d72c9f4 44 void swing(){
amitchell41 2:59677142d3dd 45 _sword.write(1.0);
amitchell41 0:eff94d72c9f4 46 wait(.1);
amitchell41 2:59677142d3dd 47 _sword.write(0.0);
amitchell41 0:eff94d72c9f4 48 }
amitchell41 0:eff94d72c9f4 49
amitchell41 0:eff94d72c9f4 50 int main() {
amitchell41 0:eff94d72c9f4 51
amitchell41 0:eff94d72c9f4 52 pb1.mode(PullUp);
amitchell41 1:aaa03bac6dd6 53 int timer = 0;
amitchell41 3:660252b9fa29 54 //float _baseRead = 0, _shoulderRead = 0, _elbowRead = 0;
amitchell41 0:eff94d72c9f4 55 double x = 0, y = 0, z = 0;
amitchell41 0:eff94d72c9f4 56 MMA8452 acc(p28, p27, 40000); //instantiate an acc object
amitchell41 0:eff94d72c9f4 57
amitchell41 0:eff94d72c9f4 58 //set parameters -- use these and don't change
amitchell41 0:eff94d72c9f4 59 acc.setBitDepth(MMA8452::BIT_DEPTH_12);
amitchell41 0:eff94d72c9f4 60 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
amitchell41 0:eff94d72c9f4 61 acc.setDataRate(MMA8452::RATE_100);
amitchell41 3:660252b9fa29 62 countdown();
amitchell41 0:eff94d72c9f4 63
amitchell41 0:eff94d72c9f4 64 while(1) {
amitchell41 3:660252b9fa29 65
amitchell41 0:eff94d72c9f4 66
amitchell41 0:eff94d72c9f4 67 acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers
amitchell41 1:aaa03bac6dd6 68
amitchell41 3:660252b9fa29 69 _base.write((-y+1)/2);
amitchell41 3:660252b9fa29 70 _shoulder.write((-x+1)/2);
amitchell41 3:660252b9fa29 71 //_elbow.write((x+1)/2);
amitchell41 1:aaa03bac6dd6 72
amitchell41 0:eff94d72c9f4 73 if(!pb1){
amitchell41 0:eff94d72c9f4 74 myLed=1;
amitchell41 0:eff94d72c9f4 75 swing();
amitchell41 0:eff94d72c9f4 76 myLed=0;
amitchell41 0:eff94d72c9f4 77 }
amitchell41 1:aaa03bac6dd6 78
amitchell41 0:eff94d72c9f4 79 // You can uncomment this line to see the values coming off the MMA8452
amitchell41 3:660252b9fa29 80 //printf("(%.2f,%.2f,%.2f)", x,y,z);
amitchell41 3:660252b9fa29 81 //printf("(%.5f,%.5f,%.5f)", _baseRead, _shoulderRead, _elbowRead);
amitchell41 3:660252b9fa29 82 //printf("\n");
amitchell41 0:eff94d72c9f4 83
amitchell41 1:aaa03bac6dd6 84 timer++;
amitchell41 1:aaa03bac6dd6 85 } //infinite while loop
amitchell41 1:aaa03bac6dd6 86 } //end main