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
00001 //Assignment 5 Exercise 2 00002 //Author: Michael Antonucci 00003 //Last Modified: 11/15/2022 00004 00005 #include "mbed.h" 00006 00007 DigitalOut myled(LED1); //Specify inputs and variables 00008 DigitalIn button(p17); 00009 Serial pc(USBTX,USBRX); 00010 PwmOut servo(p21); 00011 Timer timePressed; 00012 00013 00014 void servo_angle(float angle) //declare the angle function 00015 { 00016 float pulseCoeff = 10.0; 00017 float pulseOffset = 400; 00018 float pulseWidth; 00019 00020 //Make sure angles are within bounds 00021 if (angle < 0) { 00022 angle = 0; 00023 } else if (angle > 180) { 00024 angle = 180.0; 00025 } 00026 00027 //Calculate the pulsewidth for the desired angle and issue command: 00028 pulseWidth = pulseCoeff * angle + pulseOffset; 00029 servo.pulsewidth(pulseWidth/1000000.000); 00030 } 00031 00032 int main() 00033 { 00034 wait(3); 00035 float angle; //store angle data 00036 angle = 0; //set to zero 00037 servo_angle(angle); 00038 pc.printf("The servo angle is: %5.2f\r\n",angle); 00039 while(1) { 00040 00041 while(button.read()==0) { //while button is not pressed 00042 wait(0.1); 00043 if (button.read()==1) { //if pressed 00044 angle=angle+30; 00045 servo_angle(angle); 00046 timePressed.start(); //Start button timer 00047 pc.printf("The servo angle is: %5.2f\r\n",angle); 00048 } 00049 } 00050 while(button.read()==1) { //while button is pressed 00051 wait(0.1); 00052 if (button.read()==0) { //if released 00053 timePressed.stop(); //Stop button timer 00054 } 00055 } 00056 if (angle >= 180) { //Reset Servo if angle is greater than 180 00057 angle = 0; 00058 servo_angle(angle); 00059 pc.printf("The servo angle is: %5.2f\r\n",angle); 00060 } 00061 if (timePressed.read_ms()>= 3000) { //Reset servo if timer is greater than 3 seconds 00062 angle = 0; 00063 servo_angle(angle); 00064 pc.printf("The servo angle is: %5.2f\r\n",angle); 00065 } 00066 timePressed.reset(); //Reset button timer 00067 00068 } 00069 }
Generated on Wed Nov 16 2022 04:53:28 by
1.7.2