import processing.serial.*; // The keyword import is used to load a library into a Processing sketch. Serial serial; PImage img; String file_path; Boolean file_choosed = false; //String command_line; //#whenYouDontKnowHowToNameTheVariable boolean there_will_be_a_dot_in_this_line; boolean there_is_a_dot = false; int last_line_steps = 0; //int delay_this_line = 0; boolean pressed = false; // checkes whether mouse button pressed String portName; void setup() { size(400, 210); background(255,255); portName = Serial.list()[0]; serial = new Serial(this, portName, 9600); } void draw() { fill(0,0,255); rect(-1,-1, 401, 211); //open file button fill(255); rect(10,10, 140, 30); fill(0); text("OPEN IMAGE", 40,30); //stop fill(255); rect(10,90, 140, 30); fill(0); text("HOME", 63,110); fill(255); fill(0); text("MADE BY NIKODEM BARTNIK & EDITED BY SAFAR POURABBAS \n 08.2020", 10,185); text("THIS PROJECT IS CALLED SALMAN DOTER \n GOOGLE IT TO FIND MORE INFO", 10, 150); if(mouseButton == LEFT){ if(mouseX > 10 && mouseX < 10 + 140 && mouseY > 10 && mouseY < 10 + 30 && pressed == false){ if(serial != null){ selectInput("Select a file to process:", "fileSelected"); pressed = true; }else{ print("Firstly connect to serial port"); } } } if(file_choosed){ image(img, 0, 0); img.loadPixels(); println("Width: " + img.width); println("Height: " + img.height); //---------------------------------------------------------------------------------------------------------------- // If there is any pixel to print this part will take action! for (int y = 0; y < img.height; y++) { there_is_a_dot = false; for (int x_temp = 0; x_temp < img.width; x_temp++) { if (img.pixels[x_temp + y * img.width] != color(255,255,255)){ there_is_a_dot = true; } } if(there_is_a_dot){ there_will_be_a_dot_in_this_line = true; for (int x = 0; x < img.width; x++) { if(there_will_be_a_dot_in_this_line){ if (img.pixels[x + y*img.width] == color(255,255,255)) { serial.write("0#"); print("0"); last_line_steps++; delay(500); } else if (img.pixels[x + y*img.width] == color(0,0,255)) { // if pixel is BLUE --> pen 1 print("1"); serial.write("1#"); last_line_steps++; delay(1000); } else if (img.pixels[x + y*img.width] == color(255,0,0)) { // if pixel is RED --> pen 2 print("2"); serial.write("2#"); last_line_steps++; delay(1000); } else if (img.pixels[x + y*img.width] == color(0,255,0)) { // if pixel is GREEN --> pen 3 print("3"); serial.write("3#"); last_line_steps++; delay(1000); } else if (img.pixels[x + y*img.width] == color(0,0,0)) { // if pixel is BLACK --> pen 4 print("4"); serial.write("4#"); last_line_steps++; delay(1000); } // if there is another pixel in the line --> 'there_will_be_a_dot_in_this_line' will be set to true // if not the line will be ended and new line will begin there_will_be_a_dot_in_this_line = false; for (int xyz = x+1; xyz < img.width; xyz++) { if (img.pixels[xyz + y*img.width] != color(255,255,255)) { there_will_be_a_dot_in_this_line = true; } } } } } println(""); // causes processing to show digits in new line in its console! serial.write(";#"); delay(last_line_steps * 300); // delay_this_line = 0; last_line_steps = 0; } file_choosed = false; } } //--------------------------------------------------------------------------------------------------- void fileSelected(File selection) { if (selection == null) { println("Window was closed or the user hit cancel."); file_path = ""; } else { println("User selected " + selection.getAbsolutePath()); file_path = selection.getAbsolutePath().toString(); img = loadImage(file_path); file_choosed = true; } } void mouseReleased() { pressed = false; }