   return seqNum;
 }
 bool rollSent = false;
-
+bool registered = false;
 // ************* IMU **************************************************
 int16_t vraw[10]; // raw readings from IMU and info for sending via BLE
                   // [0:5]=AxAyAzGxGyGz, [6]=movement, [7]=stillMs, [8]=diceId, [9]=seqNum
 int vprev[6];     // previous reading
 
+#define MOVE_THRESHOLD  32
+#define GOOD_ROLL       4
 int16_t movement=0;
+int16_t maxMove[6];
+int16_t moves=0;
 unsigned long stillBegin;
 unsigned long stillMillis=0;   // number of sequential zero movements
 
+
+void idleShow()
+{
+  static int imask = 0x1;
+  static int dir = 1;
+  static int prev = 0;
+  if (stillMillis < 5000) return;
+  int p = stillMillis/150;
+  if (p!=prev) {
+    prev = p;
+    if (imask==0x01) dir = 1;
+    if (imask==0x80) dir = 0;
+    if (dir) imask = imask << 1;
+    else imask = imask >> 1;
+    dispLed(0xff,0x000,0);
+    dispLed(imask,0xff0,0);
+  }
+}
 
 int16_t imuRead() {
   for (int i=0; i<6; i++) vprev[i] = vraw[i];
   CurieImu.getMotion6(&vraw[0], &vraw[1], &vraw[2], &vraw[3], &vraw[4], &vraw[5]);
   movement = 0;
+
   for (int i=0; i<6; i++) {
     int m = (abs(vprev[i]-vraw[i])) >> 8;
     if (m>movement) movement = m;
+    if (maxMove[i]==0 && m>MOVE_THRESHOLD) { maxMove[i]=m; moves++;}
   }
   // movement is 8 bits
   int mask = 0x0;

    else {
      stillMillis = millis() - stillBegin;
      if (stillMillis > 250) {
        int a = vraw[0]>>8;
        int b = vraw[1]>>8;
        int c = vraw[2]>>8;
        unsigned char roll=0;
        if (a>0x1d && a<0x3d && b>-10 && b<13 && c>0x12 && c<0x32)
          {roll=1; dispLed(0x1,0x0f0, 30);}
        else if (a>0x13 && a<0x37 && b>-13 && b<13 && c>(signed char)0xb3 && c<(signed char)0xd6)
          {roll=2; dispLed(0x3, 0x0f0, 30);}
        else if (a>(signed char)0xee && a<10 && b>(signed char)0xb1 && b<(signed char)0xd1 && c>(signed char)0xe6 && c<10)
          {roll=3; dispLed(0x7, 0x0f0, 30);}
        else if (a>(signed char)0xed && a<10 && b>0x30 && b<0x50 && c>(signed char)0xeb && c<10)
          {roll=4; dispLed(0x17, 0x0f0, 30);}
        else if (a>(signed char)0xc5 && a<(signed char)0xe0 && b>-10 && b<10 && c>0x1a && c<0x36)
          {roll=5; dispLed(0x37, 0x0f0, 30);}
        else if (a>(signed char)0xbb && a<(signed char)0xdb && b>-10 && b<10 && c>(signed char)0xbb && c<(signed char)0xdb)
          {roll=6; dispLed(0x77, 0x0f0, 30);}
        if (roll!=0 && !rollSent)
        {
          diceRollCharacteristic.setValue((diceId << 4) | roll);
          rollSent = true;
        }
      }
    }
  }
  else stillMillis=0;
  
  SERIAL_PRINT(movement);
  SERIAL_PRINT("\t");

  SERIAL_PRINT(CurieImu.getYGyroOffset()); 
  SERIAL_PRINT("\t"); // 0
  SERIAL_PRINT(CurieImu.getZGyroOffset());
  
  dispLed(0x02, 0x00f, 200);      // 1 - blue IMU - orient dice for calibration

  CurieImu.setGyroOffsetEnabled(true);
  CurieImu.setAccelOffsetEnabled(true);

  dispLed(0x02, 0x0f0, 0);      // 1 - green IMU setup done
  readDiceId();

  for (int i=0; i<6; i++) maxMove[i]=0;
  ledLapse = millis();
}



void atten(int16_t *a, int len, int shiftBy)
{
