-2

I am trying to make a program to control matrix LED using fastLED library. I have 4 buttons where each button activate the corresponding animation. But I want the program to be able to start animating one of the animation functions I made once the program is starting before any button is pressed. I already wrote a working LED matrix animation program that works individually but when I put it in the setup it only runs the first frame and won't animate. I also tried putting the function inside while(1) it won't get interrupted and transition to another animation when the button is pressed. Anyway where I can animate the first initial animation before other buttons are pressed?

The code is as shown below and 'firstanimation()' is the function I want to animate on start up. I won't post the code for the animation as it is very long.

void setup()
{
  pinMode(tx, INPUT);
  pinMode(rx, OUTPUT);

  delay(50);
  Serial.begin(9600);
  BT.begin(9600);

  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
  pinMode(btn4, INPUT);
  pinMode(btn5, INPUT);

  FastLED.setDither(1);
  FastLED.addLeds<NEOPIXEL, DATAPIN>(leds, LEDNUM).setCorrection(TypicalPixelString); // tone down the blue. more warm
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 39500); // power management in v and mA

}

void loop()
{

  BT.listen();
  int c = BT.read();
  delay(30);

  if (digitalRead(btn1) == HIGH && is_fog != 1)
  {
    Serial.println("btn1 pressed");
    is_fog = 1;
    is_rain = 0;
    is_rlight = 0;
    is_sun = 0;
    is_wind = 0;

  }
  else if (digitalRead(btn2) == HIGH && is_rain != 1)
  {
    Serial.println("btn2 pressed");
    is_fog = 0;
    is_rain = 1;
    is_rlight = 0;
    is_sun = 0;
    is_wind = 0;

  }
  else if (digitalRead(btn3) == HIGH && is_rlight != 1)
  {
    Serial.println("btn3 pressed");
    is_fog = 0;
    is_rain = 0;
    is_rlight = 1;
    is_sun = 0;
    is_wind = 0;
  }
  else if (digitalRead(btn4) == HIGH && is_sun != 1)
  {
    Serial.println("btn4 pressed");
    is_fog = 0;
    is_rain = 0;
    is_rlight = 0;
    is_sun = 1;
    is_wind = 0;
  }
  else if (digitalRead(btn5) == HIGH && is_wind != 1)
  {
    Serial.println("btn5 pressed");
    is_fog = 0;
    is_rain = 0;
    is_rlight = 0;
    is_sun = 0;
    is_wind = 1;

  }


  if (is_fog == 1)
  {
    state = 0;
    animation1();
  }
  else if (is_rain == 1)
  {
    state = 3;
    animation2();
  }
  else if (is_rlight == 1)
  {
    state = 0;
    animation3();
  }
  else if (is_sun == 1)
  {
    state = 0;
    animation4();
  }
  else if (is_wind == 1)
  {
    state = 2;
    animation5();
  }

For your reference, the firstanimation function is as follow:

#define FASTER 1
#define TIMING 1


byte bump[LEDNUM];
void generatebumpmap()
{
  uint16_t t = millis() / 11; faster
  int index = 0;
  for (int j = 0; j < ROWS; j++)
  {
    for (int i = 0; i < COLS; i++)
    {
      byte col;
      uint16_t u, v;
      u = i * (36 + 4) + t / 5; 
      v = j * (23 + 6) - t / 3; 

      if (!FASTER)
      {
        col = inoise8(u, v, t);
        col = map(col, 0, 255, 0, 255);
      }
      else //FASTER==1
      {
        col = 76 + inoise8_raw(u, v, t); 
      }
      bump[index++] = col;
    }
  }
}

void Bumpmap(int8_t lightx, int8_t lighty)
{

  int yindex = COLS;
  int8_t vly = lighty;

  for (int y = 1; y < ROWS - 1; y++)
  {
    ++vly;
    int8_t vlx = lightx;
    for (int x = 1; x < COLS - 1; x++)
    {
      ++vlx;
      int8_t nx = bump[x + 1 + yindex] - bump[x - 1 + yindex];
      int8_t ny = bump[x + yindex + COLS] - bump[x + yindex - COLS];
      if (!FASTER)
      {
        byte difx = abs8(vlx - nx);
        byte dify = abs8(vly - ny);
        int col = 255 - sqrt16(difx * difx + dify * dify) * 3;
        if (col < 0)
          col = 0;
       leds[XY(x, y)] = CHSV(250, 255, col);
       
      }
      else
      {
        uint16_t sumsquare = (vlx - nx) * (vlx - nx) + (vly - ny) * (vly - ny);
        byte col = 0;
        if (sumsquare < 7225) // 7225 == (255 / 3)²
          col = 255 - sqrt16(sumsquare) * 3;
        leds[XY(x, y)] = chsv[col];
      }
    }
    yindex += COLS;
  }
}

void firstanimation() {
  /////COLORRR
  for (int i = 0; i < 256; i++) {
    chsv[i] = CHSV((i / 1.5 ) + 16 , 255, i); //+60
    //chsv[i] = CHSV((i / 1.5) + 16 , fogvar, i); 
  }
  int time = millis() / 8;
  int8_t lightX = 1 - (sin8(time / 3) - 128) / 3.2; // beatsin8(20,0,80); 
  int8_t lightY = 1 - (sin8(time / 2) - 128) / 3.1; // beatsin8(25,0,80); //

  unsigned long t1 = micros();
  generatebumpmap();
  unsigned long t2 = micros();
  Bumpmap(lightX, lightY);
  unsigned long t3 = micros();
  FastLED.show();

  if (TIMING) 
  {
    static unsigned long t2_sum, t3_sum;
    t2_sum += t2 - t1;
    t3_sum += t3 - t2;
    static byte frame;
    if (!(++frame % 64))
    {

      t2_sum = t3_sum = 0;
    }
  }
}

I tried putting multiple function in void loop and and void setup and none of it worked.

hcheung
  • 3,377
  • 3
  • 11
  • 23

1 Answers1

0

Why not just test your buttons in setup like that ?

 while (!(digitalRead(btn1) || digitalRead(btn2) || digitalRead(btn3) || digitalRead(btn4))) {
 generatebumpmap();
 }

Pressing anny button will exit setup and start loop.

tuyau2poil
  • 767
  • 1
  • 2
  • 7