stepper mottor for pumps

Dependencies:   mbed

Committer:
ekasinambela
Date:
Sun Jun 19 21:32:08 2016 +0000
Revision:
1:8431b042a2eb
modify stepper motor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ekasinambela 1:8431b042a2eb 1 #include "Ping.h"
ekasinambela 1:8431b042a2eb 2
ekasinambela 1:8431b042a2eb 3 #include "mbed.h"
ekasinambela 1:8431b042a2eb 4
ekasinambela 1:8431b042a2eb 5 Ping::Ping(PinName PING_PIN)
ekasinambela 1:8431b042a2eb 6 : _event(PING_PIN)
ekasinambela 1:8431b042a2eb 7 , _cmd(PING_PIN)
ekasinambela 1:8431b042a2eb 8 , _timer()
ekasinambela 1:8431b042a2eb 9 {
ekasinambela 1:8431b042a2eb 10 _event.rise(this,&Ping::_Starts);
ekasinambela 1:8431b042a2eb 11 _event.fall(this,&Ping::_Stops);
ekasinambela 1:8431b042a2eb 12 _SPEED_OF_SOUND_CM = 33;
ekasinambela 1:8431b042a2eb 13 }
ekasinambela 1:8431b042a2eb 14
ekasinambela 1:8431b042a2eb 15 void Ping::_Starts(void)
ekasinambela 1:8431b042a2eb 16 {
ekasinambela 1:8431b042a2eb 17 _Valid = false; // start the timere, and invalidate the current time.
ekasinambela 1:8431b042a2eb 18 _Busy = true;
ekasinambela 1:8431b042a2eb 19 _timer.start();
ekasinambela 1:8431b042a2eb 20 _Time = _timer.read_us();
ekasinambela 1:8431b042a2eb 21 }
ekasinambela 1:8431b042a2eb 22
ekasinambela 1:8431b042a2eb 23 void Ping::_Stops(void)
ekasinambela 1:8431b042a2eb 24 {
ekasinambela 1:8431b042a2eb 25 _Valid = true; // When it stops, update the time
ekasinambela 1:8431b042a2eb 26 _Busy = false;
ekasinambela 1:8431b042a2eb 27 _Time = _timer.read_us()-_Time;
ekasinambela 1:8431b042a2eb 28 }
ekasinambela 1:8431b042a2eb 29
ekasinambela 1:8431b042a2eb 30 void Ping::Send()
ekasinambela 1:8431b042a2eb 31 {
ekasinambela 1:8431b042a2eb 32
ekasinambela 1:8431b042a2eb 33 _cmd.output();
ekasinambela 1:8431b042a2eb 34 _cmd.write(0); // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf
ekasinambela 1:8431b042a2eb 35 wait_us(3);
ekasinambela 1:8431b042a2eb 36 _cmd.write(1);
ekasinambela 1:8431b042a2eb 37 wait_us(3);
ekasinambela 1:8431b042a2eb 38 _cmd.write(0);
ekasinambela 1:8431b042a2eb 39 _cmd.input();
ekasinambela 1:8431b042a2eb 40
ekasinambela 1:8431b042a2eb 41 }
ekasinambela 1:8431b042a2eb 42 void Ping::Set_Speed_of_Sound(int SoS_ms )
ekasinambela 1:8431b042a2eb 43 {
ekasinambela 1:8431b042a2eb 44 _SPEED_OF_SOUND_CM = SoS_ms;
ekasinambela 1:8431b042a2eb 45 }
ekasinambela 1:8431b042a2eb 46
ekasinambela 1:8431b042a2eb 47 int Ping::Read_cm()
ekasinambela 1:8431b042a2eb 48 // -1 means not valid.
ekasinambela 1:8431b042a2eb 49 {
ekasinambela 1:8431b042a2eb 50 if(_Valid && ~_Busy)
ekasinambela 1:8431b042a2eb 51 return ((_Time*_SPEED_OF_SOUND_CM)/1000);
ekasinambela 1:8431b042a2eb 52 else
ekasinambela 1:8431b042a2eb 53 return -1;
ekasinambela 1:8431b042a2eb 54 }