![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Template project for University of York ELE00032C Lab 6
main.cpp@1:ec2d05bb8112, 2021-02-04 (annotated)
- Committer:
- ajp109
- Date:
- Thu Feb 04 16:39:47 2021 +0000
- Revision:
- 1:ec2d05bb8112
- Parent:
- 0:77209603a6fe
- Child:
- 2:3eacc92b984b
Initial commit / lab6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajp109 | 0:77209603a6fe | 1 | #include "mbed.h" |
ajp109 | 0:77209603a6fe | 2 | |
ajp109 | 1:ec2d05bb8112 | 3 | DigitalOut dir1(D2); |
ajp109 | 1:ec2d05bb8112 | 4 | DigitalOut dir2(D3); |
ajp109 | 1:ec2d05bb8112 | 5 | PwmOut pwm(D4); |
ajp109 | 1:ec2d05bb8112 | 6 | |
ajp109 | 1:ec2d05bb8112 | 7 | void motor(float speed) { |
ajp109 | 1:ec2d05bb8112 | 8 | if (speed > 0) { |
ajp109 | 1:ec2d05bb8112 | 9 | dir1 = true; |
ajp109 | 1:ec2d05bb8112 | 10 | dir2 = false; |
ajp109 | 1:ec2d05bb8112 | 11 | pwm = speed; |
ajp109 | 1:ec2d05bb8112 | 12 | } else { |
ajp109 | 1:ec2d05bb8112 | 13 | dir1 = false; |
ajp109 | 1:ec2d05bb8112 | 14 | dir2 = true; |
ajp109 | 1:ec2d05bb8112 | 15 | pwm = -speed; |
ajp109 | 1:ec2d05bb8112 | 16 | } |
ajp109 | 0:77209603a6fe | 17 | } |
ajp109 | 1:ec2d05bb8112 | 18 | |
ajp109 | 1:ec2d05bb8112 | 19 | int main() { |
ajp109 | 1:ec2d05bb8112 | 20 | while (true) { |
ajp109 | 1:ec2d05bb8112 | 21 | motor(1.0); |
ajp109 | 1:ec2d05bb8112 | 22 | thread_sleep_for(1000); |
ajp109 | 1:ec2d05bb8112 | 23 | motor(0.0); |
ajp109 | 1:ec2d05bb8112 | 24 | thread_sleep_for(1000); |
ajp109 | 1:ec2d05bb8112 | 25 | motor(-0.5); |
ajp109 | 1:ec2d05bb8112 | 26 | thread_sleep_for(1000); |
ajp109 | 1:ec2d05bb8112 | 27 | motor(0); |
ajp109 | 1:ec2d05bb8112 | 28 | thread_sleep_for(1000); |
ajp109 | 1:ec2d05bb8112 | 29 | } |
ajp109 | 1:ec2d05bb8112 | 30 | } |