// 3D Environment Laser Scanner (Processing Part1) // By Callan Mackay // Published on 22/02/2016 import processing.video.*; import processing.serial.*; Serial port; // create a serial object called "port" int linefeed = 10; // ASCII character for new line PrintWriter output; // object for writing to the text file Capture video; // video capture object PImage img1; // first image with filtered red colour (thick fuzzy line) PImage img2; // second image which is a filtered version of the first image (thin, dotted line) void setup(){ size(640,960); // render window size, double the size of the webcam image to fit in filtered image too video = new Capture(this, 640, 480); // specifying the webcam resolution video.start(); // begining communication with the webcam port = new Serial(this, "COM3", 9600); // setting up the serial communication with the Arduino, check correct COM port in Arduino - Tools - Port port.clear(); // clear out the serial port before starting output = createWriter("test.txt"); // make the text file } String myString; // String to hold angles from Arduino float motangle = 0; // holds the current motor angle void draw(){ img1 = createImage(640, 480, RGB); // determining image resolution (same as webcam) img2 = createImage(640, 480, RGB); // determining image resolution (same as webcam) background(0); // set black background of render window output.print(";"); // print a semi colon in the text file to indicate the start of a new frame output.print(motangle); // print the motor angle in the text file if(video.available()){ // is there a new frame available from the webcam? video.read(); // if so, then read it! } if (port.available() >= 4){ // is there 4 or more bytes in the serial buffer from the Arduino myString = port.readStringUntil(linefeed); // if so, then read it until there is a new line and save it to "mystring" as a string if(myString !=null){ // if it succesfully read something motangle = float(myString); // angle converted from a string to a float and saved to "motangle" } } for (int i = 0; i < width*height/2; i++){ // loop to run through all of the webcam pixels if(red(video.pixels[i]) > 50){ // is the RED value in a pixel is MORE than this value (out of 255) img1.pixels[i] = color(255); // then place a white pixel in image 1 } } for (int i = 0; i < width*height/2; i++){ // run through all the pixels in image 1 int k = 0; // reset "k" to zero while(brightness(img1.pixels[i]) == 255 && i < (width*height/2)-1 && (i/width)%5 == 0){ // stay in this loop IF: there is a white pixel in image 1 //AND if it's not run off the edge of the image //AND only if it's on every 5th row k++; // increase "k" by 1. This counts the number of white pixels in a consecutive row i++; // increase "i" by 1. moves along to next pixel } if(k > 0 && !(i-(k/2) == 195201)){ // check if "k" found any white pixels AND ignore a specific dead pixel (you can delete this bit) img2.pixels[i-(k/2)] = color(255); // draw a white pixel in image 2 at the middle of a row of white pixels from image 1 output.print(","); // print a comma to the text file to seperate from motor angle (already printed) output.print(i%640); // print the columb the new white pixel is on to the text file (X-coordinate) output.print(","); // print a comma to the text file to seperate the coordinates output.print(i/640); // print the row the new white pixel is on to the text file (Y-coordinate) } } image(video,0,0); // show the webcam feed in the top of the render window image(img2,0,480); // show image 2 in the bottom of the render window println(motangle); // prints the current motor angle to the command window if (motangle > 450.0){ // if the motor angle from the Arduino is more than 450 deg, i.e. 500 deg, then stop the program output.flush(); // finish printing to the text file output.close(); // close the text file exit(); // stop this program from running } } void keyPressed(){ // make this program able to stop with any key output.flush(); // finish printing to the text file output.close(); // close the text file exit(); // stop this program from running }