Code for Task #1 - Random Random Roger Roger!

Nice, @JustPete! I found it perfectly comprehensible.

Here’s a tip for anyone who would like to post code formatted as monospace font. You can use a formatting language called Markdown. In markdown we use backticks (`) to surround `inline code snippets`, and three of them on their own line to make a monospaced code block:

``` 
// monospaced code here
```

To get code highlighting, you can add a language name right after the opening backticks:

```javascript
// Highlighted
function { var variable = 1 }
```

Will render as:

// Highlighted
function f { var variable = 1 }

Limited HTML is supported too. This is an <h3>.

2 Likes

To @jeff’s comment, I’ll just add that it is super handy to know at least a few of the more common markdown commands. You’ll find that they work on the vast majority of online forums these days.

Here’s the cheat sheet I used to learn it:

@Jeff @Scruffynerf this is great. Combines with @Scruffynerf’s curly brackets background info really well.

I found that to corral the beginner issues I had to well-organize and present the code. otherwise its too much of a random junk heap of disconnected references. reveals the structure, I think.

Question - Does markdown interface with Notepad ++ ? Or stand alone?

Comment - I just LOVE cheat sheets! Had one for DOS back in the day… two pages, front to back, mostly replaced 600 page tomes of way too much information.

IMHO most things can be accomplished/built (at least started/prototyped) with a reduced set of commands/language/information… the special cases and fine tuning can be learned/accomplished/tested later.

Oh yeah - Here’s my attempt at corralling pertinent JS/Pixelblaze code: There are definitely some holes left… Some things that seem more useful or potentially useful are highlighted -

PixelBlaze JAVASCRIPT Expressions.pdf (130.6 KB)

1 Like

Nice cheat sheet start…

Google “notepad++ markdown” for a few add-ons that support markdown (I don’t use notepad++ or Windows so can’t point to the best one there)

You can install for Notepad++ plugins from its “Plugins/Plugin Admin…” menu.

I’ve used two – Markdown++ and the NPP Markdown Panel. (I’ve been using them for a while. At this stage, both are good and either will do the job. I actually can’t remember why I felt I needed two.)

I used a few hacks I’ve learned over the years. While waiting to twinkle, brightness is negative, and an odd/even brightness scheme to store whether the pixel is increasing or decreasing in brightness

// A bunch of settings that can be customized.
var numColors = 12 // Number of colors to pick from.
// The minimum and maximum speed a single pixel will twinkle.
var minTwinkleDuration = 100
var maxTwinkleDuration = 500
// The minimum and maximum time to wait unti the next twinkle.
var minWait = 10
var maxWait = 500
// Color for the ambient (off) pixels.
var ambientHue = .5
var ambientValue = .01

// Pattern state
var pixel = 0 // Which pixel is twinkling.
var brightness = 0 // The current brightness + twinkling state (see beforeRender() below).
var hue = 0 // The hue of the twinkling pixel.
var twinkleDuration = 400 // The duration of the currently twinkling pixel.

export function beforeRender(delta) {
  // Modify the pattern state as needed.
  
  // If brightness is zero, it's time to pick another random pixel to twinkle.
  if (brightness == 0) {
    // Pick another random pixel to twinkle.
    pixel = floor(random(pixelCount))
    // Select a random hue from the palette.
    hue = random(numColors) / numColors
    // Pick a random duration to twinkle between the min/max time.
    twinkleDuration = minTwinkleDuration + random(maxTwinkleDuration - minTwinkleDuration)
    // Set the brightness to a random negative time between the min/max wait time.
    brightness = -(minWait + random(maxWait - minWait))
    
  // If brightness is negative, we're still waiting for it to light up.
  } else if (brightness < 0) {
    // Remove the wait time that has elapsed since the last loop.
    brightness += delta;
    // If we've reached zero, begin the twinkling.
    if (brightness >= 0) {
      brightness = 1;
    }
    
  // If the brightness is a positive number, increase or decrease the brightness depending on whether it is odd or even.
  } else {
    // If the brightness is odd it is increasing.
    if (brightness % 2) {
      brightness += 2
      // Check if we reached the maximum brightness and set to an even value to begin dimming.
      if (brightness > twinkleDuration) {
        brightness = floor(twinkleDuration/2)*2
      }
      
    // If the brightness is even it is dimming.
    } else {
      brightness -= 2
      // Check if we reached the end of dimming and set to zero to trigger a new random twinkle.
      if (brightness < 1) {
        brightness = 0
      }
    }
  }
}

export function render(index) {
  // If this is the twinkling pixel, light it up
  if (index == pixel) {
    // Clamp the brightness to zero. Below zero we are waiting for the next pixel to start twinkling, so it is dark.
    var value = clamp(brightness ,0, twinkleDuration)
    // Calculate the relative brightness from 0 to 1.
    value /= twinkleDuration
    // Show the chosen pixel at the selected hue and value.
    hsv(hue, 1, value)
    
  // All other pixels are black.
  } else {
    hsv(ambientHue, 1, ambientValue)
  }
  
}
4 Likes

@aaroneous, I’m glad you’re giving these things a shot. I found it a lot of fun, and useful because it gave me an excuse to experiment in directions I never would have while writing “production” code. Hope you enjoy it too!