Pages: 1 [2]   Go Down
Author Topic: Having trouble dislaying images correctly on Processing.  (Read 1921 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The print(sensorReading) statement is a debug statement. It serves no purpose other than to help you debug the program.

By putting it there, you learned a lot. You learned that the Arduino IS communicating with Processing, and that the Processing application IS receiving what you expect. Although I don't like the idea of just printing raw data like that. In a more complex program, you won't know what raw data corresponds to what print statement.

I once debugged an application that was printing 4,000,000 lines of output before I figured out the problem. Without much more identifying data, I would still be trying to figure that problem out.

OK, so what does draw() look like, now? Please delete all the blank lines before posting. My scroll finger is getting quite a workout today, and is tired.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh right, I understand how the debugging works. It's printing 1 nd 2 in the right order anyway as it's running.


 Ok, so the draw() method looks like this now

Code:
void draw() {
  if (sensorReading == "1") {
     image(img, 0, 0);
     }
 if (sensorReading == "2") {
       image(img2, 0, 0);
     }

and it's not working at all. It just shows a grey screen. sensorReading is printing 1 and 2 in the right order once it gets past NULL. So why isn't the draw working? Keep in mind that it worked earlier when I used (sensorReading != null). This is the part where I know something must be missing. I tried putting \r and \n and a few other things but it wont show the images unless its using (sensorReading != null).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only other thing I can think to do is to add a print statement or two to draw() to see if something is happening to sensorReading after serialEvent ends.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I add print statements to the draw() method like this.

Code:
void draw() {
  if (sensorReading == "1") {
     image(img, 0, 0);
print(sensorReading);
     }
 if (sensorReading == "2") {
       image(img2, 0, 0);
print(sensorReading);
     }
}

it still does nothing. Doesn't even print anything on the IDE.

If I add it into this version

Code:
void draw() {
  if (sensorReading != null) {
     image(img, 0, 0);
print(sensorReading);
     }
 if (sensorReading == null) {
       image(img2, 0, 0);
print(sensorReading);
     }
}

it changes the image the first 3 times and it keeps displaying the sensorReading for each one. The sensorReading continues to change when I turn off and on the light. But the image stays on PLAN2 after the first 3. There should be a way to fix this, I'm going crazy here trying to figure it out!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I might add that with the two print(sensorReading); added in the draw(), when it keeps displaying the sensorReading it puts a space between each one, like a line break and a blank. It looks something like this.

Code:
null
null
null
null
null
null
1
1

1

1

1

1

1

1

2

2

2

2

 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you go back a few replies, you'll see how I asked you to print sensorReadings, in [] so we could see each time serialEvent gets called. You are still not getting as much information as you could.

Try this. Define a global variable, imageToDraw, as an int.

In serialEvent(), add:
if(sensorReading == "1")
  imageToDraw = 1;
else if(sensorReading == "2")
  imageToDraw = 2;

Then, in draw(), make the decision on what to draw based on imageToDraw, rather than sensorReading.

It may be that the sensorReading being blank is a real issue, and that when draw gets nothing to do, it creates the blank screen. It may be that, because draw() is called in a loop, like loop(), the image is not persisted, if sensorReading is no longer "1" or "2".

With the changes I suggest here, it won't matter if sensorReading is not "1" or "2".

It is also possible that sensorReading isn't "1" or "2" but 1 or 2 followed by some non-printing characters. That is why I always print strings in [].
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, should be available()>0;


//Try this in your Processing sketch.

void draw()
{
if(yourPort.available()>0)
{
message=yourPort.read();   //storages the message in variable message.
yourPort.clear();                //Clears the buffer to have it ready to receive the new info from arduino later.
}
if(message==1)                   //you need to send 1 from Arduino to dispaly the image you want {Serial.write(1);}
{
background(255);               //Clears the screen
image(img,0,0);                 //displays image 1.
}
if(message==2)                  //when you send 2 from arduino the other image will display.{Serial.write(2);}
{
background(255);              //Clears the screen again to make sure nothing remains from the previous image if they are different sizes.
image(img2,0,0);              //displays image 2.
}
}
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS the imagetodraw approach didn't work. but when I changed to

print("Serial string: [");
print(sensorReading);
println("]");

I noticed it was printing like this

Serial string: [1
]
Serial string: [2
]

What would that mean? That it is receiving another character from arduino after each number?


@arduinoadrian

I tried that but it just left a grey screen. However, it was printing the sensorReading as null for each light level instead of 1 or 2. Any idea why this might be?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The string that you are reading in has the carriage return on the end. You can compare, in draw() to "1\n" or "2\n".
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've done that and it works fine.

You may have a problem on the Arduino side.
-Make sure all variables are defined properly on the Processing side. Define message int message=0;
-Make sure Arduino is using the Port you defined in Processing. Try changing the Port on both and see if that works.
-Check Serial.begin  defined at the same speed in Ardino and processing.
-If you do Serial.write(1); on Arduino and println("message from arduino="+message); on Processing after reading, you should get
message from arduino= 1 on Processing.
Remember you need to stablish a transit variable on processing to store the message from Arduino and then clear the buffer, otherwise you will loose it and get a null or 0 maybe. The message (1 or 2) will remain in that variable. In this case message.
-If you attemp to read before if(yourPort.available()>0); maybe you get a null because there is nothing in the buffer. Read only after that to make sure there is something in the buffer.
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would not complicate matters that much by sending a string and so on. Just by sending the numbers 1 or 2 it should work fine.

Copy what I told you and modify your processing sketch to wok with it and it will do the job. I've done it and it's working fine as long as Arduino and Processing are using the same Port and speed.
Thanks,
Adrian
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@arduinoadrian It worked!! I tried your method again following th extra steps you posted. All I had to do was change the code on the arduino side from serial.println("1") to serial.write(1) and the same for serial.println("2") to serial.write(2). The images change flawlessly now, over and over again. This was where it was going wrong, it was in the arduino code all along. I'm so happy I have it working!! Thanks for that and PaulS thank you too for teaching me so much today. I can finally relax and do the easy part of making the correct images in illustrator.
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm glad you did it. I went through similar issues when I began with this... Later you wil realize it's not that hard.  Processing is awsome!!!

Serial.print() will write on the Arduino Monitor not on Processing yourPort. Now you know.

Happy it helped you.
Thanks,
Adrian
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Serial.print() will write on the Arduino Monitor not on Processing yourPort.

Eh? Serial.print() writes to the serial port with no knowledge of the far end. It will appear in whatever is attached to the far end of the serial connection, whether that is the serial monitor, Processing, or anything else you might want.

Quote
Now you know.
« Last Edit: February 25, 2012, 03:14:09 am by dxw00d » Logged

Pages: 1 [2]   Go Up
Jump to: