Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Cant get my processing sketch to wait for a response from the arduino

$
0
0
The code below, does exactly as i want in terms of processing the image, and converting it into a stream of values.
however i want it to
1) wait for an initial "ready" command from the ardunio
2) wait for a "ready" command from the arduino in between each and every number transmission.

the arduino is processing the numbers physically, and therefore takes time.

I have used "300" as my ready command because the sketch is dealing in shades between 0 and 255. I figure it it one less thing to worry about if i dont start chucking ascii in there too.
My "commands" can be 300, 301, 302 etc.

I THINK i have narrowed down the lines which need attention to lines 17 and 33. I pressume i need a "serial Event" void in there? but i am dont know what to do with it. All the tutorials i have read and watched (of which there are many) do different things with it, none of which i understand properly. I presume what is required here depends on the context?

Many thanks

Ol

  1. import processing.serial.* ;
  2. Serial myPort;
  3. int ready = 300; // greyscale is 0-255. decided to use 300+ as commands, to prevent from mixing ascii with numbers.
  4. PImage img1;
  5. int cellSize = 10; // size of cells into which the image is split, larger size = less cells
  6. int squareSize = 300; // size of the working frame
  7. float greyscale;
  8. void setup() {
  9.   size(squareSize, squareSize);
  10.   println(Serial.list());
  11.   String portName = Serial.list()[0];
  12.   myPort = new Serial(this, portName, 9600);
  13.   print("Port: ");
  14.   println(myPort);
  15.   myPort.clear();
  16.   myPort.read();
  17.   myPort.bufferUntil(ready); // ********this is SUPPOSED to wait for a "ready" signal from the arduino (sent on button press) It doesn’t, obviosuy
  18. }
  19. void draw() {
  20.   background(0);
  21.   img1 = loadImage("grad.jpg"); // image, 300 x 300
  22.   imageMode(CENTER);
  23.   image(img1, (squareSize/2), (squareSize/2)); // draws the image
  24.   for (int y = 0; y < squareSize; y = y + cellSize ) {
  25.     // "carrige return" instruction from processing to the arduino will go in here (probably)
  26.     for (int x = 0; x < squareSize; x = x + cellSize) {
  27.       color c = get(x + cellSize/2, y + cellSize/2);
  28.       float greyscale = brightness(c);  // Sets 'greyscale' to 255-0
  29.       println(greyscale); //show the greyscale
  30.       myPort.write(c);    // send it to the arduino
  31.       myPort.clear();     // clear the port
  32.       delay (300);        // delay for easy reading
  33.       myPort.bufferUntil(ready); // ***** this is SUPPOSED to WAIT until the arduino replies that it is ready.
  34.     }
  35.   }
  36. }
  37. void serialEvent (Serial myPort) {
  38.   myPort.read(); // ******* Not really sure what is needed here., or if serial Event is needed at all?
  39. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles