Arduino+processing Image transfer

edited April 2014 in Arduino

Hi all,

I found some codes online to use the arduino uno and processing to transfer one image through the Arduino serial port (TX and RX). I need to transfer 6 images. How can i modify the processing code or arduino code to be able to transfer all images one after the other. Example: transfer image 1, then 2 etc.

Below is the Arduinocode:

     #include <SD.h>   

     File photoFile;   



     void setup(){   

      Serial.begin(115200);   



      //Serial.println("initializing sd card");   
      pinMode(10,OUTPUT);     // CS pin of SD Card Shield   

      if (!SD.begin(10)) {   
       Serial.print("sd initialzation failed");   
       return;   
      }   

     }   


     void loop(){   

      while(1){   

       Serial.flush();     




         File photoFile = SD.open("IMAGE09.jpg");   

         if (photoFile) {   
          while (photoFile.position() < photoFile.size()) {   


           Serial.write(photoFile.read());   
          }   

          photoFile.close();   

         }    

         else {   
          Serial.println("error sending photo");   
         }         
        }   

       }   
      }   

PROCESSING CODE: // READ: Upload arduino code then processing.File //will be created in processiong library under my documets // //image will begin to transfer. . Click on //the grey processing box AFTER complete imgae size // has been reached... and press any button. //Image should have transferred.Click stop.

import processing.serial.*;  

Serial myPort;

OutputStream output;  
//OutputStream output2;

void setup() 
{  

  size(320, 240);  

  //println( Serial.list() );  
  myPort = new Serial( this, Serial.list()[0], 115200);  
  myPort.clear();  
  output = createOutput("IMAGE09.jpg");

}  


void draw() 
{  

  try {   
    while ( myPort.available () > 0 ) 
    {  
      output.write(myPort.read());
    }
  }   
  catch (IOException e) {  
    e.printStackTrace();
  }
}  


void keyPressed()
{  
  try 
  {   
    output.flush(); // Writes the remaining data to the file 
    output.close();  // Finishes the file 
  }   

  catch (IOException e)
  {  
    e.printStackTrace();
  }
}    

Answers

  • Create multiple output instance such as output1,output2, output3 so on and write them one by one on the port.

    That's all

  • I do that for both the arduino and processing codes, correct?

  • hey.

    you could check when image1 is done sending. then send a small tag or identifier that image2 is going to start sending, then start sending image2, do this for all the other images you want to send.

    int IndexFile = 0;
    String locationImage[]; // path of the images;
    

    in the loop()

    File photoFile = SD.open(locationImage[indexFile]);   
    
    if (photoFile) {  
       Serial.write(indexFile); 
       delay(100);
       while (photoFile.position() < photoFile.size()) {   
           Serial.write(photoFile.read());   
        }   
         photoFile.close();   
         indexFile++;    
     }   
     else {   
        Serial.println("error photo "+ locationImage[indexFile]+" with index "+indexFile);   
    }         
    

    In processing check when you receiving image1. image0

    thomas

  • edited April 2014

    Arduino cannnot read or write multple data simultaneously (it reads or write in sequnce ) so there is no point creating multiple object in Arduino but it can read the data that are coming in the sequence(as @thomaslengeling said) so create data with id and tags so that you can identify them while reading inside the processing and seperearte the data.

  • Hi thomas, i am still a programming noob.

    String locationImage[]; // path of the images; I don't understand this part. Example, lets say the image i want to transfer is named Image1. How will the arduino know to get that image?

    Also for : %0

  • Hi thomas, i am still a programming noob.

    String locationImage[]; // path of the images; I don't understand this part. Example, lets say the image i want to transfer is named Image1. How will the arduino know to get that image?

    Also for : File photoFile = SD.open(locationImage[indexFile]);

    Is locationImage, the name of my image (image 1)? Would it be like this?:

    File photoFile = SD.open("IMAGE1.jpg");

  • Hi thomas, i am still a programming noob.

    String locationImage[]; // path of the images; I don't understand this part. Example, lets say the image i want to transfer is named Image1. How will the arduino know to get that image?

    Also for : File photoFile = SD.open(locationImage[indexFile]);

    Is locationImage, the name of my image (image 1)? Would it be like this?:

    File photoFile = SD.open("IMAGE1.jpg");

  • woah don't know how that happened

  • This is a really great starting point for me. Thanks for sharing this code. I am having issues tho and I know it is something very trivial. I was hoping since you have been doing this, you guys might know.

    My error is ******** java.io.IOException: Stream Closed at java.io.FileOutputStream.write(Native Method) at java.io.FileOutputStream.write(Unknown Source) at ImageProcessingTry2.draw(ImageProcessingTry2.java:45) at processing.core.PApplet.handleDraw(PApplet.java:2386) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240) at processing.core.PApplet.run(PApplet.java:2256) at java.lang.Thread.run(Unknown Source)

    So why does it say Stream Closed? is this on the processing end? or on my arduino end? maybe I have not chosen the right port? I am trying to use COM7 port... The choosing of teh com port is a little fuzzy still for me.

Sign In or Register to comment.