Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

  • java.lang.Out OfMemoryError. Failed to allocate a 8294412 byte allocation with 7320368 free bytes

    I'm using P3D, but it doesn't seem to make a difference which renderer I use (when it came to this test (I tried P2D and P3D). I tried running similar simple code to the TV noise script I posted earlier as a test and also ran into memory errors using only 2 Pimages.

    PImage noise;
    PImage testImages;
    
    
    PGraphics canvas;
    PGraphics canvas2;
    void setup()
    {
      size(displayWidth, displayHeight, P3D);
      frameRate(300);
      smooth(8);
      noise = createImage(100, 100, RGB);
      testImages = createImage(100, 100, RGB);
    
      noise.loadPixels();
    
      for (int y = 0; y < noise.pixels.length; )
        noise.pixels[y++] = int(random(-32, 32));
    
      noise.updatePixels();
      canvas = createGraphics(100, 100);
      canvas2 = createGraphics(100, 100);
    }
    void draw() {
      background(50);
      canvas2.beginDraw();
      image(noise, 0, 0, 100, 100);
      canvas2.endDraw();
      image(canvas2, 0, 0);
    
      fadeGraphics(canvas, 16);
      canvas.beginDraw();
    
      testImages = get(0, 0, 100, 100);
      scramble(testImages);
      canvas.image(testImages, 0, 0);
      canvas.endDraw();
      image(canvas, 0, 0, width, height);
    }
    
    void scramble(PImage pup) {
    
    
      for (int y = 0; y < testImages.pixels.length-2; ) {
        int R = pup.pixels[y+int(random(0, 2))] >> 16 & 0xFF ;
        int G = pup.pixels[y+int(random(0, 2))] >> 8 & 0xFF ;
        int B = pup.pixels[y+int(random(0, 2))] & 0xFF ; 
        pup.pixels[y++] = color(R, G, B, 40);
      }
      pup.updatePixels();
      // canvas.image(testImages, 0, 0, width, height);
    }
    
    
    void fadeGraphics(PGraphics c, int fadeAmount) {
    
      c.beginDraw();
      c.loadPixels();
    
      // iterate over pixels
      for (int i =0; i<c.pixels.length; i++) {
    
        // get alpha value
        int alpha = (c.pixels[i] >> 24) & 0xFF ;
    
        // reduce alpha value
        alpha = max(0, alpha-fadeAmount);
    
        // assign color with new alpha-value
        c.pixels[i] = alpha<<24 | (c.pixels[i]) & 0xFFFFFF ;
      }
    
      c.updatePixels();
    
    }
    

    Even this working with 100x100 pixel resolution stretched to the screen runs at low frame rates. I tested my app at 300x170px and it still runs at 1fps... Must be doing something wrong.

  • Fade to black at irregular frequence, how to loop on the last fade ?!

    This is my attempt working with transition/fading between two selected colors.

    Kf

    //===========================================================================
    // GLOBAL VARIABLES:
    
    StackActionExecutor ex;
    
    color c1;
    color c2;
    float ratio;
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void settings() {
      size(400, 600);
    }
    
    void setup() {
    
      textAlign(CENTER, CENTER);
      rectMode(CENTER);
    
      fill(255);
      strokeWeight(2);
    
      ex = new StackActionExecutor(this);
      ex.add(2000, "blackToRed");
      ex.add(3000, "fade_RedToBlue");
      ex.add(2000, "paintBlue");
      ex.add(3000, "fade_BlueToYelow");
      ex.add(1000, "yellowToBlack");
    
      ex.add(1500, "blackToRed");
      ex.add(1500, "fade_RedToBlue");
      ex.add(3000, "paintBlue");
      ex.add(30, "run4");
    }
    
    
    void draw() {
      background(0);
    
      fill(lerpColor(c1, c2, constrain(ratio, 0, 1)));
      ellipse(width/2, height/2, 50, 50);
    }
    
    void keyReleased() {
      redraw();
    }
    
    void mouseReleased() {
      looping=!looping;
      //ex.pauseFlag=!ex.pauseFlag;  //Call this line if sync_to_looping is false and you want the ex to mimic draw's looping state
    }
    
    
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    
    
    void fade_RedToBlue() {
      c1=color(255, 0, 0);
      c2=color(0, 0, 255);
    
      runThis("RedToBlue");
    }
    
    void paintBlue() {
      c1=color(0, 0, 255);
      c2=color(0, 0, 255);
    
      runThis("Just blue");
    }
    
    void fade_BlueToYelow() {
      c1=color(0, 0, 255);
      c2=color(255, 255, 0);
    
      runThis("BlueToYelow");
    }
    
    void blackToRed() {
      c1=color(0);
      c2=color(255, 0, 0);
    
      runThis("blackToRed");
    }
    
    void yellowToBlack() {
      c1=color(255, 255, 0);
      c2=color(0, 0, 0);
    
      runThis("yellowToBlack");
    }
    
    void run4() {
      runThis("RuN4");
      exit();
      return;
    }
    
    void runThis(String label) {
      ratio=ratio=ex.progress;
    
      if (frameCount%60==0) println(label + " running at "+millis()+" msecs.");
    }
    
    
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    
    public class StackActionExecutor { 
    
    
      ArrayList<StackActionObject> stack;
      final PApplet p;
      protected boolean pauseFlag;
    
      protected boolean sync_to_looping;
      float progress;
    
      StackActionExecutor(PApplet pa) {
        p=pa;
        p.registerMethod("draw", this);
        stack = new ArrayList<StackActionObject>();
        pauseFlag=false;
        sync_to_looping=false;  //Pauses based on main sketch state: isLooping()
        progress=0;
      }
    
      //If sync_to_looping then one can set the pause flag for this class to either true or false
      //Otherwise, the pauseFlag is handled by internal draw and pause should not be generated
      //by user. 
      //TO DO: pauseFlag should be protected and use pause/resume to enable flag. Pause allowed
      //       only when sync_to_looping is false. Conversely, sync_to_looping can be set only 
      //       when the pauseFlag is false  
    
      void add(int stime, String methodCall) {
        StackActionObject obj=new StackActionObject(stime, methodCall);
        stack.add(obj);
      }
    
      void runStack() {
    
        if (!isDone()) {
          StackActionObject obj=stack.get(0);
    
          if (obj.activated==false) {
            obj.init();
          }
    
          obj.run(pauseFlag);    
          progress=obj.lapsedTime*1.0/obj.duration;
    
          if (obj.isDone()) {
            progress=0;
            stack.remove(obj);
          }
        }
      }
    
      public void draw() {
    
        if (sync_to_looping) 
          pauseFlag=!p.isLooping();
    
        runStack();
      }
    
      boolean isDone() {
        return !(stack.size()>0);
      }
    
      void pause() {
        if (sync_to_looping==true) {
          println("Pause was not set as user stack is tight to sketch's current lopping state");
          return;
        }
        pauseFlag=true;
      }
    
      void align_to_sketch() {
        if (pauseFlag==true) {
          println("sync_to_looping can only be set when current user stack is running.");
          return;
        }
        sync_to_looping=true;
      }
    
      void resume() {
        pauseFlag=false;
      }
    
    
    
    
      class StackActionObject {
    
    
        String methodFun;
        boolean done;
        boolean activated;
        boolean pause;
    
        int duration;
        int lapsedTime;
        int lastTime;
    
        StackActionObject() {
          duration=0;
          lapsedTime=0;
          lastTime=-1;  
          done=false;
          methodFun=null;
          pause=false;
        }
    
        StackActionObject(int _duration, String mc) {
          duration= _duration;
          lapsedTime=0;      
          lastTime=-1;  
          done=false;
          activated=false;
          methodFun=mc;
          pause=false;
        }
    
        void init() {
    
          if (duration>0) {
            done=false;
            activated=true;
            lastTime=millis();
            lapsedTime=0;
            run(false);
          }
        }
    
        void run(boolean pauseRequested) {    
    
          //P  PR
          //0  0  Normal
          //0  1  Return, does not execute run content
          //1  1  Return
          //1  0
    
          if (pause && pause!=pauseRequested) {
            pause=pauseRequested;  //When restoring exection
            lastTime=millis();
            return;
          }
    
          pause=pauseRequested;  //Updating field when pausing
    
          if (pause==true)
            return;
    
          lapsedTime += millis()-lastTime;
          lastTime=millis();
    
          if (lapsedTime > duration-30) {
            done=true;
            method(methodFun);
            return;
          }
    
    
          method(methodFun);
        }
    
        boolean isDone() {
          return done;
        }
      }
    }
    
  • FATAL EXCEPTION: GLThread error. "The pixels array is not available in this renderer..."

    I get this error on compile, any idea how to fix? I'm using the P3D renderer, but I get the same error with P2D, or the default renderer. "The pixels array is not available in this renderer withouth a backing bitmap" error regardless of which renderer I use. Also, they spelled without wrong.

    FATAL EXCEPTION: GLThread 42277
    Process: processing.test.myapp, PID: 18315
    java.lang.RuntimeException: The pixels array is not available in this renderer withouth a backing bitmap
        at processing.a2d.PGraphicsAndroid2D.updatePixels(Unknown Source)
        at processing.test.myapp.myapp.fadeGraphics(myapp.java:624)
        at processing.test.myapp.myapp.draw(myapp.java:291)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.opengl.PSurfaceGLES$RendererGLES.onDrawFrame(Unknown Source)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1583)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1286)
    
  • Fade to black at irregular frequence, how to loop on the last fade ?!

    Dear Jeremy, Yes, I would like to to this :

    • show turkish; fade
    • show french; fade
    • show alsacian; fade
    • start over?

    This should be a fade-out / fade-in at the and of the prevoious/ begining of the next text .A single text ever appear at the same time. Each period of showing should be of different durations.

    • turkish : 1 min
    • french : 3 min
    • alsacian : 2 min

    I hope this time it is clear? Thank you very much for your help. best, L

  • Fade to black at irregular frequence, how to loop on the last fade ?!

    Dear kfrajer,

    Thank you very much, it works better than my version for sure ;)) Strangely there is a blinck of black frame at the end of the 1rst fade and it looks as the fades in work properly, not the fades out ?! It doesn't loop properly with fadeSartTime=millis(), why? Sorry to bother with these new questions... Thanks for the help. best, L

  • Fade to black at irregular frequence, how to loop on the last fade ?!

    My attempt as a proof of concept and with room for improvement.

    Kf

    P.S. Notice your code is not properly working... Do you need to figure out the colors?

    int fadeStartTime;
    int fadeDuration = 2000; // 2 seconds
    int intervalTime=6000;
    float f;
    
    int state=0;
    
    void setup() {
      size(1920, 1080);
      frameRate(25);
      fadeStartTime = millis();
      //f = fade(millis(), fadeStartTime, fadeDuration, 0, 255); //0->3secs = 0->255 alpha
    }
    
    void draw() {
      background(255);
      noStroke();
    
      int dur = millis()-fadeStartTime;
    
    
      // 1)TURKISH
      if ( millis() >=dur && dur <fadeStartTime+fadeDuration ) {
        state=0;
      } else if ( dur >fadeStartTime+fadeDuration+ intervalTime && dur <fadeStartTime+fadeDuration+ intervalTime +fadeDuration ) {
        state=1;
      } else if (dur >fadeStartTime+fadeDuration*2+ intervalTime && dur < fadeStartTime+fadeDuration*3+ intervalTime ) { 
        state=2;
    
    
        // 2)FRENCH
      } else if (dur > fadeStartTime+fadeDuration*3+ intervalTime*2 && dur < fadeStartTime+fadeDuration*4+ intervalTime*2 ) {
        state=3;
      } else if (dur > fadeStartTime+fadeDuration*4+ intervalTime*2 && dur < fadeStartTime+fadeDuration*5+ intervalTime*2 ) { 
        state=4;
    
        // 3)ALSACIAN
      } else if (dur > fadeStartTime+fadeDuration*5+ intervalTime*3 && dur < fadeStartTime+fadeDuration*6+ intervalTime*3 ) {
        state=5;
      } else if (dur > fadeStartTime+fadeDuration*6+ intervalTime*3 && dur < fadeStartTime+fadeDuration*7+ intervalTime*3 ) {
        state=6;
      } else {
        state=0;
        fadeStartTime = millis();
      }
    
    
    
      switch(state) {
      case 0:
        f = fade(dur, fadeStartTime, fadeDuration, 255, 0); //0->3secs = 0->255 alpha : fade to black
        break;
      case 1:
        f = fade(dur, fadeStartTime+fadeDuration+ intervalTime, fadeStartTime+fadeDuration+ intervalTime+ fadeDuration, 0, 255);//8->10secs = 255->0 alpha : fade to black
        break;
      case 2:
        f = fade(dur, fadeStartTime+fadeDuration*2+ intervalTime, fadeStartTime+fadeDuration*3+ intervalTime, 255, 0);//10->12secs = 0->255 alpha :fade to text
        break;
      case 3:
        f = fade(dur, fadeStartTime+fadeDuration*3+ intervalTime*2, fadeStartTime+fadeDuration*4+ intervalTime*2, 0, 255);//18->20secs = 255->0 alpha: fade to black
        break;
      case 4:
        f = fade(dur, fadeStartTime+fadeDuration*4+ intervalTime*2, fadeStartTime+fadeDuration*5+ intervalTime*2, 255, 0);//20->22secs = 255->0 alpha: fade to text
        break;
      case 5:
        f = fade(dur, fadeStartTime+fadeDuration*5+ intervalTime*3, fadeStartTime+fadeDuration*6+ intervalTime*3, 0, 255);//28->30secs = 255->0 alpha: fade to black
        break;
      case 6:
        f = fade(dur, fadeStartTime+fadeDuration*6+ intervalTime*3, fadeStartTime+fadeDuration*7+ intervalTime*3, 255, 0);//30->32secs = 255->0 alpha: fade to text
        break;
      }
    
      fill(0, 0, 0, f);
      rect(0, 0, 1920, 1080);
    }
    
    float fade (float amt, float start1, float stop1, float start2, float stop2) {
    
      return constrain(
        map ( amt, start1, stop1, start2, stop2 ), 
        min(start2, stop2), 
        max(start2, stop2)
        );
    }
    
  • Fade to black at irregular frequence, how to loop on the last fade ?!

    Hi Jeremy and thanks for answering my question...I would like to fade to black be tween each language, this is working, that each text will be screen during different Time, this should work too but after these 3 languages I' like to start over again(turkish, french and aksacian) and I don ' t find a way to do so. Thanks a lot for your help. Best, laurent

  • Fade to black at irregular frequence, how to loop on the last fade ?!

    I don't know how to loop and re-initiate the same 3 fades again and again, any tip please

    Please explain clearly what you want the code to do. Do you want it to:

    1. show turkish; fade
    2. show french; fade
    3. show alsacian; fade
    4. start over?

    Should this be a fade-out / fade-in, or a cross-fade? Should two different texts ever appear at the same time? Should each period of showing be the same duration, or different durations? EXPLAIN.

  • Fade to black at irregular frequence, how to loop on the last fade ?!

    Hello, I wrote this short code below. It compiles, but I don't know how to loop and re-initiate the same 3 fades again and again, any tip please?! Thanks a lot in advance. Best, L

    int fadeStartTime;
    int fadeDuration = 2000; // 3 seconds
    int intervalTime=6000;
    float f;
    
    void setup() {
      size(1920, 1080);
      frameRate(25);
      fadeStartTime = millis();
      //f = fade(millis(), fadeStartTime, fadeDuration, 0, 255); //0->3secs = 0->255 alpha
    }
    
    void draw() {
      background(255);
      noStroke();
    
      int dur = millis()-fadeStartTime;
    
      // 1)TURKISH
      if ( millis() >=dur && dur <fadeStartTime+fadeDuration ) {
        f = fade(dur, fadeStartTime, fadeDuration, 255, 0); //0->3secs = 0->255 alpha : fade to black
      }  
      if ( dur >fadeStartTime+fadeDuration+ intervalTime && dur <fadeStartTime+fadeDuration+ intervalTime +fadeDuration ) {
        f = fade(dur, fadeStartTime+fadeDuration+ intervalTime, fadeStartTime+fadeDuration+ intervalTime+ fadeDuration, 0, 255);//8->10secs = 255->0 alpha : fade to black
      }  
      if (dur >fadeStartTime+fadeDuration*2+ intervalTime && dur < fadeStartTime+fadeDuration*3+ intervalTime ) { 
        f = fade(dur, fadeStartTime+fadeDuration*2+ intervalTime, fadeStartTime+fadeDuration*3+ intervalTime, 255, 0);//10->12secs = 0->255 alpha :fade to text
    
        // 2)FRENCH
      }  
      if (dur > fadeStartTime+fadeDuration*3+ intervalTime*2 && dur < fadeStartTime+fadeDuration*4+ intervalTime*2 ) { 
        f = fade(dur, fadeStartTime+fadeDuration*3+ intervalTime*2, fadeStartTime+fadeDuration*4+ intervalTime*2, 0, 255);//18->20secs = 255->0 alpha: fade to black
      }  
      if (dur > fadeStartTime+fadeDuration*4+ intervalTime*2 && dur < fadeStartTime+fadeDuration*5+ intervalTime*2 ) { 
        f = fade(dur, fadeStartTime+fadeDuration*4+ intervalTime*2, fadeStartTime+fadeDuration*5+ intervalTime*2, 255, 0);//20->22secs = 255->0 alpha: fade to text
    
        // 3)ALSACIAN
      } 
      if (dur > fadeStartTime+fadeDuration*5+ intervalTime*3 && dur < fadeStartTime+fadeDuration*6+ intervalTime*3 ) { 
        f = fade(dur, fadeStartTime+fadeDuration*5+ intervalTime*3, fadeStartTime+fadeDuration*6+ intervalTime*3, 0, 255);//28->30secs = 255->0 alpha: fade to black
      }  
      if (dur > fadeStartTime+fadeDuration*6+ intervalTime*3 && dur < fadeStartTime+fadeDuration*7+ intervalTime*3 ) { 
        f = fade(dur, fadeStartTime+fadeDuration*6+ intervalTime*3, fadeStartTime+fadeDuration*7+ intervalTime*3, 255, 0);//30->32secs = 255->0 alpha: fade to text
      } 
      fill(0, 0, 0, f);
      rect(0, 0, 1920, 1080);
    
      if (dur > fadeStartTime+fadeDuration*7+ intervalTime*3) {
        // HOW TO LOOP?
      }
    }
    
    float fade (float amt, float start1, float stop1, float start2, float stop2) {
    
      return constrain(
        map ( amt, start1, stop1, start2, stop2 ), 
        min(start2, stop2), 
        max(start2, stop2)
        );
    }
    
  • Question about openProcessing sketch (Retro CRT TV Distortion Effect)

    I ran into an interesting legacy sketch by Luis Gonzalez on openProcessing and I was trying to figure out how it works. Unfortunately it seems like it's not working for me, at least in processing 3. What needs to be changed to make this work as intended? Here's a link to the sketch: https://www.openprocessing.org/sketch/112597#

    /*
    
     Retro CRT TV Distortion Effect 
    
     RGB Shifting for random color bleeding
     television scanlines
     random flicker of entire image
     rolling bar
     tv noise static
     turning on fades in image from black
     turning off fades image to white while shrinking it to a horizontal line
    
     */
    
    int w=480;
    int h=270;
    PImage tvscreen;
    PImage TVOverlay;
    PImage tvnoise;
    
    int barY1=10;
    int barY2=30;
    
    
    //tv noise
    int[] ppx;
    int[] px = new int[w];
    
    boolean tvstate = true;
    
    
    long timeIndexInfo;
    long firstCountInfo;
    
    float lerpAmount = 0;
    float lerpScale = 0;
    float lerpShrink = 0;
    
    int tvheight=0;
    int tvwidth=0;
    
    void setup()
    {
      size(480, 270, P2D);
      tvscreen=loadImage("obey.png");
      TVOverlay=loadImage("tv.png");
    
      // precalculate tv noise
      tvnoise = createImage(w,h,RGB);
    
      tvnoise.loadPixels();
      ppx = new int[tvnoise.pixels.length];
      for (int y = 0; y < ppx.length;)
        ppx[y++] = int(random(-32,32));
      loadPixels();
    
    
      tvwidth = w;
      tvheight = h;
    
      timeIndexInfo = millis();
      firstCountInfo = millis();
    
      noSmooth();
    
    }
    
    void draw() {
      background(0);
      renderDistort();
      image(TVOverlay, 0, 0);
    }
    
    int powerUpCounter = 256;
    
    void scaleIt(float amountA, float amountB, int valueA, int valueB, int valueC, int valueD){
      if (lerpAmount < 1)
      {
        lerpAmount += amountA;
        powerUpCounter = (int)lerp(valueA,valueB,lerpAmount);
      }
    
      if (lerpScale < 1)
      {
        lerpScale += amountB;
        tvheight = (int)lerp(valueC,valueD,lerpScale);
      }  
    
      // final horiontal line shrink on power off
      if (!tvstate && tvheight < 100)
      {
        if (lerpShrink < 1)
        {
          tvwidth = (int)lerp(width,1,lerpShrink);
          lerpShrink += .09;
    
    
    
        }
      }
    }
    
    // RGB Distort
    void renderDistort() {
      int i = 0;
    
      int offRed  = (int)(Math.random() * 2) * 2;
      int offGreen= (int)(Math.random() * 2) * 2;
      int offBlue = (int)(Math.random() * 2) * 2;
    
      if (barY2 > h) {
        barY1=10 -40;
        barY2=30 -40;
      }
    
      barY1 +=2;
      barY2 +=2;
    
      if (tvstate)
        scaleIt(0.02,0.2,156,0,2,tvscreen.height);  // turning on 
      else
        scaleIt(0.1,0.2,0,-400,tvscreen.height,1);   // turning off
    
      // dark vs light flicker + gradual fade in
      int flicker = (offBlue*8)+powerUpCounter; 
    
      for ( int y = 1; y < h; y++ ) {
    
        // vertically moving horizonal strip + flicker
        int colDiv = ( y < barY2 && y > barY1 ) ? 20+flicker : flicker; 
    
        // horizontal scanlines
        int strips=(y&1)*64 +colDiv; 
    
        // grab a random line of precalculated TV noise
        int noiseLine = int(random(0,height)) * width;
    
        for ( int x=0; x < w; x++ ) {
          int imagePixelR = tvscreen.pixels[i+offRed] >> 16 & 0xFF ;
          int imagePixelG = tvscreen.pixels[i+offGreen] >> 8 & 0xFF ;
          int imagePixelB = tvscreen.pixels[i+offBlue] & 0xFF ; 
          int processEffect = -strips-ppx[noiseLine+x];
          tvnoise.pixels[i++] =  color(imagePixelR+processEffect, imagePixelG+processEffect, imagePixelB+processEffect);      
        }
      }
      tvnoise.updatePixels();
    
      //  image(tvnoise,0,0,tvscreen.width,270);
    
    
    
    
      image(tvnoise,(tvscreen.width-tvwidth/2)-tvscreen.width/2,(tvscreen.height-tvheight/2)-tvscreen.height/2,tvwidth,tvheight);
    }
    
    void mousePressed(){
      lerpAmount = 0;
      lerpScale = 0;
      lerpShrink = 0;
      tvstate = !tvstate;
      //tvheight=280;
      tvwidth=width;  
      println(" tvheight: " + tvheight + " tvscreen.height: " + tvscreen.height );
    }
    
  • Help please! ARDUINO and PROCESSING temperature sensor code

    Hi all,

    so for a project with school i had created a temperature sensor using an arduino and breadboard circuit i will leave my code below for the arduino. My issue is that i cannot receive the data from the arduino using processing. I want to create a temperature graph to show on processing.

    Arduino code:

    #include <LiquidCrystal.h>
    
    int tempPin = 0;
    int lightPin = 1;
    
    /*const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
    LiquidCrystal lcd(rs, en, d4, d5, d6, d7);*/
    
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    
    int value=0;            
    float volts=0.0;      
    float temp=0.0;      
    float tempF=0.0;
    int sensorPin = A0;
    
    void setup() 
    {
      pinMode(3,INPUT);      //setting arduino pin3 as input
      Serial.begin(9600);
      lcd.begin(16, 2);
    }
    
    void loop()
    {
    Serial.println(analogRead(0));
     delay(50); 
    
      //tempReading = analogRead(tempPin);
    
      int tempReading = analogRead(A0);
      Serial.write(tempReading);
      float tempVolts = tempReading * 5.0 / 1024.0;
      float tempC = (tempVolts - 0.5) * 100.0;
      float tempF = tempC * 9.0 / 5.0 + 32.0;
    
      lcd.print("TempC             ");
      lcd.setCursor(6, 0);
      lcd.print(tempC);
    
      int lightReading = analogRead(lightPin);
      lcd.setCursor(0, 1);
    
      lcd.print("TempF              ");  
      lcd.setCursor(6, 1);
      lcd.print(tempF);
      Serial.print(tempC);
      Serial.println(tempF);
      delay(500);
    
    }
    
    
    Processing code:
    import processing.serial.*;
    //import cc.arduino.*;
    //Arduino arduino;
    
    
    
    //init variables
    Serial commPort;
    int val;
    float tempC;
    float tempF;
    //float tempVolts;
    int yDist;
    float[] tempHistory = new float[100];
    
    void setup()
    {
     //setup fonts for use throughout the application
    //set the size of the window
     size(900,450);
    //init serial communication port
     commPort = new Serial(this, Serial.list()[0], 9600);
    
    //fill tempHistory with default temps
     for(int index = 0; index<100; index++)
     tempHistory[index] = 0;
    }
    
    void draw()
    {
     //get the temp from the serial port
     while (commPort.available() > 0) 
     {
     tempC = commPort.read();
    //refresh the background to clear old data
     background(123);
    
     //draw the temp rectangle
     colorMode(RGB, 100);  //use color mode sized for fading
     stroke (0);
     rect (49,19,22,162);
     //fade red and blue within the rectangle
     for (int colorIndex = 0; colorIndex <= 160; colorIndex++) 
     {
     stroke(160 - colorIndex, 0, colorIndex);
     line(50, colorIndex + 20, 70, colorIndex + 20);
     }
    //draw graph
     stroke(0);
     fill(255,255,255);
     rect(90,80,100,100);
     for (int index = 0; index<100; index++)
     { 
     if(index == 99)
     tempHistory[index] = tempC;
     else
     tempHistory[index] = tempHistory[index + 1];
     point(90 + index, 180 - tempHistory[index]); 
     }
    //write reference values
     fill(10,10,10);
    textAlign(RIGHT);
     text("100 C", 45, 25); 
     text("0 C", 45, 187);
    //draw triangle pointer
     yDist = int(160 - (160 * (tempC * 0.01)));
     stroke(0);
     triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
    //write the temp in C and F
     fill(0,0,0);
    textAlign(LEFT);
     text(str((tempC)) + " C", 115, 37);
    

    tempF = ((tempC*9)/5) + 32; text(str((tempF)) + " F", 115, 65); } }

  • How to get the random function to stop generating random numbers

    Unfortunately, the random number generator will continue to keep generating random numbers for some reason which causes each pixel to continually change color values (I want my background to be static after it has generated a value for each pixel).

    Drawing once to a PGraphics rather than changing random sounds like the way to go. This would also make it easier to cross-fade the fixed image of your static background into your vignette background, for example by using tint.

  • How to draw a dynamic ECG curve (aka machine that gowe Bing)

    @TfGuy44 Very nice! However, the faded-out lines leave a grayish trace, see here: http://tinybrain.de/1013743

    Any way to fix this?

    Here's the actual program: http://tinybrain.de/1013742

  • Basic questions about reducing CPU usage when drawing pictures

    Works and reduced CPU to <1% in idle. This is how the draw() looks. (Just for reference)

    void draw() {
      background(255);
    
      for (int i=0; i<cols; i++) {
        for (int j=0; j<rows; j++) {
          int x = i*boxsize;
          int y = j*boxsize;
    
          if (mouseX > x && mouseX < (x + boxsize) && mouseY > y && mouseY < (y + boxsize)) {
            if (mousePressed && (mouseButton == LEFT)) {
              // Übergibt den char des betreffenden Feldes
              char temp = (chars[i][j]);
              port.write((int)temp);
              delay(200);
            } // end of if
          } // end of if
          // Füllt die Felder mit Farben und erzeugt die Rechtecke
          fill(colors[i][j]);
          rect(x, y, boxsize, boxsize);
    
          // Setzen der Bilder
          image(fade, 1, 241);
          image(red, 81, 241);
          image(green, 161, 241);
          image(blue, 241, 241);
          image(plus, 401, 241);
          image(minus, 481, 241);
        } // end of for
      } // end of for
      noLoop();
    } // end of draw
    
  • Basic questions about reducing CPU usage when drawing pictures

    Hello, I wrote a program for choosing colors for an LED-Strip. It works quite good, but it seems to be using quite alot of CPU. (30% CPU, i5 7600k @4.6ghz)

    I know that this usage is due to my program constantly loading pictures:

    Setup method:

          fade = loadImage("fade.jpg");
          red = loadImage("red.jpg");
          green = loadImage("green.jpg");
          blue = loadImage("blue.jpg");
          plus = loadImage("plus.jpg");
          minus = loadImage("minus.jpg");
    

    Draw-Method:

          image(fade, 1, 241, boxsize-1, boxsize-1);
          image(red, 82, 241, boxsize-2, boxsize-1);
          image(green, 161, 241, boxsize-1, boxsize-1);
          image(blue, 241, 241, boxsize-1, boxsize-1);
          image(plus, 401, 241, boxsize-1, boxsize-1);
          image(minus, 481, 241, boxsize-1, boxsize-1);
    

    Can someone help me to adapt it? How do I load the pictures without consantly having to draw them? It would be enough to draw them once when the program starts.

  • Minim/Tuio combination

    I have some question regarding the Minim library,

    I tried the setVolume or setGain instances both didn't change anything. I am trying to start with no sound till tuioCursorList.size is bigger than 0 and when it is, sound can fade in with a simple for loop but all my attempts on changing the volume failed so couldn't implement it to the code.

    I got part of the code from Minim > Synthesis > realtimeControlExample and some stuff are out of my reach. In the code below. The out.PlayNote takes 3 parameter I got the idea of them first is the start point, second is the duration and thirds is the note which will be played. How can I set so that the note plays not just for defined time but as long as program is running?

    And the I tried calling out.playNote in draw and didn't work, works only when its called setup which makes it harder for me to change the parameters.

    // import everything necessary to make sound.
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    import ddf.minim.effects.*;
    
    import TUIO.*;
    TuioProcessing tuioClient = new TuioProcessing(this);
    ArrayList<TuioCursor> tuioCursorList = new ArrayList<TuioCursor>();
    
    
    Minim minim;
    AudioOutput out;
    NoiseInstrument myNoise;
    float x = 0;
    float y = 0;
    float xPos;
    float yPos;
    void setup()
    {
      size( 500, 500, P2D );
    
    
      minim = new Minim( this );
      out = minim.getLineOut( Minim.MONO, 512 );
      myNoise = new NoiseInstrument( 1.0, out );
    
      out.playNote( 0, 100.0, myNoise );
    }
    
    
    void draw()
    {
      background( 0 );
      tuioCursorList = tuioClient.getTuioCursorList();
    
      for (int i=0; i<tuioCursorList.size(); i++) {
    
        TuioCursor tc = tuioCursorList.get(i);
        xPos = tc.getScreenX(width);
        yPos = tc.getScreenY(height);
        if ( tuioCursorList.size() > 0) {
          float freq = map( yPos, 0, height, 200, 120 );
          float q = map( xPos, 0, width, 15, 25 );
          myNoise.setFilterCF( freq );
          myNoise.setFilterQ( q );
          println(tuioCursorList.size());
        }
      }
    }
    
    
    
    // Every instrument must implement the Instrument interface so 
    // playNote() can call the instrument's methods.
    
    // This noise instrument uses white noise and two bandpass filters
    // to make a "whistling wind" sound.  By changing using the methods which
    // change the frequency and the bandwidth of the filters, the sound changes.
    
    class NoiseInstrument implements Instrument
    {
      // create all variables that must be used throughout the class
      Noise myNoise;
      Multiplier multiply;
      AudioOutput out;
      BandPass filt1, filt2;
      Summer sum; 
      float freq1, freq2, freq3;
      float bandWidth1, bandWidth2;
      float filterFactor;
    
      // constructors for this intsrument
      NoiseInstrument( float amplitude, AudioOutput output )
      {
        // equate class variables to constructor variables as necessary 
        out = output;
    
        // give some initial values to the realtime control variables
        freq1 = 150.0;
        bandWidth1 = 10.0;
        filterFactor = 1.7;
    
        // create new instances of any UGen objects
        myNoise = new Noise( amplitude, Noise.Tint.WHITE );
        multiply = new Multiplier( 0 );
        filt1 = new BandPass( freq1, bandWidth1, out.sampleRate() );
        filt2 = new BandPass( freq2(), bandWidth2(), out.sampleRate() );
        sum = new Summer();
    
        // patch everything (including the out this time)
        myNoise.patch( filt1 ).patch( sum );
        myNoise.patch( filt2 ).patch( sum );
        sum.patch( multiply );
      }
    
      // every instrument must have a noteOn( float ) method
      void noteOn( float dur )
      {
        // set the multiply to 1 to turn on the note
        multiply.setValue( 1 );
        multiply.patch( out );
      }
    
      // every instrument must have a noteOff() method
      void noteOff()
      {
        // set the multiply to 0 to turn off the note 
        multiply.setValue( 0 );
        multiply.unpatch( out );
      }
    
      // this is a helper method only used internally to find the second filter
      float freq2()
      {
        // calculate the second frequency based on the first
        return filterFactor*freq1;
      }
    
      // this is a helper method only used internally 
      // to find the bandwidth of the second filter
      float bandWidth2()
      {
        // calculate the second bandwidth based on the first
        return filterFactor*bandWidth1;
      }
    
      // this is a method to set the center frequencies
      // of the two filters based on the CF of the first
      void setFilterCF( float cf )
      {
        freq1 = cf;
        filt1.setFreq( freq1 );
        filt2.setFreq( freq2() );
      }
    
      // this is a method to set the bandwidths
      // of the two filters based on the BW of the first
      void setFilterBW( float bw )
      {
        bandWidth1 = bw;
        filt1.setBandWidth( bandWidth1 );
        filt2.setBandWidth( bandWidth2() );
      }
    
      // this is a method to set the Q (inverse of bandwidth)
      // of the two filters based on the  
      void setFilterQ( float q )
      {
        setFilterBW( freq1/q );
      }
    }
    
    // called when a cursor is added to the scene
    void addTuioCursor(TuioCursor tcur) {
    }
    
    // called when a cursor is moved
    void updateTuioCursor (TuioCursor tcur) {
    }
    
    // called when a cursor is removed from the scene
    void removeTuioCursor(TuioCursor tcur) {
    }
    
    // --------------------------------------------------------------
    // these callback methods are called whenever a TUIO event occurs
    // there are three callbacks for add/set/del events for each object/cursor/blob type
    // the final refresh callback marks the end of each TUIO frame
    
    // called when an object is added to the scene
    void addTuioObject(TuioObject tobj) {
    }
    
    // called when an object is moved
    void updateTuioObject (TuioObject tobj) {
    }
    
    // called when an object is removed from the scene
    void removeTuioObject(TuioObject tobj) {
    }
    
    // --------------------------------------------------------------
    // called when a blob is added to the scene
    void addTuioBlob(TuioBlob tblb) {
    }
    
    // called when a blob is moved
    void updateTuioBlob (TuioBlob tblb) {
    }
    
    // called when a blob is removed from the scene
    void removeTuioBlob(TuioBlob tblb) {
    }
    
    // --------------------------------------------------------------
    // called at the end of each TUIO frame
    void refresh(TuioTime frameTime) {
    }
    
  • Brightness tracking on background image

    I'm not quite sure what you are trying to do -- are you trying to shine a flashlight to scrape away black and reveal a hidden image?

    1. Load a hidden image PImage hidden
    2. Create an empty mask PGraphics mask
    3. Draw a faded circle on the mask centered on the brightest point on the camera (if it is brighter than x)
    4. Apply the mask
    5. Draw the masked image

    Over time, this will draw more and more areas on the mask, showing more and more of the masked image.

  • How to make some texte move and change its opacity ?

    Here are some quick, rough examples of the functional approach, with no classes, objects, or per-letter variables -- just a string, and that's it.

    StringAnimationDemos--screenshot

    /**
     * StringAnimationDemos -- write functions that do per-letter string manipulation
     * Jeremy Douglass 2018-01-19 Processing 3.3.6
     *
     * Each function is called once in draw. Each function is a independent, 
     * and could be cut-pasted into another sketch. To use, first translate
     * to the screen location for drawing, then call the function with a string
     * and the "amount". Here the amount is given by the mouse x position
     * but it could be based on a timer, or something else.
     **/
    
    String letters = "String manipulation testing";
    
    
    void setup() {
      size(200, 300);
    }
    
    void draw() {
      background(64);
      translate(20, 30);
      // draw a string
      text(letters, 0, 0);
      translate(0, 20);
      // draw a string yourself, one letter at a time
      drawLetters(letters);
      translate(0, 20);
      // draw letters that fade in a line, controlled by mouse
      drawLettersFadeLine(letters);
      translate(0, 20);
      // draw letters that fade, controlled by mouse
      drawLettersFadeOut(letters, mouseX/float(width));
      translate(0, 20);
      // draw letters that react to the mouse
      drawLettersHide(letters, mouseX/float(width));
      translate(0, 20);
      // draw letters that fade, controlled by mouse
      drawLettersFadeLineOut(letters, mouseX/float(width));
      translate(0, 60);
      // draw letters that scatter, controlled by mouse
      drawLettersScatterHide(letters, mouseX/float(width));
      translate(0, 60);
      // draw letters that scatter, controlled by mouse
      drawLettersScatterFade(letters, mouseX/float(width));
    }
    
    void drawLetters(String s) {
      float xoffset = 0;
      for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);        
        text(c, xoffset, 0);
        xoffset += textWidth(c);
      }
    }
    
    void drawLettersHide(String s, float amt) {
      float stringAmt = 0;
      float xoffset = 0;
      for (int i = 0; i < s.length(); i++) {
        stringAmt = (i+1)/float(s.length()+1);
        char c = s.charAt(i);
        if (stringAmt > amt) {  // skip letter positions less than amt
          text(c, xoffset, 0);
        }
        xoffset += textWidth(c);
      }
    }
    
    void drawLettersFadeLine(String s) {
      float stringAmt = 0;
      float xoffset = 0;
      for (int i = 0; i < s.length(); i++) {
        stringAmt = (i+1)/float(s.length()+1);
        char c = s.charAt(i);
        pushStyle();
        float opacity = 255 * (1-stringAmt);  // fade letters by positions
        fill(255, opacity);
        text(c, xoffset, 0);
        popStyle();
        xoffset += textWidth(c);
      }
    }
    
    void drawLettersFadeOut(String s, float amt) {
      float xoffset = 0;
      pushStyle();
      float opacity = 255 * (1-amt);  // fade the whole string by amt
      for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        fill(255, opacity);
        text(c, xoffset, 0);
        xoffset += textWidth(c);
      }
      popStyle();
    }
    
    void drawLettersFadeLineOut(String s, float amt) {
      float stringAmt = 0;
      float xoffset = 0;
      for (int i = 0; i < s.length(); i++) {
        stringAmt = (i+1)/float(s.length()+1);
        char c = s.charAt(i);
        pushStyle();
        // fade each letter by its position and the amt
        float opacity = 255 * (2 + (stringAmt-1) - (2*amt));
        fill(255, opacity);
        text(c, xoffset, 0);
        popStyle();
        xoffset += textWidth(c);
      }
    }
    
    void drawLettersScatterHide(String s, float amt) {
      float stringAmt = 0;
      float xoffset = 0;
      float travel = 0;
      for (int i = 0; i < s.length(); i++) {
        stringAmt = (i+1)/float(s.length()+1);
        char c = s.charAt(i);
        travel = amt;
        pushMatrix();
        // move each letter in a different direction
        rotate(i);
        translate(0, travel*50);
        rotate(-i);
        if (stringAmt > amt) {  // skip letter positions less than amt
          text(c, xoffset, 0);
        }
        xoffset += textWidth(c);
        popMatrix();
      }
    }
    
    void drawLettersScatterFade(String s, float amt) {
      float stringAmt = 0;
      float xoffset = 0;
      float travel = 0;
      for (int i = 0; i < s.length(); i++) {
        stringAmt = (i+1)/float(s.length()+1);
        char c = s.charAt(i);
        travel = amt;
        pushMatrix();
        // move each letter in a different direction
        rotate(i);
        translate(0, travel*50);
        rotate(-i);
        pushStyle();
        // fade each letter by its position and the amt
        float opacity = 255 * (2 + (stringAmt-1) - (2*amt));
        fill(255, opacity);
        text(c, xoffset, 0);
        popStyle();
        xoffset += textWidth(c);
        popMatrix();
      }
    }
    
  • How to make some texte move and change its opacity ?

    I have no idea which function I should use

    You will have to write your own function -- or your own class with methods.

    If you do not want to write a Letter class and create an array of Letter[] letters, then you will need to use parallel arrays. The only reason to consider doing that is that many of your variables are all derived in principle, so you don't need to store them. All you need is:

    1. a string (which is an array of letters)
    2. an array of random headings, one per letter
    3. an input of the elapsed animation amount, from 0 (nothing moved) to 1 (every letter has disappeared).

    This approach isn't necessarily easier to program, but it might be a bit easier to e.g. play backwards (letters fade in), scrub with the mouse, dynamically generate for different string lengths, etc. However as soon as you start adding many more properties (like one color per letter, or one size per letter, etc.) then you will start wishing for the Class / Object approach.

    P.S. -- actually, you could probably get rid of the random headings list too if you just use assign spinning headings -- e.g. 1, 2, 3, 4 radians.

  • How to switch between multiple movies?/Play movie after a movie?

    myMovie 1 (should be looping). When something enters it switches to myMovie 2. When the person leaves/and when the end of myMovie2 has been reached, it should play myMovie3 at full length, regardless if something enters or leaves the area. after myMovie3 happened it should switch back to the start of myMovie 1.

    Keep in mind that if you are planning to extend your logic, it currently looks like this:

    The background movie loops. When an event happens, a sequence of one or more movies plays, then the background restarts.

    ...which is, slightly more abstract:

    Here is a list of movies that lead to other movies.

    ...with looping as a special case handling of bgMovie -> bgMovie.

    One way to approach that in general is a queue like this:

    0: bgMovie, 0 (infinite loop)
    

    ...and when an event happens you add things to the front, like this:

    0: movie2, 45 // now playing
    1: movie3, 15
    2: bgMovie, 0
    

    When a movie finishes playing, you remove it from the queue and start playing the next movie.

    0: movie3, 15  // now playing
    1: bgMovie, 0
    
    0: bgMovie, 0  // now playing
    

    Now you can add really complex behaviors to your movie system easily by just adding / removing queued movies -- you don't have to program a set of special rules for each transition. If events happen to change the remaining duration of a movie, or remove it, or switch what comes next, you can express all those things concisely as operations that edit queue items. Common ones might be:

    • Replace everything in the queue
    • Add things after the current item
    • Remove all items from the queue but the last one

    If you want to add complex behaviors like cross-fading, you don't have to create a cross-fade for every single possible transition -- you can just create one cross-fade behavior that acts on the current and next items in the queue. Et cetera.