How to make an image fade to another image when a certain amount of light falls on a LDR?

edited October 2015 in Arduino

Hi, I have been trying to make an image transistion to another image when a certain amount of light falls on a LDR, and transistions back when that light is not shining on the LDR anymore. However, the piece of code is not working properly. I am new to processing, so there is a good chance that I am doing it completely wrong. It would be lovely if someone could help me achieve the fade in and out! Thanks in advance! Here is a part of my code, and what I have tried to do for the fade:

int transparency = 0;

void setup() {
  img = loadImage(imgFileName+"."+fileType);
  bla = loadImage("mermaid.jpg");
  size(img.width, img.height);
  image(img, 0, 0);

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  arduino.pinMode(sensorPin, Arduino.INPUT);
}
void draw() {
      int analogValue =  arduino.analogRead(sensorPin);
      println(analogValue);

      if (analogValue > 900) {
        noTint();
        image(bla, 0, 0);
      } else {
        while (column < width-1) {
          img.loadPixels(); 
          sortColumn();
          column++;
          img.updatePixels();
        }

        while (row < height-1) {
          img.loadPixels(); 
          sortRow();
          row++;
          img.updatePixels();
        }

     if (transparency < 255) {
          transparency++; 
        }
        tint(255, 255, 255, transparency);
        image(img, 0, 0);
        //automatically save the image 
        if (!saved && frameCount >= loops) {
          saveFrame(imgFileName+"_"+mode+".png");
          saved = true;
        }
      }
    }

Answers

  • edited October 2015

    So the values from arduino come in as expected? Then you can use analogValue like any other variable from there. Since your code is not complete, here is a simple example that fades an image depending on your mouse-position:

    int treshold =50;
    int alpha, fadeVal;
    PImage img;
    
    void setup() {
      img = loadImage("https:"  + "//processing.org/reference/images/image_0.png");
    }
    
    void draw() {
      //int value =  arduino.analogRead(sensorPin);
      int value = mouseX; 
    
      if (value>treshold)
        fadeVal =1;
      else
        fadeVal = -1;
    
      alpha= constrain(alpha+fadeVal, 0, 255);
    
      background(0);
      tint(255, alpha);
      image(img, 0, 0);
    }
    
  • edited October 2015 Answer ✓

    i would distinguish between eval the analog signal and doing the fading

    meaning: when the signal comes in, set a boolean or int marker state and do a fading (ignoring the signal state is == doFade) afterwards go to a state waitForDefade and when signal comes fade other direction (state == doDefade) then go to state waitForFade

Sign In or Register to comment.