How to display random Gifs from folder

I'm trying to pull up an Gif based on data from a bluetooth sensor.

What I would like for to happen is -Data is passed from sensor -Based on the range a number is generated -"number".gif is displayed - 5 sec delay - Repeat

(all the gifs are pre-loaded in a data folder and numbered 1.gif..and so on)

Any help is much appreciated

Thanks in advance!

import gifAnimation.*; 
import neurosky.*; 
import org.json.*;

Gif loopingGif; 
ThinkGearSocket neuroSocket; 
int attention=10; 
int meditation=10; 
int rand;

void setup() { 
size(600,600);
  ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
  try {
    neuroSocket.start();
  } 
  catch (Exception e) {
    //println("Is ThinkGear running??");
  }
  smooth();
  //noFill();
  font = createFont("Verdana",12);
  textFont(font);

if (attention<=30) {
   rand = int(random(1,5)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
   String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
   loopingGif = new Gif(this, fn);
   loopingGif.loop();
  }

  else {
    rand = int(random(5,9)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
   String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
   loopingGif = new Gif(this, fn);
   loopingGif.loop();
  }

}

void poorSignalEvent(int sig) { 
println("SignalEvent "+sig); 
}

public void attentionEvent(int attentionLevel) { 
println("Attention Level: " + attentionLevel); 
attention = attentionLevel; 
}

void meditationEvent(int meditationLevel) { 
println("Meditation Level: " + meditationLevel); 
meditation = meditationLevel; 
}

void draw() {
image(loopingGif, 0, 0); 
}

void stop() { 
neuroSocket.stop(); 
super.stop(); 
}

Answers

  • edited February 2016 Answer ✓

    gif is displayed - 5 sec delay - Repeat

    this means that this part belongs in draw(), not in setup() :

    if (attention<=30) {
       rand = int(random(1,5)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
       String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
      }
    
      else {
        rand = int(random(5,9)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
       String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
      }
    

    also you need a timer that tells you if the 5 seconds are over.

    the timer controls a var displayMyGif

    and when 5 secs are over, say displayMyGif = false;

    when displayMyGif == false do the if and else clause above

    when displayMyGif == true do image(loopingGif, 0, 0);

    all in draw()

    later you want to load an array of gif in setup() that you use in draw()

  • Thanks so much @chrisir!

    would that look something like this (apologies, I'm still very much a beginner)

    int interval = 5000; //timer's interval
    int lastRecordedTime = 0;
    int difference;
    
    void draw() {
    
      int difference= millis()-lastRecordedTime; //sketch time - last recorded time should be 5000 (ie) 5 secs
    
     if (difference > interval) {
         if (attention<=30) {
       rand = int(random(1,5)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
          String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
       lastRecordedTime= millis();
      }
      else {
        rand = int(random(5,9)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
           String fn= ((rand) + ".gif"); //CALL YOUR FUNCTION 'takerandomimage'
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
       lastRecordedTime= millis();
      }
    
      }
    
     else {
    image(loopingGif, 0, 0);
      }
    }
    
  • absolutely

    I miss setup() somehow

    make sure you display the image first

    maybe by placing line 7 at the very end or so

  • I think that's working better but now I get a NullPointerException for line 28?

  • edited February 2016

    that's right

    the if clause if (attention<=30) { must be run once.... otherwise loopingGif is empty. Bad.

    can't you say difference = 12000; in setup so that if (difference > interval) { is true initially?

  • You're totally right- I just tried that but it gives me a blank canvas, I can see values coming in from the sensor but no Gifs appear corresponding to them

    Much confusion ensues...

  • post entire code pls

  • edited February 2016

    @chrisir- I need you to know you're a life saver right now

    I made some changes to the timer and now I atleast see a Gif at first, but it doesn't change with incoming data/time- also it breaks if you increase the interval

       import gifAnimation.*;
        import neurosky.*;
    
    Gif loopingGif;
    ThinkGearSocket neuroSocket;
    int attention=10;
    int meditation=10;
    PFont font;
    int rand;
    int interval = 400; //timer's interval
    int lastRecordedTime = 0;
    //int difference=12000;
    
    
    void setup() {
      size(600,600);
      ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
      try {
        neuroSocket.start();
      } 
      catch (Exception e) {
        //println("Is ThinkGear running??");
      }
      smooth();
      //noFill();
      font = createFont("Verdana",12);
      textFont(font);
    }
    
    void poorSignalEvent(int sig) {
      println("SignalEvent "+sig);
    }
    
    public void attentionEvent(int attentionLevel) {
      println("Attention Level: " + attentionLevel);
      attention = attentionLevel;
    }
    
    
    void meditationEvent(int meditationLevel) {
      println("Meditation Level: " + meditationLevel);
      meditation = meditationLevel;
    }
    
    void blinkEvent(int blinkStrength) {
    
      println("blinkStrength: " + blinkStrength);
    }
    
    public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) {
      println("delta Level: " + delta);
      println("theta Level: " + theta);
      println("low_alpha Level: " + low_alpha);
      println("high_alpha Level: " + high_alpha);
      println("low_beta Level: " + low_beta);
      println("high_beta Level: " + high_beta);
      println("low_gamma Level: " + low_gamma);
      println("mid_gamma Level: " + mid_gamma);
    }
    
    void rawEvent(int[] raw) {
      //println("rawEvent Level: " + raw);
    }  
    
    void draw() {
    
    
     if(millis()-lastRecordedTime>interval) {
       if (attention<=30) {
       rand = int(random(1,5)); 
          String fn= ((rand) + ".gif"); 
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
       lastRecordedTime= millis();
      }
      else {
       rand = int(random(5,9)); 
       String fn= ((rand) + ".gif"); 
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
       lastRecordedTime= millis();
      }
      }
    
    else {
    image(loopingGif, 0, 0);
    lastRecordedTime= millis();
      }
    
    
    }
    
    void stop() {
      neuroSocket.stop();
      super.stop();
    }
    
  • delete line 87 maybe

    it resets all the time. Bad.

  • edited February 2016

    this timer works:

    (since I don't have gifs or the libs, it's just printing. But should do the job)

    ;-)

    PImage loopingGif=null;
    //ThinkGearSocket neuroSocket;
    int attention=10;
    int meditation=10;
    
    PFont font;
    int rand;
    int interval = 400; //timer's interval
    int lastRecordedTime = -12220;
    
    
    void setup() {
    }
    
    
    void draw() {
    
    
      if (millis()-lastRecordedTime>interval) {
        println("here");
        if (attention<=30) {
          rand = int(random(1, 5)); 
          String fn= ((rand) + ".gif"); 
          //loopingGif = new Gif(this, fn);
          //loopingGif.loop();
          lastRecordedTime= millis();
        } else {
          rand = int(random(5, 9)); 
          String fn= ((rand) + ".gif");
          println (fn); 
          //loopingGif = new Gif(this, fn);
          //loopingGif.loop();
          lastRecordedTime= millis();
        }
      } else {
        // image(loopingGif, 0, 0);
        println("show");
        // lastRecordedTime= millis();
      }
    }
    
  • YASSS!! Thank you so so much! ^:)^

  • I have eyes like an eagle.

    ;-)

  • Indeed you do B-)

  • Absolute last question- @chrisir

    If I wanted a Gif (the same Gif) to flash in between every new Gif that is loaded-

    Where would I put that? I'm loading it in my Setup() as follows-:

    void setup() {
      size(displayWidth, displayHeight);
    
    midGif= new Gif (this, "15.gif");
    midGif.loop();
    
    }
    

    And then trying variations in my actual code but I get nothing?

    void draw() {
    image(midGif, 0, 0, displayWidth, displayHeight);
    
     if(millis()-lastRecordedTime>interval) {
       rand = int(random(1,25)); 
       String fn= ((rand) + ".gif");
       loopingGif = new Gif(this, fn);
       loopingGif.loop();
       interval= int(random(2000,6000));
       lastRecordedTime= millis();
      }
    
     else {
    image(loopingGif, x, y,l, b);
      }
    
    }
    
  • Answer ✓
    PImage loopingGif=null;
    //ThinkGearSocket neuroSocket;
    int attention=10;
    int meditation=10;
    
    PFont font;
    int rand;
    int interval = 1200; //timer's interval
    int lastRecordedTime = -12220;
    
    
    int intervalFlash = 400; //timer's interval Flash
    int lastRecordedTimeFlash = 990;
    
    boolean isOn = true; 
    int isOnCounter1 = 0; 
    
    
    final int stateShow=0;
    final int stateLoad=1;
    final int stateFlash=2;
    int state = stateLoad; 
    
    
    void setup() {
      size(662, 222);
    }
    
    
    void draw() {
      switch (state) {
    
      case stateShow:
        // image(loopingGif, 0, 0);
        println("show");
        // lastRecordedTime= millis();
        if (millis()-lastRecordedTime>interval) {
          state=stateFlash;
          lastRecordedTimeFlash=millis();
        }
        break; 
    
      case stateLoad:
        println("load");
        if (attention<=30) {
          rand = int(random(1, 5)); 
          String fn= ((rand) + ".gif"); 
          //loopingGif = new Gif(this, fn);
          //loopingGif.loop();
          lastRecordedTime= millis();
        } 
        else {
          rand = int(random(5, 9)); 
          String fn= ((rand) + ".gif");
          println (fn); 
          //loopingGif = new Gif(this, fn);
          //loopingGif.loop();
          lastRecordedTime= millis();
        }
        state = stateShow; 
        break;
    
      case stateFlash:
        background(0); 
        if (isOn)
          text("Flash", 50, 50);
        if (millis()-lastRecordedTimeFlash>intervalFlash) {
          isOn=!isOn;
          isOnCounter1++;
          lastRecordedTimeFlash=millis();
        }
        if (isOnCounter1>4) { 
          state=stateLoad;
          background(0);
          isOnCounter1 = 0;
          isOn = true;
          lastRecordedTime=millis();
        }
        break; 
    
      default:
        println("exit on unknown state "+state);
        exit(); 
        break;
      } //switch
    }
    //
    
  • since we have 3 situations now we use states

Sign In or Register to comment.