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.
Diff: main.cpp
- Revision:
- 0:cad1a4329bd8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Nov 03 21:15:25 2020 +0000
@@ -0,0 +1,59 @@
+//Mark Schwarzer Assignment 6 Part 2
+#include "mbed.h"
+
+PwmOut servo(p19);
+AnalogIn Pcell(p20);
+DigitalIn switch_input(p17);
+Serial pc(USBTX,USBRX);
+float V; //voltage
+float angle;
+
+//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()
+{
+V=(3.3);
+wait(3);
+servo.period(0.01);
+float voltageLight;
+
+while(1) {
+ voltageLight= Pcell.read()*V;
+ angle=(180*V);
+ pc.printf("Servo Angle: %5.2f\r\n",angle);
+ pc.printf("Voltage;%f",voltageLight);
+ wait(1); }}