  * with the idea that low movement means colder and less bright colours,
  * with more movement turning it into warmer and brighter colours.
  * 
- * TODO Integrate pluse meter
- * 
  * Credit to Ben Nolan for the heart beat LED logic.
  * And for lending me some Neopixels to play around with!
  */
  
 #define BAUD_RATE 57600
-#define NUM_LEDS 120
-#define DATA_PIN 12
+#define NUM_LEDS_CH1 120
+#define LED_PIN_CH1 12
+#define NUM_LEDS_CH2 29
+#define LED_PIN_CH2 7
 #define ACCELX_PIN 0
 #define ACCELY_PIN 2
 #define ACCELZ_PIN 4
 #define PULSE_PIN 6
+#define CTRL_LED_RED_PIN 9
+#define CTRL_LED_GREEN_PIN 4
+#define CTRL_LED_BLUE_PIN 3
+#define BUTTON_PIN 2
+
+// ui
+int mode = 1;
+Bounce modeButton;
 
 // config
-int staticBpm = 35; // From http://ecg.utah.edu/img/items/Normal%2012_Lead%20ECG.jpg
+int staticBpm = 70; // From http://ecg.utah.edu/img/items/Normal%2012_Lead%20ECG.jpg
 byte beat[]  = {3,2,2,2,2,3,4,3,2,1,0,15,2,2,3,4,6,8,5,3,3,3};
 byte beatLength = 22;
 int movesPerBeat = 8;
 int minMagnitude = 80;
 int maxMagnitude = 150;
-int minBrightness = 10; // from 0-255
-int maxBrightness = 100; // from 0-255
+int minBrightness = 180; // from 0-255
+int maxBrightness = 255; // from 0-255
 int minHue = 160; // hue value under low motion
 int maxHue = 255; // hue value under high motion
-int gainRatePerBeat = 200; // value change towards new target value (per beat)
-int decayRatePerBeat = 1; // percent change towards new target per beat
+int gainRatePerBeat = 200; // value change towards new target value (percent*100 per beat, e.g 200 = 20%)
+int decayRatePerBeat = 3  ; // percent change towards new target per beat
 int sampleSize = 8; // smooth out accelerometer readings
+int samplePerBeat = 2;
 
 // state
 int bpm = staticBpm;
-byte bufr[NUM_LEDS];
+byte bufr[NUM_LEDS_CH1];
 byte offset = 0;
 int targetMagnitude = minMagnitude;
 int adjustedMagnitude = minMagnitude;

#include "FastLED.h"

/*

volatile boolean pulseBeatFound = false; // becomes true when Arduoino finds a beat.
volatile boolean hasPulse = false; // 

CRGB leds1[NUM_LEDS_CH1];
CRGB leds2[NUM_LEDS_CH2];

void setup() { 
      FastLED.addLeds<NEOPIXEL, LED_PIN_CH1>(leds1, NUM_LEDS_CH1);
      FastLED.addLeds<NEOPIXEL, LED_PIN_CH2>(leds2, NUM_LEDS_CH2);
      Serial.begin(BAUD_RATE);
      interruptSetup();

      pinMode(CTRL_LED_RED_PIN, OUTPUT);
      pinMode(CTRL_LED_GREEN_PIN, OUTPUT);
      pinMode(CTRL_LED_BLUE_PIN, OUTPUT);
      pinMode(BUTTON_PIN, INPUT_PULLUP);
      
      modeButton.attach(BUTTON_PIN);
      modeButton.interval(5);
}

void moveBuffer() {
  int i;
  byte c = (int) beat[offset] * 255 / beatLength;
  int midPoint = NUM_LEDS_CH1/2;

  // Move current beat intensity from middle to beginning of strip (backward in buffer)
  bufr[midPoint - 1] = c;
  for (i=0;i<midPoint - 1;i++){
    bufr[i] = bufr[i+1];
  }

  // Move current beat intensity from middle to end of strip (forward in buffer)
  bufr[midPoint] = c;
  for (i=NUM_LEDS_CH1 - 1;i>midPoint;i--){
    bufr[i] = bufr[i-1];
  }
}

