Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* sends shades in single waves, using an int, to keep track of total steps made per row (x axis).
- As per "Jose" sketch, but with arduino section included
- Arduino section is included at the bottom within comment tags
- */
- import processing.serial.* ;
- Serial myPort;
- PImage img1;
- int Ysize = 6; // height of the rows
- int squareSize = 900; // size of the working frame**********************
- float greyscalein; // inward greyscale reading of 0- 255
- byte greyscaleround; // converted and rounded down to whole bytes for transmission (0 - 9)
- int xprogress = 0; // integer for keeping track of progress across image
- int stepsize = 0; // integer for transmitting how far to move the motors per step
- int y = 0;
- void setup() {
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[0], 9600);
- println("Starting up!");
- 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
- println("Waiting for arduino");
- long startWaiting = millis();
- while (myPort.read ()!= 6) {
- delay(50);
- print('.');
- } //wait for the arduino to start sending 42 after reset, the delay is there so we don't use 100% CPU
- println("\nGot a message from the arduino after "+(millis()-startWaiting) + " milliseconds");
- myPort.write(6); //send something to the arduino so it knows that communication has been established
- myPort.clear(); //clear the buffer just in case there was more that one byte in the buffer
- delay(300); //wait for the arduino to get our message and get to the echo loop
- size(squareSize * 2, squareSize); // frame is twice image width. Original image on the left, processed image on the right.
- }
- void waitUntil(Serial port, int byteValue) {
- if (byteValue>255) throw new IllegalArgumentException("byte value out of range 0-255");
- int lastValue = -1;
- while (lastValue!=byteValue) {
- if (port.available()>0) {
- lastValue=port.read();
- }
- delay(5);// I added this delay so the sketch doesn't use 100% CPU but it's not essential
- }
- }
- void draw() {
- background(0); // background - 0 is black
- img1 = loadImage("photo3.jpg"); // image, 300 x 300 (STRS or grad)*****************
- imageMode(CENTER);
- image(img1, (squareSize/2), (squareSize/2)); // draws the image, centred
- for (int y = 0; y < squareSize; y = y + Ysize ) {
- for (int x = 0; x < squareSize; x = x + stepsize) {
- color c = get(xprogress, y + Ysize/2);
- float greyscalein = brightness(c); // Sets 'greyscale' to 255-0
- 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
- greyscalein = constrain(greyscalein, 49, 57);
- int greyscaleround = round(greyscalein); // rounds the floating value of the greyscalein(read from c) to a whole number (greyscaleround)
- stepsize = ((greyscaleround - 48));// rounds the value to nearest integer
- if (stepsize == 9) {
- xprogress = xprogress + 1;
- }
- else if (stepsize < 9)
- {
- xprogress = xprogress + (stepsize*2); // adjusts stepsize
- }
- x = xprogress; // clarifies purely for the serial monitor output
- myPort.write(greyscaleround);
- println("Sending stepsize: " + stepsize );
- waitUntil(myPort, 6);
- println("Received confirmation for "+ stepsize);
- print("X "+ x);
- println(": Y "+ (y+(Ysize/2)));
- }
- myPort.write(13);
- println("Back");
- waitUntil(myPort, 6);
- println("Received confirmation for Back");
- y = y+Ysize;
- for (int x = squareSize; x > 0; x = x - stepsize) {
- color c = get(xprogress, y + Ysize/2);
- float greyscalein = brightness(c); // Sets 'greyscale' to 255-0
- 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
- greyscalein = constrain(greyscalein, 49, 57);
- int greyscaleround = round(greyscalein); // rounds the floating value of the greyscalein(read from c) to a whole number (greyscaleround)
- stepsize = ((greyscaleround - 48));// rounds the value to nearest integer
- if (stepsize == 9) {
- xprogress = xprogress - 1;
- }
- else if (stepsize < 9)
- {
- xprogress = xprogress - (stepsize*2); // adjusts stepsize
- }
- x = xprogress; // clarifies purely for the serial monitor output
- myPort.write(greyscaleround);
- println("Sending stepsize: " + stepsize );
- waitUntil(myPort, 6);
- println("Received confirmation for "+ stepsize);
- print("X "+ x);
- println(": Y "+ (y+(Ysize/2)));
- }
- myPort.write(10);
- println("Forward");
- waitUntil(myPort, 6);
- println("Received confirmation for Forward");
- if (y >= squareSize-(Ysize)) { // "end of image command". this would be the place to put a signature perhaps?
- myPort.write(4);
- println("End, press to restart");
- waitUntil(myPort, 6);
- println("Received confirmation for End");
- }
- }
- }
- /* Arduino Section **********************************************
- #include <AFMotor.h>
- AF_Stepper motorL(200, 1); // Left Motor, M1 & M2
- AF_Stepper motorR(200, 2); // Right Motor, M3 & M4
- // Forward is Up on both motors.
- const int button = 2; // button holds the sketch in setup, until pressed.
- const int LED = 14; // LED indicates end of sketch
- const int pixelsize = 6; // size of a row height, in motor steps EVEN numbers ONLY (10 works well)
- const int multiplier = 1; // adjust the multiplier to scale the image; 3 works well on a 300 pix image
- int incomingByte = 0;
- int x = 0;
- int y = 0;
- int currstep = 0; // curr pixel shade 0 - 255?
- int direc = 10; // defines direction
- void setup() {
- pinMode (LED, OUTPUT);
- pinMode (button, INPUT);
- motorL.setSpeed(30); // 10 rpm
- motorR.setSpeed(30); // 10 rpm
- Serial.begin(9600); // Serial at 9600 bps
- while (digitalRead (button) == HIGH){
- digitalWrite (LED,HIGH); // indicator LED
- // stops script. Its waiting for a button press (LOW on "button")
- }
- digitalWrite (LED,LOW); // indicator LED
- motorR.step(300*multiplier, BACKWARD, SINGLE); //steps from centre, to starting corner
- motorL.step(300*multiplier, BACKWARD, SINGLE); //steps from centre, to starting corner
- while (digitalRead (button) == HIGH){
- digitalWrite (LED,HIGH); // indicator LED
- // stops script. Its waiting for a button press (LOW on "button")
- }
- digitalWrite (LED,LOW); // indicator LED
- while (Serial.available() <= 0) {//keep sending until we get some bytes from the pc
- Serial.write(6); // just a constant
- delay(100);
- }
- 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
- }
- void loop() {
- // if we get a valid byte, read analog ins:
- if (Serial.available()>0) {
- incomingByte=Serial.read();
- if (incomingByte == 4) { // end sketch
- motorL.step(x, BACKWARD, SINGLE); //steps up full pixel
- motorR.step(y, BACKWARD, SINGLE); //steps up full pixel
- x = 0;
- y = 0;
- digitalWrite (LED,HIGH); // indicator LED
- while (digitalRead (button) == HIGH){
- // stops script. Its waiting for a button press (LOW on "button")
- }
- Serial.write(6);//respond
- }
- else if (incomingByte == 10) { // Forward (LF)
- motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE); //steps up full pixel
- direc = 10;
- x = 0;
- y = y + pixelsize;
- Serial.write(6);//respond
- }
- else if (incomingByte == 13) { // Backward (CR)
- motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE); //steps up full pixel
- direc = 13;
- y = y + pixelsize;
- Serial.write(6);//respond
- }
- else {
- if (direc == 10){ // forward row
- currstep = map(incomingByte, 49, 57, 1 , 9); // map the shade byte between 1 and 9 (1 is black)
- if (currstep >= 9) {
- motorL.step((1*multiplier),FORWARD, SINGLE);
- x = (x + 1);
- }
- else if (currstep < 9) {
- motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // from centre steps down half a pixel
- motorL.step((((currstep))*multiplier), FORWARD, SINGLE); // appropriate shade step
- motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE); //steps back up full pixel
- motorL.step((((currstep))*multiplier), FORWARD, SINGLE);// appropriate shade step
- motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // steps down half pixel
- x = (x + (currstep * 2));
- }
- Serial.write(6);//respond
- }
- if (direc == 13){ // backward row
- currstep = map(incomingByte, 49, 57, 1 , 9); // map the shade byte between 1 and 9 (1 is black)
- if (currstep >= 9) {
- motorL.step((1*multiplier),BACKWARD, SINGLE);
- x = (x - 1);
- }
- else if (currstep < 9) {
- motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // from centre steps down half a pixel
- motorL.step((((currstep))*multiplier), BACKWARD, SINGLE); // appropriate shade step
- motorR.step(((pixelsize)*multiplier), FORWARD, SINGLE); //steps back up full pixel
- motorL.step((((currstep))*multiplier), BACKWARD, SINGLE);// appropriate shade step
- motorR.step(((pixelsize/2)*multiplier), BACKWARD, SINGLE); // steps down half pixel
- x = (x - (currstep * 2));
- }
- Serial.write(6);//respond
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement