Honmaka Astro MicroControler sample PluseMotor Sample Ver 3

Dependencies:   mbed

Fork of PulseMotor2 by Honmaka Astro

Committer:
Honmaka
Date:
Sun Jan 11 08:28:16 2015 +0000
Revision:
2:61ec30f77bc3
Parent:
1:c533a17dccc0
PulseMotor Ver3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Honmaka 0:27fe3b05f1fc 1 #include "mbed.h"
Honmaka 0:27fe3b05f1fc 2
Honmaka 0:27fe3b05f1fc 3 PwmOut PulseMotor(p21);
Honmaka 1:c533a17dccc0 4 DigitalIn MotorSpeedSw(p5);
Honmaka 1:c533a17dccc0 5 DigitalIn MotorDirectionSw(p6);
Honmaka 1:c533a17dccc0 6 DigitalOut MotorDirectionOut(p7);
Honmaka 0:27fe3b05f1fc 7
Honmaka 2:61ec30f77bc3 8 //Add push SW
Honmaka 2:61ec30f77bc3 9 DigitalIn WestPushSw(p8);
Honmaka 2:61ec30f77bc3 10 DigitalIn EastPushSw(p9);
Honmaka 2:61ec30f77bc3 11
Honmaka 0:27fe3b05f1fc 12 int main() {
Honmaka 2:61ec30f77bc3 13 //Add
Honmaka 2:61ec30f77bc3 14 int PreStateWest = 1;
Honmaka 2:61ec30f77bc3 15 int PreStateEast = 1;
Honmaka 2:61ec30f77bc3 16
Honmaka 1:c533a17dccc0 17 MotorSpeedSw.mode(PullUp);
Honmaka 1:c533a17dccc0 18 MotorDirectionSw.mode(PullUp);
Honmaka 1:c533a17dccc0 19 //Dorection
Honmaka 1:c533a17dccc0 20 MotorDirectionOut = MotorDirectionSw;
Honmaka 1:c533a17dccc0 21 //Speed
Honmaka 1:c533a17dccc0 22 if (MotorSpeedSw == 1) { //Normal Speed
Honmaka 1:c533a17dccc0 23 PulseMotor.period_us(83140);
Honmaka 1:c533a17dccc0 24 PulseMotor.pulsewidth_us(83140/2);
Honmaka 1:c533a17dccc0 25 }
Honmaka 1:c533a17dccc0 26 else { //Half Speed
Honmaka 1:c533a17dccc0 27 PulseMotor.period_us(83140*2);
Honmaka 1:c533a17dccc0 28 PulseMotor.pulsewidth_us((83140*2)/2);
Honmaka 2:61ec30f77bc3 29 }
Honmaka 2:61ec30f77bc3 30
Honmaka 2:61ec30f77bc3 31 //Add
Honmaka 2:61ec30f77bc3 32 WestPushSw.mode(PullUp);
Honmaka 2:61ec30f77bc3 33 EastPushSw.mode(PullUp);
Honmaka 2:61ec30f77bc3 34
Honmaka 0:27fe3b05f1fc 35 while(1) {
Honmaka 2:61ec30f77bc3 36 if (WestPushSw != PreStateWest) {
Honmaka 2:61ec30f77bc3 37 if (WestPushSw == 0) {
Honmaka 2:61ec30f77bc3 38 //pushed West SW
Honmaka 2:61ec30f77bc3 39 //double speed
Honmaka 2:61ec30f77bc3 40 PulseMotor.period_us(83140/2);
Honmaka 2:61ec30f77bc3 41 PulseMotor.pulsewidth_us((83140/2) /2);
Honmaka 2:61ec30f77bc3 42 }
Honmaka 2:61ec30f77bc3 43 else {
Honmaka 2:61ec30f77bc3 44 //relese West SW
Honmaka 2:61ec30f77bc3 45 //normal speed
Honmaka 2:61ec30f77bc3 46 PulseMotor.period_us(83140);
Honmaka 2:61ec30f77bc3 47 PulseMotor.pulsewidth_us(83140 /2);
Honmaka 2:61ec30f77bc3 48 }
Honmaka 2:61ec30f77bc3 49 }
Honmaka 2:61ec30f77bc3 50 if (EastPushSw != PreStateEast) {
Honmaka 2:61ec30f77bc3 51 if (EastPushSw == 0) {
Honmaka 2:61ec30f77bc3 52 //pushed East SW
Honmaka 2:61ec30f77bc3 53 //Stop
Honmaka 2:61ec30f77bc3 54 PulseMotor.period_us(83140);
Honmaka 2:61ec30f77bc3 55 PulseMotor.pulsewidth_us(0);
Honmaka 2:61ec30f77bc3 56 }
Honmaka 2:61ec30f77bc3 57 else {
Honmaka 2:61ec30f77bc3 58 //relese East SW
Honmaka 2:61ec30f77bc3 59 //normal speed
Honmaka 2:61ec30f77bc3 60 PulseMotor.period_us(83140);
Honmaka 2:61ec30f77bc3 61 PulseMotor.pulsewidth_us(83140 /2);
Honmaka 2:61ec30f77bc3 62 }
Honmaka 2:61ec30f77bc3 63 }
Honmaka 2:61ec30f77bc3 64 //Save SW state
Honmaka 2:61ec30f77bc3 65 PreStateWest = WestPushSw;
Honmaka 2:61ec30f77bc3 66 PreStateEast = EastPushSw;
Honmaka 2:61ec30f77bc3 67
Honmaka 2:61ec30f77bc3 68 wait(0.01);
Honmaka 0:27fe3b05f1fc 69 }
Honmaka 0:27fe3b05f1fc 70 }
Honmaka 2:61ec30f77bc3 71
Honmaka 2:61ec30f77bc3 72