Sam Ewins / Mbed 2 deprecated Sound_Meter

Dependencies:   elec350 mbed

Files at this revision

API Documentation at this revision

Comitter:
sewins
Date:
Wed Nov 04 21:47:34 2015 +0000
Commit message:
Published beginnings of PWM expansion

Changed in this revision

elec350.lib Show annotated file Show diff for this revision Revisions of this file
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
soft_pwm.h Show annotated file Show diff for this revision Revisions of this file
soft_pwn.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/elec350.lib	Wed Nov 04 21:47:34 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/sewins/code/elec350/#cbbb5ce60368
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 04 21:47:34 2015 +0000
@@ -0,0 +1,50 @@
+#include "mbed.h"
+
+#include "microphone.h"
+#include "led.h"
+
+int main()
+{
+    Microphone microphone;
+    microphone.start();
+
+    Led greenLed("green");
+    Led orangeLed("orange");
+    Led redLed("red");
+    Led blueLed("blue");
+
+    while(1) {
+
+        int16_t maxReading = 0;
+        int16_t Reading = 0;
+
+        for (int i = 0; i < 128; i++) {
+            Reading = microphone.read();
+            if (Reading <0) {
+                (Reading = -Reading);
+            }
+
+            if (Reading > maxReading) {
+                (maxReading = Reading);
+            }
+            if (maxReading >1) {
+                redLed.On();
+            }
+            if (maxReading >5) {
+                blueLed.On();
+            }
+            if (maxReading >10) {
+                greenLed.On();
+            }
+            if (maxReading >15) {
+                orangeLed.On();
+            }
+        }
+        wait(0.003);
+        redLed.Off();
+        blueLed.Off();
+        greenLed.Off();
+        orangeLed.Off();
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Nov 04 21:47:34 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/soft_pwm.h	Wed Nov 04 21:47:34 2015 +0000
@@ -0,0 +1,25 @@
+#ifndef _SOFT_PWM_
+#define _SOFT_PWM_
+
+#include "mbed.h"
+
+class SoftPwm
+{
+private:
+    float period;
+    float dutyCycle;
+    Timer timer;
+    
+public:
+    SoftPwm(float initialPeriod, float initialDutycycle);
+
+    float getPeriod();
+    float getDutyCycle();
+    void setPeriod(float newPeriod);
+    void setDutyCycle(float newDutyCycle);
+
+    bool isOn();
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/soft_pwn.cpp	Wed Nov 04 21:47:34 2015 +0000
@@ -0,0 +1,40 @@
+#include "soft_pwm.h"
+
+SoftPwm::SoftPwm(float initialPeriod, float initialDutyCycle)
+{
+    this->period = initialPeriod;
+    this->dutyCycle = initialDutyCycle;
+
+    this->timer.start();
+}
+
+float SoftPwm::getPeriod()
+{
+    return this->period;
+}
+float SoftPwm::getDutyCycle()
+{
+    return this->dutyCycle;
+}
+void SoftPwm::setPeriod(float newPeriod)
+{
+    this->period = newPeriod;
+}
+void SoftPwm::setDutyCycle(float newDutyCycle)
+{
+    this->dutyCycle = newDutyCycle;
+}
+
+bool SoftPwm::isOn()
+{
+    float onPhaseDuration = this->dutyCycle * this->period;
+    float currentTime = this->timer.read();
+    if (currentTime > this->period) {
+        this->timer.reset();
+    }
+    if (currentTime<onPhaseDuration) {
+        return true;
+    } else {
+        return false;
+    }
+}
\ No newline at end of file