opacity low and high on image through tint()

edited May 2016 in Arduino

Hi,

I am almost finished with my code but i am experience a little problem. I am writing a program where i have one background image and one image "on top of the background" which changes its opacity with a potentiometer. When the potentiometer is at 0 the opacity on the image on top of the background is 0%, and when the potentiometer is 1023 the image on top of the background is 100%. So far i've managed to make processing listen to the potentiometer, and it is able to fade in through the changing of opacity, so when the potentiometer is turned up from 0 to 1023, the image's opacity goes from 0-100. BUT it will not fade out again. eg. when the potentiometer is at 1023 and i go down to 500, the image's opacity does not follow and the image does not fade again. I have a deadline on this project tomorrow, so i appriciate all the help i can get. Here is my code from processing:

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int sensorPin = 2; 

PImage bg;
PImage img;
PImage imgstreg;
float mappedValue;

void setup(){
arduino = new Arduino(this, Arduino.list()[2], 57600);
  arduino.pinMode(2, Arduino.OUTPUT);


  size(550,770);
    img = loadImage("udensvedbilledemini.jpg");
    background(img);

}
void draw(){
    imgstreg = loadImage("svedbilledemini.jpg");
float sensorValue = arduino.analogRead(2);
println(sensorValue);

//i mapped the sensor value from 0-1500 instead of 1023, as the opacity gained too much in too little time
mappedValue = map(sensorValue, 0, 1500, 0, 100);

tint(255, mappedValue);
image(imgstreg,0,0);
delay (400);
}

Answers

  • https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Do not use loadImage() after setup() either! Acquire resources before draw() starts. ~O)

  • I put the second image in draw as it is supposed to loop, while the other one is the background.
    Whether I load the second image in setup or draw makes no difference for the code. But this still does not solve my problem of getting the tint to react to when the potentiometer is turned down and having the image to fade again. If anyone knows how to do this i would be very happy :-)

  • are you clearing the screen with every frame? if not the image will just keep building up.

  • I dont think im doing that - maybe thats the problem! Im new to this, so sorry for a "stupid" question, but how do I do that? which code and functions do i need to read up on?

  • background() will set the background to either a colour or an image.

  • edited May 2016

    is worked if i made an if sentence and set the background image. However i still can't control the tint (which i use to control the opacity) right. When the program on nothing starts until 800 and the it just goes up to 100% opacity.

    my code is now:

    import processing.serial.*;
    import cc.arduino.*;
    Arduino arduino;
    int sensorPin = 2; //ændrer pin alt efter sensor
    
    
    PImage bg;
    PImage img;
    PImage imgstreg;
    float mappedValue;
    
    void setup(){
      arduino = new Arduino(this, Arduino.list()[2], 57600);
      arduino.pinMode(2, Arduino.OUTPUT);
    
      size(550,770);
      img = loadImage("udensvedbilledemini.jpg");
      background(img);
    
      imgstreg = loadImage("svedbilledemini.jpg");
    }
    
    void draw(){
    
      float sensorValue = arduino.analogRead(2);
      println(sensorValue);
      mappedValue = map(sensorValue, 0, 1500, 0, 100);
    
      if (sensorValue  > 800){
        tint(255, 255, 255, 20);
        image(imgstreg,0,0);
        delay (400);
      }
      else if (sensorValue < 801) {
        tint(255, 255, 255, 20);
        image(imgstreg,0,0);
        delay (400);
        background(img);
      }
    }
    
  • delay() doesn't do what you think it does. it is not recommended.

  • you ONLY see the results of your drawing at the end of draw(). so if the last thing you do in draw is set the background (line 38) then that is ALL you will see.

    you also aren't using mappedValue.

Sign In or Register to comment.