const int pinAccel = A0;
const int pinsLED[] = {10, 11, 12, 13};
const unsigned long timeLEDOn = 300;
const int threshold0 = 100;
const int threshold1 = 400;
const int threshold2 = 700;
const int threshold3 = 1000;
const unsigned long timeWait = 150;
enum {START, WAIT, STOP} state;
int previousThreshold = -1;
unsigned long timerStart;

void setup()
{
  Serial.begin(9600);
  for (byte i = 0; i < 3; i++)
  {
    pinMode(pinsLED[i], OUTPUT);
  }
}

void loop()
{
  unsigned long timeNow = millis();
  int mAV_EMA = analogRead(pinAccel);
  int threshold = getThreshold(mAV_EMA);
  switch (state)
  {
    case START:
      if (threshold >= 0)
      {
        previousThreshold = threshold;
        timerStart = timeNow;
        state = WAIT;
      }
      break;
    case WAIT:
      if (threshold > previousThreshold)
      {
        previousThreshold = threshold;
        timerStart = timeNow;
      }
      if (timeNow - timerStart >= timeWait)
      {
        digitalWrite(pinsLED[previousThreshold], HIGH);
        timerStart = timeNow;
        state = STOP;
      }
      break;
    case STOP:
      if (timeNow - timerStart >= timeLEDOn)
      {
        digitalWrite(pinsLED[previousThreshold], LOW);
        state = START;
      }
      break;
  }
}

int getThreshold(float mAVEMA)
{
  if (mAVEMA >= threshold3) return 3;
  if (mAVEMA >= threshold2) return 2;
  if (mAVEMA >= threshold1) return 1;
  if (mAVEMA >= threshold0) return 0;
  return -1;
}