Test code

Dependencies:   mbed Motor Servo

Committer:
m215910
Date:
Fri Oct 12 14:45:12 2018 +0000
Revision:
2:17956bff3ff2
Parent:
1:bd813dd636a9
Child:
3:638dced18d87
kind of works, motor updates, need to fix button;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m215910 1:bd813dd636a9 1 //Maria Satre, Riki Gagnon, Theo Boskov, Janelle Stewart
m215910 1:bd813dd636a9 2 //Project 2
m215910 1:bd813dd636a9 3 //Goat Launcher
m215910 1:bd813dd636a9 4
m215910 1:bd813dd636a9 5 //Libraries and mechanical directives
m215910 1:bd813dd636a9 6 #include "Motor.h" //must change line 31 in Motor.cpp according to Lab
m215910 0:755c12c1a9ad 7 #include "mbed.h"
m215910 1:bd813dd636a9 8 #include "Servo.h" //need Servo library
m215910 0:755c12c1a9ad 9
m215910 2:17956bff3ff2 10 Motor m(p29, p30, p26); //designates the pins used by the motor
m215910 2:17956bff3ff2 11 Servo servo1(p21);
m215910 2:17956bff3ff2 12 Servo servo2(p22);
m215910 2:17956bff3ff2 13 DigitalIn sw1(p16); //switches or buttons
m215910 2:17956bff3ff2 14 DigitalIn bu1(p24); //Army man
m215910 2:17956bff3ff2 15 //DigitalIn bu2(p); //Army man
m215910 2:17956bff3ff2 16 //DigitalIn bu3(p); //Army man
m215910 2:17956bff3ff2 17 //DigitalIn bu4(p); //Motor clockwise
m215910 2:17956bff3ff2 18 //DigitalIn bu5(p); //Motor CC
m215910 2:17956bff3ff2 19 DigitalOut leds[3]={p25, p26, p27};
m215910 1:bd813dd636a9 20 float pos = rand()/(float)RAND_MAX; //allows the servo to start at a random position
m215910 1:bd813dd636a9 21 float pos2 = rand()/(float)RAND_MAX;
m215910 1:bd813dd636a9 22 int sw1state, bu1state, bu2state, bu3state, bu4state, bu5state; //the state of the switch
m215910 1:bd813dd636a9 23 int s=0.2; //speed of motor rotation - fairly slow so as to be exact
m215910 1:bd813dd636a9 24
m215910 0:755c12c1a9ad 25
m215910 0:755c12c1a9ad 26 int main() {
m215910 1:bd813dd636a9 27 servo1.calibrate(0.0009, 90.0); //need to calibrate the servos so they don't freak out
m215910 1:bd813dd636a9 28 servo2.calibrate(0.0009, 90.0);
m215910 1:bd813dd636a9 29 servo1=pos; //used to help calibrate
m215910 1:bd813dd636a9 30 servo2=pos2;
m215910 1:bd813dd636a9 31
m215910 1:bd813dd636a9 32 while(1) { //do we need while?
m215910 1:bd813dd636a9 33 sw1state=sw1.read(); //read in switch state
m215910 1:bd813dd636a9 34 bu1state=bu1.read();
m215910 2:17956bff3ff2 35 //bu2state=bu2.read();
m215910 2:17956bff3ff2 36 //bu3state=bu3.read();
m215910 2:17956bff3ff2 37 //bu4state=bu4.read();
m215910 2:17956bff3ff2 38 //bu5state=bu5.read();
m215910 2:17956bff3ff2 39 pos=0;
m215910 2:17956bff3ff2 40 pos2=0;
m215910 2:17956bff3ff2 41
m215910 2:17956bff3ff2 42 if (sw1state == 1) { //if bu4 is on rotate motor fwd
m215910 2:17956bff3ff2 43 m.speed(s);
m215910 2:17956bff3ff2 44 s=1;}
m215910 2:17956bff3ff2 45 if (sw1state == 0) { //if bu5 is on rotate motor rev
m215910 2:17956bff3ff2 46 m.speed(-s);
m215910 2:17956bff3ff2 47 s=0;}
m215910 1:bd813dd636a9 48
m215910 1:bd813dd636a9 49
m215910 1:bd813dd636a9 50 if (bu1state == 0) { //if ARMY button is off light up LEDS
m215910 1:bd813dd636a9 51 leds[0]=1;}
m215910 1:bd813dd636a9 52 if (bu2state == 0) {
m215910 1:bd813dd636a9 53 leds[1]=1;}
m215910 1:bd813dd636a9 54 if (bu3state == 0) {
m215910 1:bd813dd636a9 55 leds[2]=1;}
m215910 1:bd813dd636a9 56 if (bu1state == 1) { //if ARMY button is on nothing happens - ARMY guys with weight on LEDS
m215910 1:bd813dd636a9 57 leds[0]=0;}
m215910 1:bd813dd636a9 58 if (bu2state == 1) {
m215910 1:bd813dd636a9 59 leds[1]=0;}
m215910 1:bd813dd636a9 60 if (bu3state == 1) {
m215910 1:bd813dd636a9 61 leds[2]=0;}
m215910 1:bd813dd636a9 62
m215910 1:bd813dd636a9 63
m215910 2:17956bff3ff2 64 //if((sw1state) == 1) { //if switch is on move servos quickly to other angle
m215910 2:17956bff3ff2 65 // (pos && pos2) = 1;}
m215910 2:17956bff3ff2 66 //if((sw1state) == 1) { //if switch is off move back
m215910 2:17956bff3ff2 67 // (pos && pos2) = 0;}
m215910 1:bd813dd636a9 68
m215910 1:bd813dd636a9 69 } //end while
m215910 1:bd813dd636a9 70 } //end main