Control a solenoid using a power MOSFET circuit
Dependencies: mbed
Revision 0:9a9311d745c3, committed 2018-02-14
- Comitter:
- Nydrel
- Date:
- Wed Feb 14 09:39:54 2018 +0000
- Commit message:
- Changed the solenoid to pin21;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9a9311d745c3 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Feb 14 09:39:54 2018 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +//Solenoid Hello World +DigitalOut myled(LED1); +//Non blocking with auto off delay using timer interrupt setup by class +class Solenoid +{ +public: + Solenoid (PinName pin, float delay=0.5); + void write(bool state); + Solenoid& operator= (bool value); + +private: + void Solenoid_Off_Int(); + DigitalOut _pin; + Timeout tint; + float ontime; +}; +Solenoid::Solenoid(PinName pin, float delay) : _pin(pin), ontime(delay) +{ + _pin=0; +} +void Solenoid::Solenoid_Off_Int() +{ + _pin=0;//timer interrupt routine to auto turn off solenoid +} +void Solenoid::write(bool value) +{ + _pin = value; + if (value!=0) //do auto off with timer interrupt + tint.attach(this,&Solenoid::Solenoid_Off_Int,ontime);//setup a timer interrupt +} +Solenoid& Solenoid::operator= (bool value) +{ + write(value); + return *this; +} + +Solenoid mySolenoid(p21); +int main() +{ + while(1) { + mySolenoid = 1; //ON with timer auto off + myled = 1; + wait(.5); //just for LEDs - solenoid turns off with timer interrupt + myled = 0; + wait(2.0); + } +} \ No newline at end of file
diff -r 000000000000 -r 9a9311d745c3 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Feb 14 09:39:54 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e \ No newline at end of file