Amplitude Modulation Code

Dependencies:   HIDScope mbed

Fork of SymbitronValves by First Last

Revision:
0:80993ec26dfd
Child:
1:926025d703f4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 26 15:12:05 2015 +0000
@@ -0,0 +1,71 @@
+#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));
+    }
+}