Need to freeze or capture random index number

edited February 2016 in Questions about Code

I have tried to find an answer to this in the forum. Nothing seems to work. Working on a program to display scrolled patterns on addressable LEDs in a sculpture. Have figured out how to display the patterns in order. Have now decided to add a random display in the last part of the performance. First part works fine. Random part flashes split second images as the sketch keeps updating the index variable in draw(). Have tried to pin down the index variable for two minutes in a separate function using constrain(). When I hard-assign the index a number, works fine. But when I try and assign a variable to it nothing happens, just continues to flash split-second images. Anybody know what I'm doing wrong? Here's the code:

// Declaring array of img.
int imMax = 7; // Total # of img
int imDex = 0; // Initial image to be displayed is the first
PImage[] img = new PImage[imMax];


// global loop variables
final int interval = 120000; // 10000 ms = 10 seconds
int timePoint; // When the current image was first displayed
int y; // 'y' position of image to be scrolled

void setup() {
  size(512, 30);
  frameRate(30);

  // Load the array images into the array
  for (int i = 0; i < img.length; i ++ ) {
    img[i] = loadImage("image" + i + ".jpg" );
  }

  //set current point in time as a marker
  timePoint=millis();
}

void draw() {
  background (0);

  // scale the image so that it matches the width of the window
  int imgHeight = img[imDex].height * width / img[imDex].width;

  stasis();

  // display first image
  image(img[imDex], 0, -y);

  //increment y
  y++;

  if (y > imgHeight) { 
    y = y % imgHeight;
  }

  // display next image
  image(img[imDex], 0, imgHeight-y);

  println (imDex);
}


void stasis() {
  float min = minute();

  // if it's the first 30 minutes of the hour 
  // display the main image (#3)
  if (min >= 00.0 && min <= 30.0) {    
    imDex = 3;
  }

  // if it's the last thirty minutes of the hour 
  // display all the images in random order
  if (min > 30 && min <= 59) {

    int stasis = int(random(imMax));// Does not work
    //int stasis = 4; (WORKS)
    imDex = stasis;

    // freeze the index variable for two minutes
    if (millis() - timePoint < interval)   
      imDex = constrain(imDex, stasis, stasis);

    println (stasis);
  }
}
Tagged:

Answers

  • edited February 2016
    // forum.processing.org/two/discussion/14780/
    // need-to-freeze-or-capture-random-index-number
    
    // GoToLoop (2016-Feb-06)
    
    static final int MINUTE = 60 * 1000, DELAY = 2 * MINUTE;
    static final int IMAGES = 7, FPS = 30;
    
    final PImage[] imgs = new PImage[IMAGES];
    int idx, delayStop;
    
    void setup() {
      size(512, 30);
      smooth(4);
      noLoop();
      frameRate(FPS);
      thread("stasis");
    
      for (int i = 0; i < IMAGES; imgs[i]
        = loadImage("image" + i++ + ".jpg"));
    }
    
    void draw() {
      background((color) random(#000000));
      frame.setTitle("Current Index = " + idx
        + ", Minute = " + minute());
    }
    
    void stasis() {
      int millisStop = 0;
    
      for (;; delay(MINUTE>>2)) {
        final int minutes = minute(), ms = millis();
    
        if (minutes < 30)  idx = 3;
        else if (ms >= millisStop) {
          millisStop = ms + DELAY;
          int rnd;
          while ((rnd = (int) random(IMAGES)) == idx);
          idx = rnd;
        }
    
        redraw();
      }
    }
    
  • Thanks for the fast reply, GoToLoop! I'll check it out. Looks like it'll work. Smart coding. Way more elegant than what I had. Will get back to you if I have questions.

  • I still admire your coding, GoToLoop. But I guess that I was unclear about what I am trying to do. I need to display each jpeg image and scroll through it along the "y" axis. This creates the illusion of motion through a single 30-foot long, winding LED strip. That's why I was incrementing "y" each time draw was executed. Your code, while more intricate yet also more direct than mine, mostly by-passes draw (which looks like a great idea, actually, if I could get it to work). Still, what your sketch displays are random solid colors instead of random jpeg images contained in my data file. Any ideas? I'm sure I can somehow achieve a hybrid of the two codes.

  • edited February 2016 Answer ✓

    Oops! Since I didn't have your images, I did my example overlooking any need for "animation" during draw(); by just delivering static updates each 15 seconds (1/4 MINUTE)! X_X

    Nevertheless, you can merely comment out noLoop(), and if wish, redraw() too from it, in order to have an FPS "animation" according to frameRate(). #:-S

  • Thanks! I'll try that and get back to you. Your help is much appreciated.

  • Wow! It works great. You, sir, are brilliant. Thanks for the help.

Sign In or Register to comment.