Show Posts
Pages: [1]
1  Using Arduino / Sensors / Re: Arduino Uno R3 and Linksprite LS-Y201 Serial Transmission Problem on: December 10, 2012, 12:28:42 pm
I've been struggling with this camera myself recently!

Have you tried the sketch on the linksprite website? Using that (under windows) I can get a picture that makes sense. I can see from the data that you posted that the camera isn't giving a valid jpeg (it should start ff d8...).

Thank,

Alex
2  Using Arduino / Sensors / Re: Linux + Arduino + Linksprite LS-Y201 + SoftwareSerial on: December 09, 2012, 07:40:36 am
Well, the latest Arduino IDE version under Linux behaved exactly the same ...

I have noticed as well that the blink sketch compiles to different sizes under both Linux and Windows - I assume that this must be something to do with the toolchain / gcc-avr version.

It looks like my only solution to use these cameras is to program them under Windows!  However, I'm not sure I'll be using them very much as they aren't very capable.  They are OK if you need to take occasional still images (1 per minute at best), but the resolution isn't great and they are slow.

I'm still baffled by the difference though and would dearly love to figure it out!

Alex
3  Using Arduino / Sensors / Re: Linux + Arduino + Linksprite LS-Y201 + SoftwareSerial on: December 09, 2012, 07:04:39 am
The software serial library claims speeds of up to 115200 - though I have not tested this.  In simple serial communication it can do 57600 easily though.

If I change the baudrate at all, then the camera fails to transmit anything.

Bizarrely the same sketch compiles to different sizes under both Linux and Windows (using the same version of the IDE - 1.0)!

This is the code:

Code:
/*
 * latest version of the camera tester - 09-12-2012
 *
 */

#include <SoftwareSerial.h>

SoftwareSerial camport(10,11);

void setup()
{
  Serial.begin(115200);
 
  // set up the software serial pins properly
  // rx = input, tx = output
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  camport.begin(38400);
}

void loop()
{
  // send a command to the camera
  Serial.println("resetting camera");
  resetCamera();
  delay(4000);
 
  // try and read the camera response
  while(camport.available() > 0)
  {
    byte inByte = camport.read();
    Serial.print(inByte, HEX);
    Serial.print(" ");   
  }
  Serial.println();
 
  // stop
  while(true)
  {
  }
     
}

// send the reset command to the camera: 0x56 0x00 0x26 0x00
void resetCamera()
{
  byte resetCmd[4] = {0x56, 0x00, 0x26, 0x00}; 
  camport.write(resetCmd, 4);
}

Which weighs in at 5056 bytes under Windows and 4688 bytes in Linux.

I am about to try the very latest Linux version - will report how that goes!

Thanks for your help,

Alex
4  Using Arduino / Sensors / Linux + Arduino + Linksprite LS-Y201 + SoftwareSerial on: December 08, 2012, 10:06:22 am
Hi all,

I am currently trying to use the above camera (https://www.sparkfun.com/products/10061?) with an Arduino Uno in one of my projects; however, I cannot get the camera to communicate properly via the SoftwareSerial port under Linux (currently using Mint 13).  When I send a command to the camera I don't get back the expected response - for example (from the manual):

send >> 0x56 0x00 0x26 0x00 (reset)
recv << 0xF6 0x80 0x93 0x80 0xC0 0xD6 0xA1 ... (expected  0x76 0x00 0x26 0x00)

After many days / weeks puzzling at this, I turned to the dark side smiley-twist and uploaded my code via a windows machine - and now I get the expected response ... I can even receive the correct response from the Linux serial monitor - providing the code was uploaded from Windows ...

Can anybody provide any sort of explanation for this behaviour?  Is there any logical reason why the compiled code should be different?  And is there anything I can do about it?

Many thanks,

Alex
5  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 20, 2012, 09:31:37 am
Thanks Paul!

Alex
6  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 20, 2012, 07:46:58 am
Thank you - that explains a lot!

Would the following code be OK for checking that there is space in the buffer?

Code:
    // if not a carriage return or new line then add the
    // character to the buffer
    else {
     
        // if there is room in the buffer for this character (plus the null terminator)
        // then copy the character into the buffer
        if (bufferIndex < (STRLEN - 1)) {
            buffer[ bufferIndex++ ] = ch;
        }
     
    }

Thanks again,

Alex
7  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 20, 2012, 06:53:57 am
I have now had a good look at handling char[]s in C/C++ - thanks for the pointers.  Hopefully I now understand them a bit better.

I _think_ my code now works.  Much of the extremely strange behaviour with the SD card seems to be due to using too big a character buffer.  If I use 256 or 512 for STRLEN, the code below works fine.  However, if I use 1024 everything starts misbehaving (I seem to get the setup function constantly being called).  I assume this is something to do with program memory being overwritten (?)

Thanks for your help.

Alex

Code:
#include <SD.h>

// string length is 256 bytes
#define STRLEN 256

// chip select is pin 4 on the ethernet shield
const int chipSelect = 4;

// initialise the string (really a char array!)
char buffer[ STRLEN ];
int  bufferIndex = 0;

// set up the arduino
void setup() {

  // set up serial
  Serial.begin(9600);
  Serial.println("serial comms working ... ");
 
  // pin 10 must be output for the SD card!
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized
  if (!SD.begin(chipSelect)) {   
    return;  // do nothing ...
  }
  Serial.println("card initialized ...");
 
}

// main program loop
void loop() {
 
  // whilst there is serial data waiting ...
  if (Serial.available()) {
   
    // read character from serial monitor
    char ch = Serial.read();
   
    // if the character is a carriage return or new line
    if (ch == '\r' || ch == '\n') {
     
      // terminate the string with a 0 (null terminated)
      buffer[ bufferIndex ] = 0;
     
      // Serial echo output
      Serial.print("echo >>> ");
      Serial.println(buffer);

      // open the file - note that only one file can be open
      // at a time so you have to close this one before
      // opening another
      Serial.println("opening file ...");
      File dataFile = SD.open("log1.txt", FILE_WRITE);

      // if the file is available then write to it
      if (dataFile) {
        Serial.println("writing to file ...");
        dataFile.println(buffer);
        dataFile.close();
      } 
      // if the file isn't open then pop up an error
      else {
        Serial.println("error opening file");
      }
     
      // reset the index ready for another string         
      Serial.println("resetting index ...");
      bufferIndex = 0;               
           
    }
   
    // if not a carriage return or new line then add the
    // character to the buffer
    else {
     
      buffer[ bufferIndex++ ] = ch;
     
    }
  }
}
8  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 19, 2012, 01:40:23 pm
@pauls and here was me thinking arduino was for beginners and artist types...

Much of my programming experience is with java and c# so I've not spent much time dealing with low level bytes and chars.

I still am seeing some quite strange behaviour with processing bytes and chars into strings and writing them to the sd card. I'll go read up on strings / char[]) in c...

Alex
9  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 19, 2012, 06:14:37 am
@PaulS

Thanks for your scathing assessment of my knowledge and code.  You sure know how to make someone feel welcome in the community ...

Before posting on the forum I spent a while working through the String constructor tutorial on the Arduino website, but there is no mention there of null terminating strings.  However, following your advice, I have added '\0' to the end of my character array to null terminate the string and it now works. 

Code:
      // string conversion
      char str[ix];
      for(int i = 0; i < ix; i++) {       
        str[i] = (char)buff[i];
      }
      str[ix] = '\0';

Thanks,

Alex
10  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 19, 2012, 05:19:32 am
@paulS

I have also tried constructing a string from my character array:

String properString =  String(str);

before using println.  I still get the same error (I think).
11  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 19, 2012, 05:17:16 am
Ahh metaphysics ...  smiley

Because there is no dynamic array class I have initialised my array to be bigger than I need.  So, therefore, I only want to print the first (ix - 1) elements to the file (ix-1 being the number of bytes I have read from the serial port).

12  Using Arduino / Programming Questions / Re: Weird issues using serial input with SD library on: March 19, 2012, 05:06:39 am
It should read:

Code:
      for(int i = 0; i < ix; i++) {       
        str[i] = (char)buff[i];
      }

I'm building up a character array from the raw byte array that I've read in from the serial monitor.

// initialise a character array of the correct size
// for every byte in the byte array that we've read in ...
// ... cast to a char and assign to our character array

Thanks,

Alex
13  Using Arduino / Programming Questions / Weird issues using serial input with SD library on: March 19, 2012, 04:45:19 am
I'm currently trying to read data from the serial monitor and write it to a file on the SD card using the rough structure below:

// setup everything

// main program

// -- read string from serial monitor byte by byte until we get cr or nl
// -- once our string is finished, echo to serial
// -- open sd card file and append the string to the end (and close the file)

Everything works fine if I initialise the file from within the setup loop:

Code:
File dataFile

void setup() {

<snip ...>

dataFile = SD.open("log0.txt", FILE_WRITE);

}

and then write to it in the main loop:

Code:
void loop() {

<snip ...>

dataFile.println("string");
dataFile.flush();

}

However if I try initialising the file within the main loop (as in the examples) I end up with weird ascii characters at the end of my character array.

Code:
void loop() {

<snip ...>

File dataFile = SD.open("log0.txt", FILE_WRITE);

// if the file is available then write to it
if (dataFile) {
dataFile.println(str);
dataFile.close();

// if the file isn't open then pop up an error
else {
Serial.println("error opening datalog.txt");
}

}

Could anybody explain this behaviour to me?  At the moment I am working around it by not closing the file and instead calling flush() after I have tried to write the string to the file to ensure all the data is written.  However, I know this isn't best practice and I am sure this will come back to bite me ...

I have provided the complete programs below for reference.

Thanks,

Alex

Working:

Quote
#include <SD.h>

// chip select is pin 4 on the ethernet shield 
const int chipSelect = 4;

// the maximum data buffer is 1024 bytes
const int dataSize = 256;

// initialise data buffer
byte buff[dataSize];
int ix;

byte cr = 13;  // carriage return
byte nl = 10;  // new line

File dataFile;

void setup() {

  // setup serial port
  Serial.begin(9600);
  Serial.println("serial comms working ...");

  // initialise ...
  ix = 0;
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized
  if (!SD.begin(chipSelect)) {   
    return;
  }
  Serial.println("card initialized ...");

  // for some reason opening the sd card file in the main loop goes weird. 
  // characters in the serial monitor are strange
  dataFile = SD.open("log0.txt", FILE_WRITE);

}

void loop() {
  
  // check that the serial port has data waiting to be read
  while (Serial.available() > 0) {

    // read a byte and, if it's not the line ending character,
    // add it to the buffer
    byte inByte = Serial.read();   
    if (inByte != cr && inByte != nl) {
      buff[ix] = inByte;
      ix++;
    }
    else {
      
      // string conversion
      char str[ix-1];
      for(int i = 0; i < ix; i++) {       
        str = (char)buff;
      }

      // Serial output
      Serial.print("echo >>> ");
      Serial.println(str);

      // if the file is available then write to it
      if (dataFile) {
        dataFile.println(str);
        dataFile.flush();
      }  
      // if the file isn't open then pop up an error
      else {
        Serial.println("error opening datalog.txt");
      }

      // reset the index
      ix = 0;

    }

  }

}


Producing weirdness:

Quote
#include <SD.h>

// chip select is pin 4 on the ethernet shield 
const int chipSelect = 4;

// the maximum data buffer is 1024 bytes
const int dataSize = 256;

// initialise data buffer
byte buff[dataSize];
int ix;

byte cr = 13;  // carriage return
byte nl = 10;  // new line

void setup() {

  // setup serial port
  Serial.begin(9600);
  Serial.println("serial comms working ...");

  // initialise ...
  ix = 0;
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized
  if (!SD.begin(chipSelect)) {   
    return;
  }
  Serial.println("card initialized ...");

}

void loop() {
  
  // check that the serial port has data waiting to be read
  while (Serial.available() > 0) {

    // read a byte and, if it's not the line ending character,
    // add it to the buffer
    byte inByte = Serial.read();   
    if (inByte != cr && inByte != nl) {
      buff[ix] = inByte;
      ix++;
    }
    else {
      
      // string conversion
      char str[ix-1];
      for(int i = 0; i < ix; i++) {       
        str = (char)buff;
      }

      // Serial output
      Serial.print("echo >>> ");
      Serial.println(str);

      // open the file. note that only one file can be open
      // at a time so you have to close this one before
      // opening another.
      File dataFile = SD.open("log0.txt", FILE_WRITE);

      // if the file is available then write to it
      if (dataFile) {
        dataFile.println(str);
        dataFile.close();
      }  
      // if the file isn't open then pop up an error
      else {
        Serial.println("error opening datalog.txt");
      }

      // reset the index
      ix = 0;

    }

  }

}


Pages: [1]