Pages: 1 2 3 [4]   Go Down
Author Topic: C328 camera  (Read 7377 times)
0 Members and 1 Guest are viewing this topic.
Louisville, KY
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Viktar,
    Here is the code I used. The Arduino code is loaded then I close the IDE and run the Processing code.
I am using the library downloaded from http://arms22.googlecode.com/files/CameraC328R.zip

EDIT: I modified the library changing CameraC328R to C328R so I could also use the original UART version
Arduiino code
Code:
#include <C328R.h>
#include <NewSoftSerial.h>
#define LED_PIN 13
#define PAGE_SIZE 64
#define USB_BAUD 115200
#define CAMERA_BAUD 14400
NewSoftSerial mySerial(2, 3);
C328R camera(&mySerial);
uint16_t pictureSizeCount = 0;

/*
 This callback is called EVERY time a JPEG data packet is received.
*/
void getJPEGPicture_callback( uint16_t pictureSize, uint16_t packageSize, uint16_t packageCount, byte* package ){
  // packageSize is the size of the picture part of the package
  pictureSizeCount += packageSize;
  Serial.write(package,packageSize);
  if( pictureSizeCount >= pictureSize )  {
    digitalWrite( LED_PIN, LOW );
    Serial.flush();
  }
}

void setup(){
  Serial.begin( USB_BAUD );
  mySerial.begin(CAMERA_BAUD);
  pinMode( LED_PIN, OUTPUT );
  digitalWrite( LED_PIN, LOW );
}

void loop(){
  if( Serial.available() ){
    while(Serial.read() != -1);
    digitalWrite( LED_PIN, HIGH );
    if( !camera.sync() )
    {
      Serial.println( "Sync failed." );
      return;
    }
    if( !camera.initial( C328R::CT_JPEG, C328R::PR_160x120, C328R::JR_640x480 ) )
    {
      Serial.println( "Initial failed." );
      return;
    }
    if( !camera.setPackageSize( 64 ) )
    {
      Serial.println( "Package size failed." );
      return;
    }
    if( !camera.setLightFrequency( C328R::FT_50Hz ) )
    {
      Serial.println( "Light frequency failed." );
      return;
    }
    if( !camera.snapshot( C328R::ST_COMPRESSED, 0 ) )
    {
      Serial.println( "Snapshot failed." );
      return;
    }
    pictureSizeCount = 0;
    if( !camera.getJPEGPicture( C328R::PT_JPEG, PROCESS_DELAY, &getJPEGPicture_callback ) )
    {
      Serial.println( "Get JPEG failed." );
      return;
    }
  }
}

Processing code:
Code:
import processing.serial.*;
Serial myPort;
String filename = "photo.jpg";
byte[] photo = {};
Boolean readData = false;
PImage captureImage;
void setup(){
  size(640,480);
  println( Serial.list() );
  myPort = new Serial( this, Serial.list()[1], 115200 );   //Serial.list()[3][ch12399][ch29872][ch22659][ch12395][ch21512][ch12431][ch12379][ch12390][ch22793][ch26356][ch12377][ch12427][ch12371][ch12392][ch12290]
}

void draw(){
  byte[] buffer = new byte[64];
  if( readData )  {
    while( myPort.available() > 0 )    {
      int readBytes = myPort.readBytes( buffer );
      print( "Read " );
      print( readBytes );
      println( " bytes ..." );
      for( int i = 0; i < readBytes; i++ )      {
        photo = append( photo, buffer[i] );
      }
    }
  }  else  {
    while( myPort.available() > 0 )    {
      print( "COM Data: " );
      println( myPort.readString() );
    }
  }
}

void keyPressed(){
  if( photo.length > 0 ) {
    readData = false;
    print( "Writing to disk " );
    print( photo.length );
    println( " bytes ..." );
    saveBytes( filename, photo );
    println( "DONE!" );
    photo = new byte[0];
    captureImage = loadImage(filename);
    image(captureImage, 0, 0);
  }  else {
    readData = true;
    myPort.write(0);
    println( "Waiting for data ..." );
  }
}
« Last Edit: July 05, 2010, 11:13:44 am by ronczap » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Ron for posting this. But when you say you close IDE and the run the processing, do you mean you open another sketch in Arduino IDE and upload it again? Please clarify, as I have never used processing.
Logged

Louisville, KY
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hadn't used it before either. You can download it and learn about it here:
http://processing.org/

Since the Arduino IDE is partially based on Processing, it looks similar and uses the .pde file extension.

- Ron
« Last Edit: July 05, 2010, 01:32:02 pm by ronczap » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also have another question regarding the raw read mode of the C328 camera. I am using Serial1 on Arduino Mega to read the data sent from camera. I am aware that serial buffer can only hold 127 bytes. Using the resolution of 80*60 I am expecting 4800 bytes with grayscale mode. How would I go about reading the data from the camera? Do you have to flush the serial1 buffer every now and then?

Thanks.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great! Great! Great!

I finally have the C328R camera working with my Arduino Duemilanove!!
After about 10 months of trying different codes, configurations, etc, it si working now that to the libraries and codes (arduino and processing showed here). So thanks for that guys!

Just only a couple of questions:
1.- I wired this circuit http://blog-imgs-15-origin.fc2.com/a/r/m/arms22/jpeg_camera_module_connection2.png and not this one http://blog-imgs-15-origin.fc2.com/a/r/m/arms22/4007500769_35ed6a83d1_b.jpg : i think that it is preferable the second one, right? In that case, wich ones are the components (resistance values that you use and the voltage regulator?

2.- What i want to do now is to save the images on a microSD (i have this shield: http://www.libelium.com/tienda/catalog/product_info.php?cPath=21&products_id=66. Any idea?

Thanks again for your inputs in this form that allowed to me to have the camera running!

cHEERS!
Logged

Louisville, KY
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of using a SD card, I used a Vinculum VDIP1 module to save files on to a USB Thumbdrive

See this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278806653/1
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 449
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Ron C.

Looks interesting, i will consider also this option...
Cheers,
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: