LED animation - make object exit the frame on one side, and enter on the other

Status
Not open for further replies.

harald25

Well-known member
Hi!
I'm having a trouble figuring out how to make this happen, or even how to google it properly.
I have an animation that makes a "wave" move through an matrix of LEDs. I can adjust the angle of the wave so it moved straight up in the Y-direction, or at an angle towards the corner. In order to have a smooth animation I have a value delta. When I loop through my pixels I start lighting up a pixel when distance < delta.
My problem is when the wave reaches the end of the matrix and I want it to start entering the matrix on the opposite side. At the moment when the "current position" in the program reaches a defined "total steps" value it starts at 0 again. But the the current posision is suddenly 0, and LED number 1,2,3,4,5,6, etc (depending on my delta value) is also lit. I need LED number 0, 1,2,3,4,etc to start lighting up when the "current position" is nearing the end of the matrix.
At the moment I've "solved" the problem by doing two thing. I set a "total steps" value that is larger than the size of the matrix. So that the wave kind of moves outside of the matrix. That way the animation looks smooth in the end. Also, when I calculate the distance between the "current position" in the program and y value of my current pixel, I add delta to the distance:
Code:
int16_t dist = abs(y - (led_coordinates[x][1]+delta));
That way the animation looks smooth in the beginning, but it doesn't really solve my problem.
I hope this made sense to someone! Here is my code so that you can take a look yourself:

Code:
#include "program_cycle.h"

cycle_order order;
cycle_mode mode;

CRGBPalette16 * cycle_palette_array[] = { &heatcolorPalette};
CRGBPalette16 * active_cycle_palette;

void cycle(cycle_mode c_mode, cycle_order c_order)
{
  order = WAVE;
  mode = GRADIENT;
  saturation1 = 255;
  value1 = 255;
  active_program = CYCLE;
  update = true;
  program_index1 = 0;
  program_index2 = 0;
  delta = 20;
  total_steps1 = 350;
  total_steps2 = 255;
  increment_by1 = 1;
  interval = 35;
  active_cycle_palette = &heatcolorPalette;
  slope = -2.0;

}

void cycleUpdate()
{
  if (order == START_TO_END)
  {
    for (int x = 0; x < NUM_LEDS_PER_STRIP*NUM_STRIPS; x++)
    {
      if(mode == RAINBOW) {
        leds[led_array[x]] = CHSV(((x+program_index1)%255), saturation1, value1);
      }
      if(mode == GRADIENT) {
        leds[led_array[x]] = ColorFromPalette(*active_cycle_palette,((x+program_index1)%255));
      }

    }
  }

  if (order == WAVE)
  {
    // Note to self: don't confuse the x of the loop with the x-coordinate of the pixels. x in the loop represents a pixel in the LED strip
    // 'led_coordinates[x][0]' is the x-coordinate of the x'th pixel, 'led_coordinates[x][1]' is the y-coordinate of the x'th pixel
    for (int x = 0; x < NUM_LEDS_PER_STRIP*NUM_STRIPS; x++)
    {

      int16_t y = program_index1 + (slope*led_coordinates[x][0]);
      // Calculate the distance between the y coordinate of the current pixel the y to be lit, calculated above
      int16_t dist = abs(y - (led_coordinates[x][1]+delta));

      // If distance is less than delta, the pixel should be lit
      if (dist < delta) {
        // Calculate brightness of the current pixel based on distance
        uint16_t brightness = (delta-dist)*(value1/delta);

        if (mode == RAINBOW) {
          leds[led_array[x]] = CHSV(((led_coordinates[x][1]+program_index1+program_index2)%255), saturation1, brightness);
        }
        if(mode == GRADIENT) {
          leds[led_array[x]] = ColorFromPalette(*active_cycle_palette,((led_coordinates[x][1]+program_index1+program_index2)%255));
          leds[led_array[x]].nscale8_video(brightness);
        }
      }
      else {
        leds[led_array[x]] = CRGB::Black;
      }

    }
    incrementIndex(&program_index2, &total_steps2, &increment_by2);
  }

  incrementIndex(&program_index1, &total_steps1, &increment_by1);

  FastLED.show();
}


PS: This is not all the code needed to actually run the program, but there's a lot of code irrelevant to my question, so I've opted not to paste it here. I am going to upload it to Git soon, and I will link to my git repo when I've done that.

EDIT: Uploaded to GitHub now: https://github.com/harald25/KjellerTak
The code I've pasted above is from src/program_cycle.cpp
 
Last edited:
Status
Not open for further replies.
Back
Top