Advertisement
OllyR

Black and white, FM polargraph

Apr 21st, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.86 KB | None | 0 0
  1. /* sends shades in single waves, using an int, to keep track of total steps made per row (x axis).
  2. As per "Jose" sketch, but with arduino section included
  3. Arduino section is included at the bottom within comment tags
  4.  */
  5.  
  6.  
  7. import processing.serial.* ;
  8. Serial myPort;
  9. PImage img1;
  10.  
  11. int Ysize = 6; // height of the rows
  12. int squareSize = 900; // size of the working frame**********************
  13. float greyscalein; // inward greyscale reading of 0- 255
  14. byte greyscaleround; // converted and rounded down to whole bytes for transmission (0 - 9)
  15. int xprogress = 0; // integer for keeping track of progress across image
  16. int stepsize = 0; // integer for transmitting how far to move the motors per step
  17. int y = 0;
  18.  
  19. void setup() {
  20.   println(Serial.list());
  21.   myPort = new Serial(this, Serial.list()[0], 9600);
  22.  
  23.  
  24.   println("Starting up!");
  25.   myPort.clear(); //this is necessary because the arduino keeps sending 42 even before we connect, when we connect the arduino gets reset but since we still see some 42s in our buffer we send it characters to echo too soon (before it reboots) and we will wait forever for it to reply
  26.   println("Waiting for arduino");
  27.   long startWaiting = millis();
  28.   while (myPort.read ()!= 6) {
  29.     delay(50);
  30.     print('.');
  31.   } //wait for the arduino to start sending 42 after reset, the delay is there so we don't use 100% CPU
  32.   println("\nGot a message from the arduino after "+(millis()-startWaiting) + " milliseconds");
  33.   myPort.write(6); //send something to the arduino so it knows that communication has been established
  34.   myPort.clear(); //clear the buffer just in case there was more that one byte in the buffer
  35.   delay(300); //wait for the arduino to get our message and get to the echo loop
  36.  
  37.   size(squareSize * 2, squareSize); // frame is twice image width. Original image on the left, processed image on the right.
  38. }
  39.  
  40.  
  41. void waitUntil(Serial port, int byteValue) {
  42.  
  43.   if (byteValue>255) throw new IllegalArgumentException("byte value out of range 0-255");
  44.  
  45.   int lastValue = -1;
  46.  
  47.   while (lastValue!=byteValue) {
  48.     if (port.available()>0) {
  49.       lastValue=port.read();
  50.     }
  51.     delay(5);// I added this delay so the sketch doesn't use 100% CPU but it's not essential
  52.   }
  53. }
  54.  
  55. void draw() {
  56.  
  57.   background(0); // background - 0 is black
  58.   img1 = loadImage("photo3.jpg"); // image, 300 x 300 (STRS or grad)*****************
  59.   imageMode(CENTER);
  60.   image(img1, (squareSize/2), (squareSize/2)); // draws the image, centred
  61.  
  62.   for (int y = 0; y < squareSize; y = y + Ysize ) {
  63.  
  64.     for (int x = 0; x < squareSize; x = x + stepsize) {
  65.  
  66.  
  67.  
  68.  
  69.       color c = get(xprogress, y + Ysize/2);
  70.       float greyscalein = brightness(c);  // Sets 'greyscale' to 255-0
  71.  
  72.       greyscalein = map(greyscalein, 50, 220, 49, 57); // maps the resolution of the transmited data from 0-255, to 1- 9 ASCII *** white limit and black limit probably need adjusting
  73.       greyscalein = constrain(greyscalein, 49, 57);
  74.  
  75.       int greyscaleround = round(greyscalein); // rounds the floating value of the greyscalein(read from c) to a whole number (greyscaleround)
  76.  
  77.       stepsize = ((greyscaleround - 48));// rounds the value to nearest integer
  78.  
  79.       if (stepsize == 9) {
  80.         xprogress = xprogress + 1;
  81.       }
  82.       else if (stepsize < 9)
  83.       {
  84.         xprogress =  xprogress + (stepsize*2); // adjusts stepsize
  85.       }
  86.       x = xprogress; // clarifies  purely for the serial monitor output
  87.  
  88.       myPort.write(greyscaleround);
  89.       println("Sending stepsize: " + stepsize );
  90.       waitUntil(myPort, 6);
  91.       println("Received confirmation for "+ stepsize);
  92.       print("X "+ x);
  93.       println(": Y "+ (y+(Ysize/2)));
  94.     }
  95.  
  96.     myPort.write(13);
  97.     println("Back");
  98.     waitUntil(myPort, 6);
  99.     println("Received confirmation for Back");
  100.     y = y+Ysize;
  101.  
  102.  
  103.     for (int x = squareSize; x > 0; x = x - stepsize) {
  104.  
  105.       color c = get(xprogress, y + Ysize/2);
  106.       float greyscalein = brightness(c);  // Sets 'greyscale' to 255-0
  107.  
  108.       greyscalein = map(greyscalein, 50, 220, 49, 57); // maps the resolution of the transmited data from 0-255, to 1- 9 ASCII *** white limit and black limit probably need adjusting
  109.       greyscalein = constrain(greyscalein, 49, 57);
  110.  
  111.       int greyscaleround = round(greyscalein); // rounds the floating value of the greyscalein(read from c) to a whole number (greyscaleround)
  112.  
  113.       stepsize = ((greyscaleround - 48));// rounds the value to nearest integer
  114.  
  115.       if (stepsize == 9) {
  116.         xprogress = xprogress - 1;
  117.       }
  118.       else if (stepsize < 9)
  119.       {
  120.         xprogress =  xprogress - (stepsize*2); // adjusts stepsize
  121.       }
  122.       x = xprogress; // clarifies  purely for the serial monitor output
  123.  
  124.       myPort.write(greyscaleround);
  125.       println("Sending stepsize: " + stepsize );
  126.       waitUntil(myPort, 6);
  127.       println("Received confirmation for "+ stepsize);
  128.       print("X "+ x);
  129.       println(": Y "+ (y+(Ysize/2)));
  130.     }
  131.  
  132.     myPort.write(10);
  133.     println("Forward");
  134.     waitUntil(myPort, 6);
  135.     println("Received confirmation for Forward");
  136.  
  137.     if (y >= squareSize-(Ysize)) { // "end of image command". this would be the place to put a signature perhaps?
  138.       myPort.write(4);
  139.       println("End, press to restart");
  140.       waitUntil(myPort, 6);
  141.       println("Received confirmation for End");
  142.     }
  143.   }
  144. }
  145.  
  146.  
  147.  
  148. /* Arduino Section **********************************************
  149.  
  150. #include <AFMotor.h>
  151.  
  152. AF_Stepper motorL(200, 1);  // Left Motor, M1 & M2
  153. AF_Stepper motorR(200, 2);  // Right Motor, M3 & M4
  154. // Forward is Up on both motors.
  155.  
  156. const int button = 2; // button holds the sketch in setup, until pressed.
  157. const int LED = 14; // LED indicates end of sketch
  158.  
  159. const int pixelsize = 6; // size of a row height, in motor steps EVEN numbers ONLY (10 works well)
  160. const int multiplier = 1; // adjust the multiplier to scale the image; 3 works well on a 300 pix image
  161.  
  162. int incomingByte = 0;
  163. int x = 0;
  164. int y  = 0;
  165. int currstep = 0; // curr pixel shade 0 - 255?
  166. int direc = 10; // defines direction
  167.  
  168. void setup() {
  169.  
  170.   pinMode (LED, OUTPUT);
  171.   pinMode (button, INPUT);
  172.  
  173.   motorL.setSpeed(30);  // 10 rpm  
  174.   motorR.setSpeed(30);  // 10 rpm  
  175.  
  176.   Serial.begin(9600);  // Serial at 9600 bps
  177.  
  178.   while (digitalRead (button) == HIGH){
  179.     digitalWrite (LED,HIGH); // indicator LED
  180.     // stops script. Its waiting for a button press (LOW on "button")
  181.   }
  182.   digitalWrite (LED,LOW); // indicator LED
  183.  
  184.   motorR.step(300*multiplier, BACKWARD, SINGLE); //steps from centre, to starting corner
  185.   motorL.step(300*multiplier, BACKWARD, SINGLE); //steps from centre, to starting corner
  186.  
  187.   while (digitalRead (button) == HIGH){
  188.     digitalWrite (LED,HIGH); // indicator LED
  189.     // stops script. Its waiting for a button press (LOW on "button")
  190.   }
  191.  
  192.   digitalWrite (LED,LOW); // indicator LED
  193.   while (Serial.available() <= 0) {//keep sending until we get some bytes from the pc
  194.     Serial.write(6);   // just a constant
  195.     delay(100);
  196.   }
  197.   while(Serial.available()>0) Serial.read(); //immediately after we get something from the PC clear the input buffer by reading from it until it's empty
  198. }
  199.  
  200.  
  201. void loop() {
  202.  
  203.   // if we get a valid byte, read analog ins:
  204.   if (Serial.available()>0) {
  205.     incomingByte=Serial.read();
  206.  
  207.     if (incomingByte == 4) { // end sketch
  208.  
  209.  
  210.       motorL.step(x, BACKWARD, SINGLE);  //steps up full pixel    
  211.       motorR.step(y, BACKWARD, SINGLE);  //steps up full pixel          
  212.       x = 0;      
  213.       y = 0;      
  214.  
  215.       digitalWrite (LED,HIGH); // indicator LED
  216.       while (digitalRead (button) == HIGH){
  217.         // stops script. Its waiting for a button press (LOW on "button")
  218.       }
  219.       Serial.write(6);//respond
  220.     }
  221.  
  222.     else if (incomingByte == 10) { // Forward (LF)
  223.       motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE);  //steps up full pixel
  224.       direc = 10;
  225.       x = 0;
  226.       y = y + pixelsize;
  227.       Serial.write(6);//respond
  228.     }
  229.     else if (incomingByte == 13) { // Backward (CR)
  230.       motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE);  //steps up full pixel
  231.       direc = 13;
  232.       y = y + pixelsize;
  233.       Serial.write(6);//respond
  234.     }
  235.  
  236.     else    {
  237.  
  238.       if (direc == 10){ // forward row
  239.  
  240.         currstep = map(incomingByte, 49, 57, 1 , 9); // map the shade byte between 1 and 9 (1 is black)
  241.  
  242.         if (currstep >= 9) {
  243.           motorL.step((1*multiplier),FORWARD, SINGLE);
  244.           x = (x + 1);
  245.         }
  246.  
  247.         else if (currstep < 9) {
  248.           motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // from centre steps down half a pixel
  249.           motorL.step((((currstep))*multiplier), FORWARD, SINGLE); // appropriate shade step
  250.           motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE);  //steps back up full pixel
  251.           motorL.step((((currstep))*multiplier), FORWARD, SINGLE);// appropriate shade step
  252.           motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // steps down half pixel
  253.           x = (x + (currstep * 2));
  254.  
  255.         }
  256.         Serial.write(6);//respond
  257.       }
  258.  
  259.  
  260.       if (direc == 13){ // backward row
  261.         currstep = map(incomingByte, 49, 57, 1 , 9); // map the shade byte between 1 and 9 (1 is black)
  262.  
  263.         if (currstep >= 9) {
  264.           motorL.step((1*multiplier),BACKWARD, SINGLE);
  265.           x = (x - 1);
  266.         }
  267.  
  268.         else if (currstep < 9) {
  269.           motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // from centre steps down half a pixel
  270.           motorL.step((((currstep))*multiplier), BACKWARD, SINGLE); // appropriate shade step
  271.           motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE);  //steps back up full pixel
  272.           motorL.step((((currstep))*multiplier), BACKWARD, SINGLE);// appropriate shade step
  273.           motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // steps down half pixel
  274.           x = (x - (currstep * 2));
  275.         }
  276.         Serial.write(6);//respond
  277.       }
  278.     }
  279.   }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement