ECE 4180 Final Project

Dependencies:   mbed PinDetect

Committer:
amitchell41
Date:
Tue Dec 11 00:56:05 2018 +0000
Revision:
2:59677142d3dd
Parent:
1:aaa03bac6dd6
Child:
3:660252b9fa29
*attempted to* add led support

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 0:eff94d72c9f4 5
amitchell41 2:59677142d3dd 6 Serial pc(USBTX,USBRX);
amitchell41 2:59677142d3dd 7 DigitalOut myLed(LED1);
amitchell41 2:59677142d3dd 8 PinDetect pb1(p20);
amitchell41 2:59677142d3dd 9
amitchell41 2:59677142d3dd 10 DigitalOut Comm(p11); //If master
amitchell41 2:59677142d3dd 11 //DigitalIn Comm(p11); //If slave
amitchell41 2:59677142d3dd 12
amitchell41 2:59677142d3dd 13 DigitalOut Red4(p5);
amitchell41 2:59677142d3dd 14 DigitalOut Red3(p6);
amitchell41 2:59677142d3dd 15 DigitalOut Yel2(p7);
amitchell41 2:59677142d3dd 16 DigitalOut Grn1(p8);
amitchell41 0:eff94d72c9f4 17
amitchell41 0:eff94d72c9f4 18 Servo _base(p21);
amitchell41 0:eff94d72c9f4 19 Servo _shoulder(p22);
amitchell41 0:eff94d72c9f4 20 Servo _elbow(p23);
amitchell41 2:59677142d3dd 21 Servo _sword(p24);
amitchell41 2:59677142d3dd 22
amitchell41 2:59677142d3dd 23 void countdown(){
amitchell41 2:59677142d3dd 24 Comm = 1;
amitchell41 2:59677142d3dd 25 Red4 = 1;
amitchell41 2:59677142d3dd 26 wait(1);
amitchell41 2:59677142d3dd 27 Red4 = 0;
amitchell41 2:59677142d3dd 28 Red3 = 1;
amitchell41 2:59677142d3dd 29 wait(1);
amitchell41 2:59677142d3dd 30 Red3 = 0;
amitchell41 2:59677142d3dd 31 Yel2 = 1;
amitchell41 2:59677142d3dd 32 wait(1);
amitchell41 2:59677142d3dd 33 Red4 = 1;
amitchell41 2:59677142d3dd 34 Red3 = 1;
amitchell41 2:59677142d3dd 35 Grn1 = 1;
amitchell41 2:59677142d3dd 36 wait(1);
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 Comm = 0;
amitchell41 2:59677142d3dd 42 }
amitchell41 0:eff94d72c9f4 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 0:eff94d72c9f4 54 double x = 0, y = 0, z = 0;
amitchell41 0:eff94d72c9f4 55 MMA8452 acc(p28, p27, 40000); //instantiate an acc object
amitchell41 0:eff94d72c9f4 56
amitchell41 0:eff94d72c9f4 57 //set parameters -- use these and don't change
amitchell41 0:eff94d72c9f4 58 acc.setBitDepth(MMA8452::BIT_DEPTH_12);
amitchell41 0:eff94d72c9f4 59 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
amitchell41 0:eff94d72c9f4 60 acc.setDataRate(MMA8452::RATE_100);
amitchell41 0:eff94d72c9f4 61
amitchell41 2:59677142d3dd 62 countdown(); //If master
amitchell41 2:59677142d3dd 63
amitchell41 2:59677142d3dd 64 /*
amitchell41 2:59677142d3dd 65 while(Comm = 1) {
amitchell41 2:59677142d3dd 66 wait(.1);
amitchell41 2:59677142d3dd 67 }
amitchell41 2:59677142d3dd 68 */
amitchell41 0:eff94d72c9f4 69
amitchell41 0:eff94d72c9f4 70 while(1) {
amitchell41 0:eff94d72c9f4 71
amitchell41 0:eff94d72c9f4 72 acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers
amitchell41 1:aaa03bac6dd6 73
amitchell41 1:aaa03bac6dd6 74 _base.write((y+1)/2);
amitchell41 1:aaa03bac6dd6 75 _shoulder.write((x+1)/2);
amitchell41 2:59677142d3dd 76 _elbow.write((x+1)/4);
amitchell41 1:aaa03bac6dd6 77
amitchell41 0:eff94d72c9f4 78 if(!pb1){
amitchell41 0:eff94d72c9f4 79 myLed=1;
amitchell41 0:eff94d72c9f4 80 swing();
amitchell41 0:eff94d72c9f4 81 myLed=0;
amitchell41 0:eff94d72c9f4 82 }
amitchell41 1:aaa03bac6dd6 83
amitchell41 0:eff94d72c9f4 84 // You can uncomment this line to see the values coming off the MMA8452
amitchell41 1:aaa03bac6dd6 85 //printf("\n(%.2f,%.2f,%.2f)", x,y,z);
amitchell41 0:eff94d72c9f4 86
amitchell41 1:aaa03bac6dd6 87 timer++;
amitchell41 1:aaa03bac6dd6 88 } //infinite while loop
amitchell41 1:aaa03bac6dd6 89 } //end main