servos

Dependencies:   Servo mbed

Committer:
m215910
Date:
Fri Oct 12 13:54:00 2018 +0000
Revision:
1:46040c1dc16d
Parent:
main.cpp@0:4e3507ef1416
servos;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m215910 0:4e3507ef1416 1
m215910 0:4e3507ef1416 2
m215910 0:4e3507ef1416 3 //#include "Motor.h" //must change line 31 in Motor.cpp according to Lab
m215910 0:4e3507ef1416 4 #include "mbed.h"
m215910 0:4e3507ef1416 5 #include "Servo.h"
m215910 0:4e3507ef1416 6
m215910 0:4e3507ef1416 7
m215910 0:4e3507ef1416 8 float pos = rand()/(float)RAND_MAX; //allows the servo to start at a random position
m215910 0:4e3507ef1416 9 float pos2 = rand()/(float)RAND_MAX;
m215910 0:4e3507ef1416 10 Servo servo1(p21);
m215910 0:4e3507ef1416 11 Servo servo2(p22);
m215910 0:4e3507ef1416 12 DigitalIn sw1(p16);
m215910 0:4e3507ef1416 13 int sw1state;
m215910 0:4e3507ef1416 14 int i=1;
m215910 0:4e3507ef1416 15
m215910 0:4e3507ef1416 16 int main() {
m215910 0:4e3507ef1416 17
m215910 0:4e3507ef1416 18 servo1.calibrate(0.0009, 90.0); //need to calibrate the servos so they don't freak out
m215910 0:4e3507ef1416 19 servo2.calibrate(0.0009, 90.0);
m215910 0:4e3507ef1416 20 servo1=pos; //used to help calibrate
m215910 0:4e3507ef1416 21 servo2=pos2;
m215910 0:4e3507ef1416 22
m215910 0:4e3507ef1416 23 while(1) {
m215910 0:4e3507ef1416 24 pos=0;
m215910 0:4e3507ef1416 25 pos2=0;
m215910 0:4e3507ef1416 26 sw1state=sw1.read();
m215910 0:4e3507ef1416 27
m215910 0:4e3507ef1416 28 if((sw1state) == 1) { //if switch is on move servos quickly to other angle
m215910 0:4e3507ef1416 29 servo1 = i;
m215910 0:4e3507ef1416 30 servo2 = i;}
m215910 0:4e3507ef1416 31 if((sw1state) == 0) { //if switch is off move back
m215910 0:4e3507ef1416 32 servo1 = 0;
m215910 0:4e3507ef1416 33 servo2 = 0;}
m215910 0:4e3507ef1416 34 }}