LED umbrella code help

I’ve built an umbrella with LEDs based on this:

I’m now trying to understand the code behind it and add some additional effects.
The original sketch is BeaUmbrella.zip (2.3 KB)
And I have some additional effects in this one to add to it BoostCodeFor17LEDs.zip (2.2 KB)

I’m trying to understand how the option for the button pressing to switch from one effect to the other works and also how to perhaps improve it or ignore it so it simply cycles.

If anyone can help I can meet you at the space on Thursday night if you’re going to be there.

Unfortunately we’re heading off to Burning Man on Saturday so not much time left to try and puzzle this out on my own.

Thanks
David

I plan to be there on Thursday evening, and like some others round here that’s something I should be able to help with. It’s µMeet night then, so you should be in luck regardless!

I agree, I can help you but need to sit with you to test code and stuff…

Thanks guys.I’ll bring it in tonight.

David - If you copy and paste the code we wrote last night but was left hanging when the last train struck, I’ll take a look and we can salvo back and forth.

(last train struck… that could be interpreted differently, eh!)

I think it’s the one attached. Although I suspect it wasn’t saved properly
after I fixed the typos but too jetlagged at the moment to be sure.
Going to try and work on this today but finding a gap now I’m at my sisters
is trickier than I thought.
Thanks
Umbrella_Toby.zip (2.65 KB)

Ah. Yes, that had a silly mistake in it.

After lighting the current position of the drop, we were moving the drop on to the next position. Except that we were moving on our working copy of the drop that only exists within the loop, so that moving on was being lost.
This

pixelOn++;

should be:

dropIndexes[i]++;

And now, because we’re advancing the drop in it’s spoke, not the whole strip, the check to see whether it’s fallen off is a lot easier too:

if (pixelOn == dropStrips[i]*kPixelsInSpoke + kPixelsInSpoke)

becomes

if (dropIndexes[i] >= kPixelsInSpoke)

Here’s the whole thing.

// loop through every pixel in the strip to set what's displayed per frame
for (int pixelIndex=0; pixelIndex<kPixelsInSpoke*kSpokesInStrip; pixelIndex++){
  
  uint32_t color = strip.Color(0, 0, 0);
  for (int i=0; i<kMaxDrops; i++)
  {
    int pixelOn = dropStrips[i]*kPixelsInSpoke + dropIndexes[i];
    if (pixelOn == pixelIndex)
    {
      color = strip.Color(0,0,255);
    }
    // advance animation for next time: move the drop to the next position in the strip
    dropIndexes[i]++;
    // have we dropped off the end? if so, form a new drop somewhere else
    if (dropIndexes[i] >= kPixelsInSpoke)
    {
      dropStrips[i] = random(0,kSpokesInStrip);
      dropIndexes[i] = random(0,kPixelsInSpoke);
    }
  }
  strip.setPixelColor (pixelIndex, color);
}
strip.show();

Changing the code up from a single drop to multiple drops has made it a fair bit more confusing. There’s a good way to handle this though, and that’s to model what a drop is, and have lots of instances of that model. So if you want to keep on with this, it’s a good introduction to more useful programming things: object-orientated programming, classes.

Thanks.
That’s working now but what happens is it lights up a portion of a spoke
starting at it’s random start pixel to the end of the spoke. There’s no
progression of the drop from the start pixel just a length of spoke lit up
and then it moves on to another spoke.
I also had to put a delay in at the end otherwise it progresses to the next
spoke as fast as it can.

Someone needs to come up with a simulator so you can see how this looks
without needing all the hardware :slight_smile:

Want a simulator? Now you’re thinking like a developer!

Oh! Look: we’re advancing our drop on every time it checks to see whether the current pixel is the drop pixel. Bear with me…

// calculate which pixels in the overall strip are to be lit    
int onStripIndexes[4];
for (int i=0; i<kMaxDrops; i++)
{
  onStripIndexes[i] = dropStripIndexes[i]*kPixelsInSpoke + dropPixelIndexes[i];
}

// loop through every pixel in the strip to set lit or not
for (int pixelIndex=0; pixelIndex<kPixelsInSpoke*kSpokesInStrip; pixelIndex++)
{
  // default to off
  uint32_t color = strip.Color(0, 0, 0);
  // test if should be on and update colour if so
  for (int i=0; i<kMaxDrops; i++)
  {
    if (pixelOn == onStripIndexes[i])
    {
      color = strip.Color(0,0,255);
    }
  }
  // set the colour in the strip
  strip.setPixelColor (pixelIndex, color);
}

// loop through each drop to advance on it's fall to the ground
for (int i=0; i<kMaxDrops; i++)
{
  dropPixelIndexes[i]++;
  // have we dropped off the end? if so, form a new drop somewhere else
  if (dropPixelIndexes[i] >= kPixelsInSpoke)
  {
    dropStripIndexes[i] = random(0,kSpokesInStrip);
    dropPixelIndexes[i] = random(0,kPixelsInSpoke);
  }
}
strip.show();

As part of that I renamed dropStrips and dropIndexes[i] to be more consistent and explanatory, so here’s the full thing with the definitions etc. elsewhere renamed.

Umbrella_Toby_v2.zip (3.0 KB)

I’ll check this out when I get a chance later tonight or tomorrow
hopefully. As it is I managed to alter the code that I had before that was
running the wrong way and now have something that at least runs a single
drop at a time down a spine starting at a random spot and with a small
acceleration in it as it goes. This is what I ended up with below:
Thanks

//My version of Rain Drops. It's not bad. I played with it a bit. Changing
the acceleration does funny things though.
void trickle(uint16_t number) {
  int spine, start, count, accel, realdrop;
  int droptime = 30; //30
  int wait = 50; //50
  uint16_t i;
  // wipe strip before starting to clear whatever the preceding animation
left behind -DR
  for (i = 0; i < TPIXEL; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
  }  strip.show();
  for (uint16_t i = 0; i < number; i++) {
    spine = random(8);
    start = random(16);
    droptime = random(100);
    wait = random(100);
    //didnt use the random accel value as couldn't get it to work properly
- set static value instead -DR
    //  accel = ((80 + random(20)) / 100);
    accel = .9;
    count = start;
    // removed the realdrop = value + droptime from next line this as it
made no sense to me so set it to static figure -DR
    realdrop = 250;
    // next line removed and replaced as it ran the raindrop up not down -
DR
    //for (count; count >= 0; count--) {
    for (count; count <= 16; count++) {
      strip.setPixelColor(spine * 17 + count, strip.Color(30, 10, 70));
      strip.show();
      // the next line accelerates the drop by reducing the wait between
moving the lit pixel down the spine -DR
      realdrop = realdrop * accel;
      delay(realdrop);
      //delay(100);
      strip.setPixelColor(spine * 17 + count, strip.Color(0, 0, 0));
      strip.show();
    }
    delay (50);
  }
}

Accelleration is an interesting problem to solve. Each drop should be accelerating individually, as they fall at different times and land at different positions (where further out is angled more and they should be faster). So you’d want individual delays… but you can’t use delays as your drops are falling in parallel and one would block all.

For what it’s worth, the code above should give a constant speed, as you give your 0.9 value to an integer type, so it gets rounded off. Actually, it gets truncated, so your delay(realdrop) is actually delay(0), ie nothing.

What the code was going for, I think, was to shorten the delay as the drop travelled down the spoke.

1 Like

Prompted by –

How did it go, got any photos?

1 Like

Unfortunately not. I’m rubbish at remembering to take photos.
I need to work on the code you started for me. I managed to get the rest of it cleaned up and working thanks to your help (the original sketch was for an umbrella wired the other way around and also had some errors). I would never have been able to get that far without teh lesson in basics you gave me along the way.
I’ll probably bring it to the xmas fair next month since it still functions as an umbrella.

Should probably get a video of it and post it up.

Ah, shame. Well, we can certainly help with a photo or video around the space. Christmas fair stall would be cool too (come rain or shine!?). And ping me if you get stuck with the code.