ITI "DallaChiesa" 2019
/
servomotore
servom
main.cpp@1:ead492739e6c, 2019-03-28 (annotated)
- Committer:
- faraonemena
- Date:
- Thu Mar 28 15:56:14 2019 +0000
- Revision:
- 1:ead492739e6c
- Parent:
- 0:c48a837ce951
servom
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faraonemena | 0:c48a837ce951 | 1 | /* Copyright (c) 2017 STMicroelectronics |
faraonemena | 0:c48a837ce951 | 2 | * |
faraonemena | 0:c48a837ce951 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
faraonemena | 0:c48a837ce951 | 4 | * you may not use this file except in compliance with the License. |
faraonemena | 0:c48a837ce951 | 5 | * You may obtain a copy of the License at |
faraonemena | 0:c48a837ce951 | 6 | * |
faraonemena | 0:c48a837ce951 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
faraonemena | 0:c48a837ce951 | 8 | * |
faraonemena | 0:c48a837ce951 | 9 | * Unless required by applicable law or agreed to in writing, software |
faraonemena | 0:c48a837ce951 | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
faraonemena | 0:c48a837ce951 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
faraonemena | 0:c48a837ce951 | 12 | * See the License for the specific language governing permissions and |
faraonemena | 0:c48a837ce951 | 13 | * limitations under the License. |
faraonemena | 0:c48a837ce951 | 14 | */ |
faraonemena | 0:c48a837ce951 | 15 | |
faraonemena | 0:c48a837ce951 | 16 | /**************************************************** |
faraonemena | 0:c48a837ce951 | 17 | * RAPID PROTOTYPING WITH NUCLEO * |
faraonemena | 0:c48a837ce951 | 18 | * Example Code 11: Servo pwm * |
faraonemena | 0:c48a837ce951 | 19 | * Author: Mauro D'Angelo * |
faraonemena | 0:c48a837ce951 | 20 | * Organization: STMicroelectronics * |
faraonemena | 0:c48a837ce951 | 21 | *****************************************************/ |
faraonemena | 0:c48a837ce951 | 22 | |
faraonemena | 0:c48a837ce951 | 23 | #include "mbed.h" |
faraonemena | 0:c48a837ce951 | 24 | #include "Servo.h" |
faraonemena | 1:ead492739e6c | 25 | //#include "SG90.h" |
faraonemena | 1:ead492739e6c | 26 | |
faraonemena | 0:c48a837ce951 | 27 | |
faraonemena | 0:c48a837ce951 | 28 | //Instantiate an object of Servo type on pin PB_3 named servo |
faraonemena | 0:c48a837ce951 | 29 | Servo servo(PB_3); |
faraonemena | 0:c48a837ce951 | 30 | //Instantiate an object of DigitalOut type on pin LED1 named myled |
faraonemena | 0:c48a837ce951 | 31 | DigitalOut myled(LED1); |
faraonemena | 0:c48a837ce951 | 32 | |
faraonemena | 0:c48a837ce951 | 33 | //Instantiate an object of Serial type on Tx and Rx pin of USB port named pc (being the USB port connected to PC) |
faraonemena | 0:c48a837ce951 | 34 | Serial pc(USBTX, USBRX); |
faraonemena | 0:c48a837ce951 | 35 | |
faraonemena | 0:c48a837ce951 | 36 | |
faraonemena | 0:c48a837ce951 | 37 | //Entry point |
faraonemena | 0:c48a837ce951 | 38 | int main() { |
faraonemena | 0:c48a837ce951 | 39 | |
faraonemena | 0:c48a837ce951 | 40 | //Define and set angle variable |
faraonemena | 0:c48a837ce951 | 41 | int angle = 0; |
faraonemena | 0:c48a837ce951 | 42 | |
faraonemena | 0:c48a837ce951 | 43 | while(1) { |
faraonemena | 0:c48a837ce951 | 44 | //Modify servo position |
faraonemena | 1:ead492739e6c | 45 | |
faraonemena | 0:c48a837ce951 | 46 | servo.write(angle); |
faraonemena | 0:c48a837ce951 | 47 | |
faraonemena | 0:c48a837ce951 | 48 | // Print angle |
faraonemena | 0:c48a837ce951 | 49 | pc.printf("Servo set at %d degrees\r\n", angle); |
faraonemena | 0:c48a837ce951 | 50 | |
faraonemena | 0:c48a837ce951 | 51 | // Modify LED status (toggle) |
faraonemena | 0:c48a837ce951 | 52 | myled = !myled; |
faraonemena | 0:c48a837ce951 | 53 | |
faraonemena | 0:c48a837ce951 | 54 | //Increase angle value |
faraonemena | 0:c48a837ce951 | 55 | angle+=10; |
faraonemena | 0:c48a837ce951 | 56 | |
faraonemena | 0:c48a837ce951 | 57 | //If angle increases more than 180 degrees, reset |
faraonemena | 0:c48a837ce951 | 58 | if(angle>360) |
faraonemena | 0:c48a837ce951 | 59 | angle=0; |
faraonemena | 0:c48a837ce951 | 60 | |
faraonemena | 0:c48a837ce951 | 61 | wait(0.25); |
faraonemena | 0:c48a837ce951 | 62 | } |
faraonemena | 0:c48a837ce951 | 63 | } |