Loading...
Logo
Processing Forum
Hi...
I want to do the transition of images with respect to the intensity of light strike on LDR.
I manipulate the output of LDR from 1 to 10 in arduino and there is 10 images in processing, i want those images in processing to change with respect to the LDR output. 
But i am getting this error......

error, disabling serialEvent() for //./COM3
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.NullPointerException
at processing.core.PGraphics.image(PGraphics.java:3541)
at processing.core.PApplet.image(PApplet.java:11912)
at transition_of_images.serialEvent(transition_of_images.java:65)
... 8 more

Here is my code 

Copy code
  1. import processing.serial.*;
  2. Serial myPort;

  3. PImage[] imgs;

  4. int nbr_of_images;
  5. float transitionFrames = 30;
  6. int displayFrames = 50;
  7. float fadeValue = 0;

  8. float img_Actual;
  9. float img_Next;


  10. void setup() {
  11.   size(1440, 900);

  12.   nbr_of_images = 10;  
  13.   imgs = new PImage[nbr_of_images];  
  14.  println(Serial.list());
  15. // print a list of all available ports
  16. myPort = new Serial(this, Serial.list()[1], 9600);
  17. myPort.bufferUntil('\n');
  18. for (int i = 0; i < nbr_of_images; i++)
  19. {
  20.   imgs[i] = loadImage(nf(i, 2) + ".JPG");
  21. }
  22. }


  23. void draw () {
  24.  // everything happens in the serialEvent()
  25.  }
  26.  
  27.  void serialEvent (Serial myPort) {
  28.  // get the ASCII string:
  29.  String inString = myPort.readStringUntil('\n');
  30.  

  31.  // convert to an int and map to the screen height:
  32.  float inByte = float(inString); 
  33.  println(inByte);
  34.   
  35.       // full opacity for image in background
  36.       tint(255);
  37.       image(imgs[int(img_Actual)], 0, 0, width, height);
  38.       
  39.       // fade foreground-image using tint()
  40.       tint(255, lerp(0, 255, fadeValue));
  41.       image(imgs[int(img_Next)], 0, 0, width, height);
  42.             
  43.             // change image and reset fadeValue
  44.   if (frameCount % (displayFrames) == 0)
  45.   {
  46.     fadeValue = 0;
  47.     img_Actual = img_Next;
  48.     img_Next = inByte;
  49.   }
  50.    // increase opacity of second image
  51.       fadeValue = fadeValue+1/transitionFrames;
    }

Need your valuable suggestions...
Thanking you in anticipation

Replies(2)

I do not know what is causing the error but your code is structured in a way to make troubleshooting difficult. Since you are the one to figure this out yourself I think you ought to partition your code into distinct elements that allow you to separate what you know works, and may be complete, from what is added or not working. For example the serialEvent function should take care of receiving the incoming signal and do nothing else. In your case it appears that you have 10 images in an array and you want the serialEvent to make an indexing variable correspond to the latest valid incoming number from the Arduino. Incidentally, the index will vary from 1 to 10 and so it need not be a float. A simple byte would do. I am guessing your serialEvent function should read the latest incoming value, compare it to the current index value, and then change the current index value to that new value if it is different. You would put diagnostic printlns in there to follow the progress and pinpoint exactly where an error occurs.  All that other code for changing and fading the image belongs in its own function that you call in the draw loop. I am wondering where in the program you are getting the error message. Does the code ever reach the println(inByte)? Does the code ever reach the println(Serial.list())? I think there may also be the possibility you are shorting or drawing too much current at the Arduino thereby causing the computer's serial port power manager to shut down the port. That might result in such an odd error message. A test for that would be to run the program on a plain Arduino that does not have any circuits added to it. 
Hi 
Thank you for your reply...
You were right,  code crashed before reaching this line
Copy code
  1. println(inByte);

I am sending data from arduino by this method
Copy code
  1.  Serial.println(average*10/1023);
and getting in processing by 
Copy code
  1.  void serialEvent (Serial myPort) {
  2.  String inString = myPort.readStringUntil('\n');
  3.  float inByte = float(inString);
problem in this section.
I don't know how to send data from arduino to get as a string in processing.
Need your valuable suggestion.
thanking you in anticipation