MCP3008 Version

Dependencies:   mbed-rtos mbed mcp3008

Fork of KIK01_Proto01 by Ryo Od

Revision:
3:f89b400cfe57
Parent:
2:8dff77a1ee4d
Child:
4:9f53a82fc1b6
--- a/main.cpp	Sun Jun 04 05:53:01 2017 +0000
+++ b/main.cpp	Sun Jun 04 06:25:48 2017 +0000
@@ -1,3 +1,11 @@
+/*
+ * KIK01
+ * Kick Machine
+ *
+ * 2017.06.04 created.
+ *
+ */
+
 #include "mbed.h"
 #include "rtos.h"
 
@@ -8,13 +16,14 @@
 #define SAMPLING_RATE   (96000)
 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
 
-#define ENVELOPE_PERIOD     (500)
 #define FREQUENCY_ATTACK    (5)
 #define FREQUENCY_RELEASE   (300)
 #define AMPLITUDE_ATTACK    (50)
 #define AMPLITUDE_RELEASE   (200)
 
-AnalogOut Dac1(PA_4);
+AnalogOut Dac1(PA_5);
+
+AnalogIn AinBpm(PA_0);
 
 class EnvelopeAR {
 public:
@@ -90,12 +99,15 @@
     FREQUENCY_ATTACK, FREQUENCY_RELEASE, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
 EnvelopeAR envelopeAmplitude(AMPLITUDE_ATTACK, AMPLITUDE_RELEASE, 0.9f, 1.0f, 0.0f);
 
-int ticks;
-int envelopeTicks;
-float frequency;
-float phi;
-float phiDelta;
-float amplitude;
+volatile int ticks;
+volatile int envelopeTicks;
+volatile float frequency;
+volatile float phi;
+volatile float phiDelta;
+volatile float amplitude;
+
+float bpm;
+int envelopeLength;
 
 void generateWave()
 {
@@ -105,7 +117,7 @@
     }
     float level = sinf(PI_F * phi) * amplitude;
 
-    Dac1.write((level + 1.0f) / 2.0f);
+    Dac1.write((level * 0.7f + 1.0f) / 2.0f);
 }
 
 void generateEnvelope()
@@ -118,16 +130,16 @@
     amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
 
     envelopeTicks++;
-    if (envelopeTicks == ENVELOPE_PERIOD) {
+    if (envelopeTicks == envelopeLength) {
         envelopeTicks = 0;
     }
 }
 
 void update()
 {
-    ticks--;
-    if (ticks == 0) {
-        ticks = SAMPLING_RATE / 1000;
+    ticks++;
+    if (ticks == SAMPLING_RATE / 1000) {
+        ticks = 0;
         generateEnvelope();
     }
     generateWave();
@@ -141,11 +153,17 @@
     phiDelta = 2.0f * frequency / SAMPLING_RATE;
     amplitude = 1.0f;
     
-    ticks = SAMPLING_RATE / 1000;
+    ticks = 0;
     envelopeTicks = 0;
     
     Ticker samplingTicker;
     samplingTicker.attach(&update, SAMPLING_PERIOD);
         
-    for (;;) {}
+    bpm = 120.0f;
+    for (;;) {
+        bpm = AinBpm.read() * 120.0f + 60.0f;
+        envelopeLength = 60 * 1000 / bpm;
+        printf("%f\t%d\r\n", bpm, envelopeLength);
+        Thread::wait(100);    
+    }
 }