Pages: [1]   Go Down
Author Topic: Cannot read from TTL Camera on Mega, works fine on UNO  (Read 1054 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

   I have a project where I am reading from a LinkSprite Serial TTL Camera and saving the image to an SD card.  I got this working on the UNO with an Ethernet shield.  I want to move it to the MEGA 2560 to give me more room to program.

  The problem is this:  Every time I connect the camera to the MEGA I seem unable to receive any serial data from it.  This small bit of sample code below will just hang forever (in the loop after "Waiting for camera init end") on the MEGA, but will work fine on my UNO.

I have tried several things including using SoftwareSerial on pins 50 and 51, and also using Mega's Serial2 on pins 16 and 17.  It just seems to be unable to receive from the camera on the MEGA.  Any explanations as to what the problem might be? Every time I switch back to the UNO it is fine...

Code:
/* Linksprite

Infrared Camera Test Code for MEGA.  This is meant to be a functional test of your camera and SD card only... To be used to verify your
circuit and board before you use oap_motioncamera_mega (which is the full Ethernet Shield, Motion Detector, and camera)
As such, you don't really need any of the motion detector connections outlined below


  Notes:
    Not all pins on the Mega and Mega 2560 support change interrupts, so only the following
      can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

*/

#include <SoftwareSerial.h>
#include <SD.h>

int sdCardControlPin = 4;  // SD Card Control Pin

byte incomingbyte;
byte responseMessage[20]; // Used to hold responses

//SoftwareSerial(rxPin, txPin)
SoftwareSerial mySerial(5,6);                     //Configure pin (rx) and (tx) as soft serial port
// Try using MEGA serial 2 which should be: 17 (RX) and 16 (TX)

int i=0; // loop

void setup()
{
  Serial.begin(9600);
  //Serial2.begin(38400);
  mySerial.begin(38400);
  delay(25);
 
//  if (mySerial.isListening() )
 //   Serial.println("Camera is listening");
 
  // Wait for proper response from camera
  Serial.println("Waiting for camera init end");
  i = 0;
  while(i <= 15)
  {
    if (mySerial.available()>0) {
      incomingbyte=mySerial.read();
      responseMessage[i] = incomingbyte;
      Serial.print(responseMessage[i]);
      i++;
    }
  }
 
  // Did we get the expected response?
  if (responseMessage[0] == 0x36 && responseMessage[1] == 0x32 && responseMessage[14] == 0x0A) {
    Serial.println("Camera is initialized");
  }
  else
    Serial.println("WARNING: Camera not initialized"); 
 
  // Set up SD Card
  pinMode(sdCardControlPin, OUTPUT);

  if (!SD.begin(sdCardControlPin)) {
    Serial.println("  Card failed, or not present"); // don't do anything more:
    return;
  }
  Serial.println("SD Card initialized.");
     
}



void loop()
{
  Serial.println("Beginning Loop");
 
     Serial.println("Sleeping before taking another picture...");
     delay(5000);
     
}






Thanks!
Dave
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The UNO and the Mega have different bootloaders with different timings (I think to remember that Mega waits longer till it activates the saved sketch). You don't send anything to the camera, you just wait for a string to be submitted over the serial. What if the string is submitted to the Mega while it's still waiting for a new sketch to arrive? In this case all the serial data gets lost and is never seen by the Mega.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks much @pylon!  I do suspect some sort of timing issue as I remember I solved a sticky problem on the UNO with a simple delay(50).

Unfortunately, the camera manual (the parts that were translated to useful English  smiley ) simply implies: Turn power on then wait for this string...

I think I'll go with powering the camera with an output pin so I have more control over when those characters might arrive...

Thanks again and I'll post an update when I solve.
Dave
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think I'll go with powering the camera with an output pin

Take care with that, an output pin can give 40mA in best case, and shouldn't be more than 20mA in normal cases. If your camera needs more power you should insert a transistor or MOSFET.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34246
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think software serial works on a mega. After all with four real serial ports there is simply no need for it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@pylon and @Grumpy_Mike:  Thanks much for both of your input!!

I did get it working.  There were several things I had to do:

1) Use hardware Serial (Ended up using Serial2 on 16/17)
2) Ensure the camera serial code is AFTER the setting up the SD card step.  For some reason it never works if the code is before that
3) A bit of playing around with the responses and it is working now.

I think Pylon was right - the camera's initial "power on response" was coming before the mega was awake.

I did not end up having to control the camera with a digital pin.  Lucky for me because the camera wants 100mA.

For those of you with interest, I am opensourcing this motion-triggered/camera/sd/Ethernet code (currently the mega version is a work in process) on github at https://github.com/RalfieRoo/OAP_Arduino.

Thanks again,
Dave
Logged

Pages: [1]   Go Up
Jump to: