Go Down

Topic: Sensors on a single arduino uno (Read 473 times) previous topic - next topic

estillcn

Hey guys! I am trying to put 4 sensors onto a single Arduino. 2 ttl cameras, a single co2meter, and a singleir thermal camera, I have been working on this code for weeks, and I am 95 percent sure that the hardware is correct, if you want to check me, here are some pictures. It is very messy I know, if you need clarification please ask.

I know that I am doing some extra with having two different camera functions but I couldn't find another way to do it. Also, I only need to take data once every hour. And I have never posted to arduino forums before, if I have forgotten anything please ask. If you need more explanation for the code, I can give you that. There are some comments in there already. Also the IR and the Cameras are adapted from the example code that is given from their libraries, here those are if you want to have a look.

I can post the code in another reply though because otherwise this is too big.

Thanks a bunch to anyone that can help, and I'm kind of on a deadline so the faster the better, I have to come up with something by Tuesday at the absolute latest. I love you all if you decide to help me conquer this. Thanks again in advance.

estillcn

My Code:
Code: [Select]
#include <Wire.h>
#include <Adafruit_AMG88xx.h>
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>

//void cameraStuff(Adafruit_VC0706 cam);
//void setup();
//void loop();
//void getCo2();
//String* sense(String command);
//void irStuff();

// How long the device waits before taking data again.
int mins = 1;
int index = 1;

#define chipSelect 10

void setup() {
  
  // Serial monitor comm
  Serial.begin(9600);

  // Stuff below for SD card comm
  if (!SD.begin(chipSelect)) {
    return;
  }

  if (SD.exists("co2Data.txt")) {
    SD.remove("co2Data.txt");
  }

  if (SD.exists("irData.txt")) {
    SD.remove("irData.txt");
  }
  
  pinMode(10, OUTPUT);
  
}



void loop() {
  
  switch (index){
    case 1:
      irStuff();
      Serial.println("ir");
      for (int i = 0; i < 5; i++) {
        delay(1000);
      }
      break;
    case 2:
//      cameraStuff1();
//      cameraStuff2();
      Serial.println("ca");
      for (int i = 0; i < 5; i++) {
        delay(1000);
      }
      break;
    case 3:
      getCo2();
      Serial.println("co");
      for (int i = 0; i < 5; i++) {
        delay(1000);
      }
      break;
    case 4:
      index = 0;
      delay(1000);
//      for (int i = 0; i < 60 * mins; i++) {
//        delay(1000);
//      }
      break;
    default:
      break;
  }
  index++;
}

void getCo2() {
  //purple goes in 6 and grey goes in 7
  SoftwareSerial Serial1(6, 7);
  // co2 serial comm
  Serial1.begin(9600);
  File co2Data;

  float output2 = 0;
  
  String* raw2 = sense("Z\r\n", Serial1);
  output2 = raw2->substring(2, 7).toFloat() * 100;
  delete raw2;

  co2Data = SD.open("co2Data.txt", FILE_WRITE); // Opens the file
  if (co2Data) {
    co2Data.print(output2);
    co2Data.println();
  }
  else { // If it didnt write to the file, tell the user with an error
    digitalWrite(4, HIGH);
  }
  co2Data.close();
  Serial1.end();
}

String* sense(String command, SoftwareSerial Serial1) {
  Serial1.print(command);
  Serial.flush();
  delay(200);
  String* raw = new String();
  char incoming_byte = 0;
  
  while (incoming_byte != '\n') {
    // read the incoming byte:
    incoming_byte = Serial1.read();
    *raw += incoming_byte;
  }
  
  return raw;
}

void irStuff() {
  
  Adafruit_AMG88xx amg;

  float pixels[AMG88xx_PIXEL_ARRAY_SIZE];

  File irData;

  bool status;
  // default settings
  status = amg.begin();

  if (!status) {
    Serial.println("Ir not plugged in correctly");
    exit(1);
  }
  //read all the pixels
  amg.readPixels(pixels);
  //create file for writing to for ir data
  for (int i = 1; i <= AMG88xx_PIXEL_ARRAY_SIZE; i++) {
    irData = SD.open("irData.txt", FILE_WRITE); // Opens file
    if (irData) {
      
      irData.print(pixels[i - 1]);
      irData.print("\t");
      
      if ( i % 8 == 0 ) {
        irData.println();
      }
    }
    else { // If it didnt write to the file, tell the user with an error
      Serial.println("it did not print to the sd card");
    }
    irData.close();
  }
  irData = SD.open("irData.txt", FILE_WRITE); // Opens file
  irData.println();
  irData.close();
  delete pixels;
}


void cameraStuff1() {
  SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
  Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

  if (cam.begin()) {
   Serial.println("camera found");
  } else {
    Serial.println("camera not found");
    return;
  }

  char *reply = cam.getVersion();

  cam.setImageSize(VC0706_640x480);        // biggest
  //cam.setImageSize(VC0706_320x240);        // medium
  //  cam.setImageSize(VC0706_160x120);          // small

  // You can read the size back from the camera (optional, but maybe useful?)
  uint8_t imgsize = cam.getImageSize();

  // Create an image with the name IMAGExx.JPG
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i / 10;
    filename[6] = '0' + i % 10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }

  if (cam.takePicture())
    Serial.println("took a picture");
  else
    Serial.println("didnt take a picture");

  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  if (imgFile) {
Serial.println("file saved");
  }

  // Get the size of the image (frame) taken
  uint16_t jpglen = cam.frameLength();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  while (jpglen > 0) {
    // read 32 bytes at a time;
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    jpglen -= bytesToRead;
  }
  imgFile.close();
  cameraconnection.end();
}

void cameraStuff2() {
  SoftwareSerial cameraconnection = SoftwareSerial(4, 5);
      Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

  if (cam.begin()) {
   Serial.println("camera found");
  } else {
    Serial.println("camera not found");
    return;
  }

  char *reply = cam.getVersion();

  cam.setImageSize(VC0706_640x480);        // biggest
  //cam.setImageSize(VC0706_320x240);        // medium
  //  cam.setImageSize(VC0706_160x120);          // small

  // You can read the size back from the camera (optional, but maybe useful?)
  uint8_t imgsize = cam.getImageSize();

  // Create an image with the name IMAGExx.JPG
  char filename[14];
  strcpy(filename, "IMAGE002.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i / 10;
    filename[6] = '0' + i % 10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }

  if (cam.takePicture())
    Serial.println("took a picture");
  else
    Serial.println("didnt take a picture");

  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  if (imgFile) {
Serial.println("file saved");
  }

  // Get the size of the image (frame) taken
  uint16_t jpglen = cam.frameLength();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  while (jpglen > 0) {
    // read 32 bytes at a time;
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    jpglen -= bytesToRead;
  }
  imgFile.close();
  cameraconnection.end();
}






estillcn

Camera Example Code:
Code: [Select]
// This is a basic snapshot sketch using the VC0706 library.
// On start, the Arduino will find the camera and SD card and
// then snap a photo, saving it to the SD card.
// Public domain.

// If using an Arduino Mega (1280, 2560 or ADK) in conjunction
// with an SD card shield designed for conventional Arduinos
// (Uno, etc.), it's necessary to edit the library file:
//   libraries/SD/utility/Sd2Card.h
// Look for this line:
//   #define MEGA_SOFT_SPI 0
// change to:
//   #define MEGA_SOFT_SPI 1
// This is NOT required if using an SD card breakout interfaced
// directly to the SPI bus of the Mega (pins 50-53), or if using
// a non-Mega, Uno-style board.

#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>

// comment out this line if using Arduino V23 or earlier
#include <SoftwareSerial.h>        

// uncomment this line if using Arduino V23 or earlier
// #include <NewSoftSerial.h>      

// SD card chip select line varies among boards/shields:
// Adafruit SD shields and modules: pin 10
// Arduino Ethernet shield: pin 4
// Sparkfun SD shield: pin 8
// Arduino Mega w/hardware SPI: pin 53
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
#define chipSelect 10

// Pins for camera connection are configurable.
// With the Arduino Uno, etc., most pins can be used, except for
// those already in use for the SD card (10 through 13 plus
// chipSelect, if other than pin 10).
// With the Arduino Mega, the choices are a bit more involved:
// 1) You can still use SoftwareSerial and connect the camera to
//    a variety of pins...BUT the selection is limited.  The TX
//    pin from the camera (RX on the Arduino, and the first
//    argument to SoftwareSerial()) MUST be one of: 62, 63, 64,
//    65, 66, 67, 68, or 69.  If MEGA_SOFT_SPI is set (and using
//    a conventional Arduino SD shield), pins 50, 51, 52 and 53
//    are also available.  The RX pin from the camera (TX on
//    Arduino, second argument to SoftwareSerial()) can be any
//    pin, again excepting those used by the SD card.
// 2) You can use any of the additional three hardware UARTs on
//    the Mega board (labeled as RX1/TX1, RX2/TX2, RX3,TX3),
//    but must specifically use the two pins defined by that
//    UART; they are not configurable.  In this case, pass the
//    desired Serial object (rather than a SoftwareSerial
//    object) to the VC0706 constructor.

// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif

Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

// Using hardware serial on Mega: camera TX conn. to RX1,
// camera RX to TX1, no SoftwareSerial object is required:
//Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);

void setup() {

  // When using hardware SPI, the SS pin MUST be set to an
  // output (even if not connected or used).  If left as a
  // floating input w/SPI on, this can cause lockuppage.
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
  if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

  Serial.begin(9600);
  Serial.println("VC0706 Camera snapshot test");
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }  
  
  // Try to locate the camera
  if (cam.begin()) {
    Serial.println("Camera Found:");
  } else {
    Serial.println("No camera found?");
    return;
  }
  // Print out the camera version information (optional)
  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print("Failed to get version");
  } else {
    Serial.println("-----------------");
    Serial.print(reply);
    Serial.println("-----------------");
  }

  // Set the picture size - you can choose one of 640x480, 320x240 or 160x120
  // Remember that bigger pictures take longer to transmit!
  
  cam.setImageSize(VC0706_640x480);        // biggest
  //cam.setImageSize(VC0706_320x240);        // medium
  //cam.setImageSize(VC0706_160x120);          // small

  // You can read the size back from the camera (optional, but maybe useful?)
  uint8_t imgsize = cam.getImageSize();
  Serial.print("Image size: ");
  if (imgsize == VC0706_640x480) Serial.println("640x480");
  if (imgsize == VC0706_320x240) Serial.println("320x240");
  if (imgsize == VC0706_160x120) Serial.println("160x120");

  Serial.println("Snap in 3 secs...");
  delay(3000);

  if (! cam.takePicture())
    Serial.println("Failed to snap!");
  else
    Serial.println("Picture taken!");
  
  // Create an image with the name IMAGExx.JPG
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i/10;
    filename[6] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }
  
  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  // Get the size of the image (frame) taken  
  uint16_t jpglen = cam.frameLength();
  Serial.print("Storing ");
  Serial.print(jpglen, DEC);
  Serial.print(" byte image.");

  int32_t time = millis();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  byte wCount = 0; // For counting # of writes
  while (jpglen > 0) {
    // read 32 bytes at a time;
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
      Serial.print('.');
      wCount = 0;
    }
    //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
    jpglen -= bytesToRead;
  }
  imgFile.close();

  time = millis() - time;
  Serial.println("done!");
  Serial.print(time); Serial.println(" ms elapsed");
}

void loop() {
}

estillcn

IR Thermal Camera Example Code:
Code: [Select]
/***************************************************************************
  This is a library for the AMG88xx GridEYE 8x8 IR camera

  This sketch tries to read the pixels from the sensor

  Designed specifically to work with the Adafruit AMG88 breakout
  ----> http://www.adafruit.com/products/3538

  These sensors use I2C to communicate. The device's I2C address is 0x69

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Dean Miller for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <Adafruit_AMG88xx.h>

Adafruit_AMG88xx amg;

float pixels[AMG88xx_PIXEL_ARRAY_SIZE];

void setup() {
    Serial.begin(9600);
    Serial.println(F("AMG88xx pixels"));

    bool status;
    
    // default settings
    status = amg.begin();
    if (!status) {
        Serial.println("Could not find a valid AMG88xx sensor, check wiring!");
        while (1);
    }
    
    Serial.println("-- Pixels Test --");

    Serial.println();

    delay(100); // let sensor boot up
}


void loop() {
    //read all the pixels
    amg.readPixels(pixels);

    Serial.print("[");
    for(int i=1; i<=AMG88xx_PIXEL_ARRAY_SIZE; i++){
      Serial.print(pixels[i-1]);
      Serial.print(", ");
      if( i%8 == 0 ) Serial.println();
    }
    Serial.println("]");
    Serial.println();

    //delay a second
    delay(1000);
}

estillcn

Here is the last of what I need to share (I think). This is the picture of the setup, I KNOW you will need clarification, but all the sensors work individually with this setup and I don't want to mess with it right now. So any questions are very helpful.

ieee488

You don't have to write 4 post to explain clearly what exactly you want to do and what is not working.


Your deadline is not our concern!



.

slipstick

Thanks for that information but you forgot to tell us what it's all for. And if there are any problems with it not doing what you wanted it to then you also forgot to mention them.

Steve

Go Up