#define NUMPIXELS 94 

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

int peak1, peak2;
int source=NUMPIXELS/2;
int s1, s2;                 // sursele pt cele doua valuri
int speed1, speed2;         // viteza de crestere pt cele doua valuri

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

int cresc1 = 2;
int cresc2 = 2;


#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


// generare pozitie random pt noul val
int ran()
{
  int s = random (3, NUMPIXELS-3);
  // source = NUMPIXELS/2;
  if(s>=NUMPIXELS)
  {
    s = NUMPIXELS-1;
  }

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

  return(s);
}


// afisare pt toata banda, copiere in prev[]
void actualizare()
{
  for(int i=0; i<NUMPIXELS; i++)
  {
    pixels.setPixelColor(i, pixels.Color(field[i], 255-field[i], 0));
    // pixels.setPixelColor(i, pixels.Color(max(10,(field[i])), max(10,(field[i])),max(25,(field[i]))));
    prev[i] = field[i];
  }
  pixels.show();
}


// calculare wave
void calculate (int source)
{
  for (i = abs(source - 1); i>=0; i--)
  {
    if(field[i+1]>=prev[i+1])
    {
      field[i] = prev[i+1];
    }
    else if(field[i+1]< prev[i+1])
    {
      field[i] = field[i+1];
    }
    else
    {
      // field[i] = field[i+1]-1;
    }
    // Serial.print(source);
    // Serial.print(",");
    // Serial.println(field[source]);
  }
  

  for (int i = source + 1; i<NUMPIXELS; i++)
  {
    if(field[i-1]>=prev[i-1])
    {
      field[i] = prev[i-1];
    }
    else if(field[i-1]< prev[i-1])
    {
      field[i] = field[i-1];
    }
    else
    {
      // field[i] = field[i-1]-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);
    // s1 = random (3, NUMPIXELS-3);
    // cresc = 1;
    // // source = NUMPIXELS/2;
    // if(s1>=NUMPIXELS)
    // {
    //   s1 = NUMPIXELS-1;
    // }

    // if(s1 <=0)
    // {
    //   s1 = 1;
    // }


    

   

    if(cresc2 == 2)
    {
      // s2 = ran();
      s2 = 70;
      cresc2 = 1;
      speed2 = random(10,20);
    }
  


    if ((cresc2 == 1) && (peak2 < 255))
    {
      field[s2] = peak2;
      calculate(s2);
      
      peak2 += speed2;
    }

    if ((cresc2 == 1) && (peak2 >= 255))
    {
      cresc2 = 0;
      peak2 -= speed2;
    }

    if ((cresc2 == 0) && (peak2 >0))
    {
      field[s2] = peak2;
      calculate(s2);
      
      peak2 -= speed2;
    }

    if ((cresc2 == 0) && (peak2 <= 0))
    {
      cresc2 = 2;
      peak2 += speed2;
    }



 if(cresc1 == 2)
    {
      // s1 = ran();
      s1 = 25;
      speed1 = random(15, 22);
      cresc1 = 1;
    }



    if ((cresc1 == 1) && (peak1 < 255))
    {
      field[s1] = peak1;
      calculate(s1);
      
      peak1 += speed1;
    }

    if ((cresc1 == 1) && (peak1 >= 255))
    {
      cresc1 = 0;
      peak1 -= speed1;
    }

    if ((cresc1 == 0) && (peak1 >0))
    {
      field[s1] = peak1;
      calculate(s1);
      
      peak1 -= speed1;
    }

    if ((cresc1 == 0) && (peak1 <= 0))
    {
      cresc1 = 2;
      peak1 += speed1;
    }


    
/*
    Serial.print(s1);
    Serial.print(",");
    Serial.print(s2);
    Serial.print(",");
    Serial.print(peak1);
    Serial.print(",");
    Serial.println(peak2);
    */


    actualizare();
    delay(50);

   
    
    

  }
 
 
}