Change of code in Solenoid taken from Jim Hamblen's Notebook page on solenoids. https://developer.mbed.org/users/4180_1/notebook/an-introduction-to-solenoids/ Changed action of Solenoid class. Removed off delay functionality. Added on delay functionality and renamed for clarity between on delay and on time.
Fork of Solenoid by
Diff: Solenoid.h
- Revision:
- 0:bd163b4ce456
- Child:
- 1:b87c9e1546c5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Solenoid.h Tue Nov 24 09:36:27 2015 +0000 @@ -0,0 +1,51 @@ +//Found on Jim Hamblen's Solenoid Notebook page at https://developer.mbed.org/users/4180_1/notebook/an-introduction-to-solenoids/ + +#ifndef __SOLENOID_H__ +#define __SOLENOID_H__ + +#include "mbed.h" + +class Solenoid +{ +public: + Solenoid (PinName pin, float ondelay=0.5, float offdelay=2.0); + void write(bool state); + Solenoid& operator= (bool value); + +private: + void Solenoid_Off_Int(); + DigitalOut _pin; + Timeout tint; + Timer offtimer; + float ontime; + float offtime; +}; +Solenoid::Solenoid(PinName pin, float ondelay, float offdelay) : _pin(pin), ontime(ondelay), offtime(offdelay) +{ + _pin=0; + offtimer.start(); + +} +void Solenoid::Solenoid_Off_Int() +{ + _pin=0;//OFF timer interrupt routine to auto turn off solenoid + offtimer.start(); //start off-time delay count +} +void Solenoid::write(bool value) +{ + if (value!=0) {//ON so do auto off with timer interrupt + while(offtimer.read() < offtime); //wait for min OFF time before next ON allowed + offtimer.stop(); + offtimer.reset(); //reset off timer delay count + tint.attach(this,&Solenoid::Solenoid_Off_Int,ontime);//setup a timer interrupt for on time + } else + offtimer.start(); //solenoid turned off with a write call (not timers) so start off count + _pin = value; +} +Solenoid& Solenoid::operator= (bool value) +{ + write(value); + return *this; +} + +#endif /* __SOLENOID_H__ */ \ No newline at end of file