Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- seanlane
- Date:
- 2020-11-03
- Revision:
- 0:01b067538192
- Child:
- 1:515b6f9e29b4
File content as of revision 0:01b067538192:
//Sean Lane, 100648027, 10/16/2020 //A6_1: PWM and Servo Control #include "mbed.h" PwmOut servo(p23); DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalIn button(p17); Serial pc(USBTX,USBRX); float newangle; //Run through a range of pulse widths, report to serial output: void servo_test(float period, float minPulse, float maxPulse, float pulseStep) { float currentPulse; servo.period(period); //set the PWM period //Vary the pulse width from minimum to maximum in steps. pc.printf("Commencing Servo Test\r\n"); for (currentPulse=minPulse; currentPulse<maxPulse; currentPulse=currentPulse+pulseStep) { servo.pulsewidth(currentPulse/1000000.000); //convert uSec to sec. pc.printf("Current pulse width is %f\r\n",currentPulse); wait(0.5); } pc.printf("Servo Test Finished\r\n"); } //Sends correct pulse width to servo to achieve desired angle: void servo_set_angle(float angle) { float pulseCoeff = 10.0; float pulseOffset = 400; float pulseWidth; //Check to make sure commanded angle is within min-max bounds: if (angle < 0) { angle = 0; } else if (angle > 180) { angle = 180.0; } //Calculate pulsewidth for the desired angle and issue command: pulseWidth = pulseCoeff * angle + pulseOffset; servo.pulsewidth(pulseWidth/1000000.000); } int main() { bool bs; //Leave some time to get connection set up properly wait(3); //test servo from 0 to 5000 uSec, steps of 500 servo_test(0.01,0,3000,50); //cycle through angles, repeat: float angle; servo.period(0.01); while(1) { bs=button.read(); angle = 0; pc.printf("Test servo behavior here!"); //Run through angles from 0 to 180 ad nauseum: for (angle=0.0; angle<180.0; newangle=angle+30.0) { servo_set_angle(newangle); if (bs==1) { pc.printf("Button press detected"); if (newangle=0.0) myled1=1; pc.printf("Servo Angle: %5.2f\r\n",newangle); if (newangle>0.0 and newangle>=90.0) myled2=1; pc.printf("Servo Angle: %5.2f\r\n",newangle); if (newangle>90.0 and newangle>=180.0) myled3=1; pc.printf("Servo Angle: %5.2f\r\n",newangle); if (bs==0) { pc.printf("Servo Angle: %5.2f\r\n",angle); pc.printf("No button press detected"); wait(1); } } } } }