#define NUMPIXELS 94 // Popular NeoPixel ring size

int red;
int green;
int blue;
int i;

int peak;
int source=NUMPIXELS/2;

int field[NUMPIXELS];
int prev[NUMPIXELS];


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIN        15 // On Trinket or Gemma, suggest changing this to 1



Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 10 // Time (in milliseconds) to pause between pixels

void actualizare()
{
  for(int i=0; i<NUMPIXELS; i++)
  {
    // pixels.setPixelColor(i, pixels.Color(field[i]/3, field[i]/3, field[i]));
    pixels.setPixelColor(i, pixels.Color(max(40,(field[i])), max(40,(field[i])),max(100,(field[i]))));
    prev[i] = field[i];
  }
  pixels.show();
}



void calculate ()
{
  for (i = abs(source - 1); i>=0; i--)
  {
    // Serial.print(i);
    // Serial.print("  ");
    // Serial.println(source);
    if((field[i+1]>=prev[i+1]) && (prev[i+1] > field[i]) && (abs(i-source) <20))
    {
      field[i]++;
      // Serial.print("*** ");
      // Serial.print("  ");
      // Serial.print(prev[i]);
      // Serial.print("  ");
      // Serial.print(i);
      // Serial.print("  ");
      // Serial.println(source);
      // field[i] ++;

    }


    if((field[i+1]>prev[i+1]) && (prev[i+1] > field[i]) && (abs(i-source) >=20))
    {
      field[i]++;
    }
  
 

    // if((field[i+1]<=prev[i+1]) && (prev[i+1] < field[i]))
    if((field[i+1]<prev[i+1]) && (prev[i+1] < prev[i]))
    {
      
      field[i] --;
      // field[i] = prev[i+1];
    }
  }
  

  for (int i = source + 1; i<NUMPIXELS; i++)
  {
    if((field[i-1]>=prev[i-1]) && (prev[i-1] > field[i]) && (abs(i-source) <  20))
    {
      field[i]++;
     
    }

    if((field[i-1]>prev[i-1]) && (prev[i-1] > field[i]) && (abs(i-source) >=20))
    {
      field[i]++;
     
    }

    // if((field[i-1]<=prev[i-1]) && (prev[i-1] < field[i]))
    if((field[i-1]<prev[i-1]) && (prev[i-1] < prev[i]))
    {
      
      field[i] --;
      // field[i] = prev[i-1];
      
    }
  }
}


void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  // pixels.setBrightness(40);
  
  Serial.begin(115200);
  randomSeed(42);
}

void loop() 
{
  pixels.clear(); // Set all pixel colors to 'off'

  while (true)
  {
    // source += random(-3, 3);
    source = random (3, NUMPIXELS-3);
    // source = NUMPIXELS/2;
    if(source>=NUMPIXELS)
    {
      source = NUMPIXELS-1;
    }

    if(source <=0)
    {
      source = 1;
    }


    int temp = field[source];
    // for (peak = field[source]; peak < field [source] + random(abs(255-field[source])); peak ++)
    // for (peak = temp; peak < temp + random(100,abs(150-field[source])); peak ++)
    for (peak = 0; peak < 100; peak ++)
    {
      field[source] = peak;
      calculate();
      actualizare();
      delay(10);
    }

    // for (peak = field[source]; peak > field[source] - random(field[source]); peak -= random(25))
    for (peak = field[source]; peak > 0; peak --)
    {
      field[source] = peak;
      calculate();
      actualizare();
      delay(10);
    } 

    // for (int r=0; r<(max(source, NUMPIXELS-source)-33); r++)
    // {
    //   calculate();
    //   actualizare();
    //   delay(10);
    // }

  }
 
 
}