Amplitude Modulation Code

Dependencies:   HIDScope mbed

Fork of SymbitronValves by First Last

main.cpp

Committer:
vsluiter
Date:
2015-06-26
Revision:
0:80993ec26dfd
Child:
1:926025d703f4

File content as of revision 0:80993ec26dfd:

#include "mbed.h"
#include "FastPWM.h"


#define V1_OPEN  D2
#define V1_CLOSE D3
#define V2_OPEN  D4
#define V2_CLOSE D5
#define V3_OPEN  D6
#define V3_CLOSE D7
#define V4_OPEN  D8
#define V4_CLOSE D9

#define PRESSURE A0

class Valve
{
    public:
    Valve(PinName open, PinName close);
    void write(float pwm);
    private:
    float _pwm;
    float _period;
    FastPWM _open;
    FastPWM _close;
};

Valve::Valve(PinName open, PinName close): _open(open), _close(close)
{
    _period = 0.05;
    _open.period(_period);
    _close.period(_period);
    _open.pulsewidth(0);
    _close.pulsewidth(0);   
}

void Valve::write(float pwm)
{
    if(pwm > 1)
        pwm = 1;
    if(pwm < -1)
        pwm = -1;
    if(pwm > 0)
    {
        _close.pulsewidth(0);
        _open.pulsewidth(_period*pwm);
    }
    else
    {
        _open.pulsewidth(0);
        _close.pulsewidth(_period*pwm*-1);
    }
}

int main() {
    Valve v1(V1_OPEN, V1_CLOSE);
    Valve v2(V2_OPEN, V2_CLOSE);
    Valve v3(V3_OPEN, V3_CLOSE);
    Valve v4(V4_OPEN, V4_CLOSE);
    //PwmOut v1(V1_OPEN);
    //v1.period(0.2);
    while(1) {
        static float time = 0;
        wait(0.02);
        time +=0.05;
        v1.write(sin(time));
        v2.write(sin(time));
        v3.write(sin(time));
        v4.write(sin(time));
    }
}