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

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

$
0
0
Ok so to clarify.
The data produced by the sketch is used to move a plotter. they way i am building it, the processing sketch needs to send ONE number (ascii between 0 and 9) , wait for the arduino to finish physically moving, before sending the next single number.
I am intending to use ASCII commands, therefore between each movement i was thinking of using "ACK" and for the carrige returns, "CR"

here is an example image:, the darker blocks will be represented by a 9, the white ones, by a 0.


The code works, the serial connection works, however the processing sketch and the arduino do not acknowledge each other. I would like them to take it in turns to do what they need to do.

I have tried many various examples and tutorials, however none of them have worked in this scenario.
maybe i am not using the ASCII bytes correctly It doesnt help that ALL the tutorialsi have found work the other way around, with the arduino waiting for processing. Therefore, in frustration i have stripped out all the references to "bufferUntil"

here is the code, without any attempt at the serial response. Any suggestions would be greatly appreciated.

http://pastebin.com/f6x7QuQj

  1. /* sends shades in single waves, using an int, to keep track of total steps made per row (x axis).
  2.  */
  3. import processing.serial.* ;
  4. Serial myPort;
  5. PImage img1;
  6. int Ysize = 10; // height of the rows
  7. int squareSize = 300; // size of the working frame
  8. float greyscalein; // inward greyscale reading of 0- 255
  9. byte greyscaleround; // converted and rounded down to whole bytes for transmission (0 - 9)
  10. int xprogress = 0; // integer for keeping track of progress across image
  11. int stepsize = 0; // integer for transmitting how far to move the motors per step
  12. void setup() {
  13.   size(squareSize * 2, squareSize); // frame is twice image width. Original image on the left, processed image on the right.
  14.   println(Serial.list());
  15.   myPort = new Serial(this, Serial.list()[0], 9600);
  16. }
  17. void draw() {
  18.   background(0); // background - 0 is black
  19.   img1 = loadImage("STRS.jpg"); // image, 300 x 300 (STRS or grad)
  20.   imageMode(CENTER);
  21.   image(img1, (squareSize/2), (squareSize/2)); // draws the image, centred
  22.   for (int y = 0; y < squareSize; y = y + Ysize ) {
  23.     // "carrige return" instruction from processing to the arduino will go in here (probably)
  24.     for (int x = 0; x < squareSize; x = x + stepsize) {
  25.       color c = get(xprogress, y + Ysize/2);
  26.       float greyscalein = brightness(c);  // Sets 'greyscale' to 255-0
  27.       greyscalein = map(greyscalein, 0, 255, 48, 57); // maps the resolution of the trsnamisted data from 0-255, to 0- ???
  28.       int greyscaleround = round(greyscalein); // rounds the floating value of the greyscalein(read from c) to a whole number (greyscaleround)
  29.       print("Greyscale:");
  30.       print(greyscaleround-48); //show the greyscale, adjusted from ascii so that 0 is black
  31.       myPort.write(greyscaleround);    // send it to the arduino
  32.       delay (0);        // delay for easy reading of monitor
  33.       // *** this section is for printing of the preview in the processing window.It is not required by the arduino.***
  34.       stepsize = ((greyscaleround - 46)*1);
  35.       fill(c);
  36.       stroke(100); // bounded by a box, dark grey
  37.       rect(squareSize + xprogress, y, squareSize + xprogress + stepsize, y + Ysize);
  38.       xprogress =  xprogress + stepsize;
  39.       print(" ; stepsize:");
  40.       println(stepsize); //show the size of the step taken on the monitor
  41.       delay (00);
  42.     }
  43.     xprogress = 0; // reset x-progress before going to next line (y axis)
  44.   }
  45. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles