Make a function , the name of a file in the sd card

Hello !

I have made a code and a electric mount that made if a person walk in front of a sensor ,a camera take a picture and the pic is save into the sd card but i have add a humidty and temperature sensor and i want that the humidity and the temperature going to be the name of the picture in the sd card but i don’t know how , i have tried but i didnt success ( all the material work i have tested the sensor , camera etc)

If you want i can give the code that i use , he is in the attachment part

Thanks !!

Tout_plus_humidtytemp.ino (8.14 KB)

Upload your code directly using code tags.

Why do you want to have the function name (as stated in the title) be the name of the picture? That isn't going to work and doesn't make much sense to me.

{
is_header = true;
myCAM.CS_HIGH();
//Create a avi file
k = DHT.humidity,DHT.temperature ;
itoa(k, str, 10);
strcat(str, “.jpg”);
//Open the new file
outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
if (! outFile)
{
Serial.println(F(“File open failed”));
while (1);
}
myCAM.CS_LOW();
myCAM.set_fifo_burst();
buf[i++] = temp_last;
buf[i++] = temp;
}
}

Here is the code for the sd in the entire code, i want to make DHT.temperature and DHT.humidty
the name of the pic taked.

oh seriously …how i can make humity and temperature the name of the file so ?

thanks for your reply

Use code-tags when posting code. It is the </> button in the top left corner when you post something.

zakari944:
oh seriously …how i can make humity and temperature the name of the file so ?

You simply can’t do that in C/C++. However, I think what you really want to do is store the humidity and temperature values in a file which relates to the picture by its name?

Yes , i think its will be better if i do that
but how ?

  else if ((temp == 0xD8) & (temp_last == 0xFF))
  {
      is_header = true;
    myCAM.CS_HIGH();
    //Create a avi file
    k = k + 1;
    itoa(k, str, 10);
    strcat(str, ".jpg");
    //Open the new file
    outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
    file = SD.open("Humidité & Temperature.TXT", FILE_WRITE);
    file.println("DHT.temperature,DHT.humidty");
    if (! outFile)
    {
      Serial.println(F("File open failed"));
      while (1);
    }
    myCAM.CS_LOW();
    myCAM.set_fifo_burst();   
    buf[i++] = temp_last;
    buf[i++] = temp;   
  }

}
  myCAM.CS_HIGH();
  return 1;
}

I have made this , but it dont put the humidity and temperature in the file :confused: he create the file but dont do anything more

zakari944:
I have made this , but it dont put the humidity and temperature in the file :confused: he create the file but dont do anything more

Try closing the file, or flushing the content to the sd-card after you write to the file.

The first thing you have to do is to restructure and clean up your code. Right now it looks horrible and I really don't want to read through that mess. Here you go with some pseudo code instead:

void loop()
{
    auto humidity = getHumidityReading();
    auto temperature = getTemperatureReading();
    auto image = takePicture();
    
    auto imageFilename = generateImageFilename();
    storeImageToSdCard(imageFilename, image);
    
    auto sensorValuesFilename = generateSensorValuesFilename();
    storeSensorValuesToSdCard(humidity, temperature);
}

Yes i didnt clean the code i have think that in the end i will clean all but yea im going to make it now when i finish i repost the code

Okey i have cleaned the code and organised it , i have tried somethign but it didnt worked :confused: ( im a newbie in arduino srry)

#include <stdio.h>
#include <DS1302.h>
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include <SD.h>
#include "memorysaver.h"
#include <RTClib.h>
#include <dht.h>


//This demo can only work on OV5640_MINI_5MP_PLUS or OV5642_MINI_5MP_PLUS platform.
#if !(defined (OV5640_MINI_5MP_PLUS)||defined (OV5642_MINI_5MP_PLUS))
#error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
#endif
#define   FRAMES_NUM    0x00
// set pin 7 as the slave select for the digital pot:
const int CS = 7;
// Pin for  day/hour/month for each file that are stored in the sd
const uint8_t SD_CS = 10;
#define SD_CS 9
File file;  // test file
bool is_header = false;
//int total_time = 0;
#if defined (OV5640_MINI_5MP_PLUS) 
  ArduCAM myCAM(OV5640, CS);
#else
  ArduCAM myCAM(OV5642, CS);
#endif
uint8_t read_fifo_burst(ArduCAM myCAM);

// Define the humidty and temp sensor object and define the pin where the information go
dht DHT;
#define DHT11_PIN 3

// Pin connection for the clock
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 4;  // Serial Clock

// Create a DS1302 object (clock object ).
DS1302 rtc(kCePin, kIoPin, kSclkPin);
 char buf[30];

// Define the code and the placement for the date of each file who are put in the ( when there are create)
void printTime(uint16_t* date,uint16_t* time ) {
  // Get the current time and date from the chip.
  Time t = rtc.time();
  // Name the day of the week.
  const String day = dayAsString(t.day);
  // Format the time and date and insert into the temporary buffer.
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day.c_str(),t.date,t.yr, t.mon, t.hr, t.min, t.sec);
  Serial.println("yy");
  *date = FAT_DATE(t.yr,t.mon, t.date);
  *time = FAT_TIME(t.hr, t.min, t.sec);

}

String dayAsString(const Time::Day day) {
  switch (day) {
    
    case Time::kMonday: return "Lundi";
    case Time::kTuesday: return "Mardi";
    case Time::kWednesday: return "Mercredi";
    case Time::kThursday: return "Jeudi";
    case Time::kFriday: return "Vendredi";
    case Time::kSunday: return "Samedi";
    case Time::kSaturday: return "Dimanche";
  }
  return "(unknown day)";
}



void setup() {
// put your setup code here, to run once:

// Take the time from the clock to synchronize it with the file who is put a in the sd
 SdFile::dateTimeCallback(printTime);
Time t = rtc.time();
const String day = dayAsString(t.day);
   snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",day.c_str(),
   t.date,t.yr, t.mon, t.hr, t.min, t.sec);
 
// camera code
uint8_t vid, pid;
uint8_t temp;
#if defined(__SAM3X8E__)
  Wire1.begin();
#else
  Wire.begin();
#endif
Serial.begin(115200);
Serial.println(F("ArduCAM Start!"));

// set the CS as an output:
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);

// initialize SPI:
SPI.begin();
//Reset the CPLD
myCAM.write_reg(0x07, 0x80);
delay(100);
myCAM.write_reg(0x07, 0x00);
delay(100); 
  
while(1){
  //Check if the ArduCAM SPI bus is OK
  myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
  temp = myCAM.read_reg(ARDUCHIP_TEST1);
  if(temp != 0x55)
  {
    Serial.println(F("SPI interface Error!"));
    delay(1000);continue;
  }else{
    Serial.println(F("SPI interface OK."));break;
  }
}
#if defined (OV5640_MINI_5MP_PLUS)
while(1){
  //Check if the camera module type is OV5640
  myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
  myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
  if ((vid != 0x56) || (pid != 0x40)){
    Serial.println(F("Can't find OV5640 module!"));
    delay(1000); continue;
  }else{
    Serial.println(F("OV5640 detected."));break;      
  }
}
#else
while(1){
  //Check if the camera module type is OV5642
  myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
  myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
  if ((vid != 0x56) || (pid != 0x42)){
    Serial.println(F("Can't find OV5642 module!"));
    delay(1000);continue;
  }else{
    Serial.println(F("OV5642 detected."));break;      
  }
}
#endif
//Initialize SD Card
while(!SD.begin(SD_CS))
{
  Serial.println(F("SD Card Error!"));delay(1000);
}
Serial.println(F("SD Card detected."));
//Change to JPEG capture mode and initialize the OV5640 module
myCAM.set_format(JPEG);
myCAM.InitCAM();
myCAM.set_bit(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);
myCAM.clear_fifo_flag();
myCAM.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);
}


void loop() {

// read the information from the humidity and temp sensor
 int chk = DHT.read11(DHT11_PIN);

// Read the value in A0 from the IR sensor
 int sensorValue = analogRead(A0);
 float voltage = sensorValue * (5.0 / 1023.0);

// Condition for taking a picture
if (voltage < 3) {
myCAM.flush_fifo();
myCAM.clear_fifo_flag();
#if defined (OV5640_MINI_5MP_PLUS)
  myCAM.OV5640_set_JPEG_size(OV5640_320x240);delay(1000);
#else
  myCAM.OV5642_set_JPEG_size(OV5642_320x240);delay(1000);
#endif
//Start capture
myCAM.start_capture();
Serial.println(F("start capture."));
while ( !myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK)); 
Serial.println(F("CAM Capture Done."));
Serial.print(F("capture total_time used (in miliseconds):"));
read_fifo_burst(myCAM);
Serial.print(F("save capture total_time used (in miliseconds):"));
myCAM.clear_fifo_flag();
delay(1000);
}
else {}
}


uint8_t read_fifo_burst(ArduCAM myCAM)
{
uint8_t temp = 0, temp_last = 0;
uint32_t length = 0;
static int i = 0;
static int k = 0;
char str[8];
File outFile;
byte buf[256]; 
length = myCAM.read_fifo_length();
Serial.print(F("The fifo length is :"));
Serial.println(length, DEC);
if (length >= MAX_FIFO_SIZE) //8M
{
  Serial.println("Over size.");
  return 0;
}
if (length == 0 ) //0 kb
{
  Serial.println(F("Size is 0."));
  return 0;
} 
myCAM.CS_LOW();
myCAM.set_fifo_burst();//Set fifo burst mode
i = 0;
while ( length-- )
{
  temp_last = temp;
  temp =  SPI.transfer(0x00);
  //Read JPEG data from FIFO
  if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
  {
    buf[i++] = temp;  //save the last  0XD9     
    //Write the remain bytes in the buffer
    myCAM.CS_HIGH();
    outFile.write(buf, i);    
    //Close the file
    outFile.close();
    Serial.println(F("OK"));
    is_header = false;
    myCAM.CS_LOW();
    myCAM.set_fifo_burst();
    i = 0;
  }  
  if (is_header == true)
  { 
    //Write image data to buffer if not full
    if (i < 256)
     buf[i++] = temp;
    else
    {
      //Write 256 bytes image data to file
      myCAM.CS_HIGH();
      outFile.write(buf, 256);
      i = 0;
      buf[i++] = temp;
      myCAM.CS_LOW();
      myCAM.set_fifo_burst();
    }        
  }
  else if ((temp == 0xD8) & (temp_last == 0xFF))
  {
      is_header = true;
    myCAM.CS_HIGH();
    //Create a avi file
    k = k + 1;
    itoa(k, str, 10);
    strcat(str, ".jpg");
    //Open the new file
    outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
    file = SD.open("Humidité & Temperature.TXT", FILE_WRITE);
    file.println("DHT.temperature,DHT.humidty");
    if (! outFile)
    {
      Serial.println(F("File open failed"));
      while (1);
    }
    myCAM.CS_LOW();
    myCAM.set_fifo_burst();   
    buf[i++] = temp_last;
    buf[i++] = temp;   
  }

}
  myCAM.CS_HIGH();
  return 1;
}

I have tried to definne the fonction like this and thats doesnt work

void loop()
{



    getHumidityReading = DHT.humidity 
     getTemperatureReading == DHT.temperature 

    auto humidity = getHumidityReading();
    auto temperature = getTemperatureReading();
    auto image = takePicture();
    
    auto imageFilename = generateImageFilename();
    storeImageToSdCard(imageFilename, image);
    
    auto sensorValuesFilename = generateSensorValuesFilename();
    storeSensorValuesToSdCard(humidity, temperature);
}
    auto sensorValuesFilename = generateSensorValuesFilename();
    storeSensorValuesToSdCard(humidity, temperature);

Are we allowed to see these two functions or are they secret ?

I dont know wath do with these 2 functions :confused: im lost and i dont know wath to do

First of, a few words about clean code and cleaning up code:

  1. Use functions. A function is a piece of code that helps you to reduce code redundancy. You can call it and it can return something. If you are a beginner, you can read more about functions here.

  2. Don't make your functions larger than 7-10 lines of code. There are of course some exceptions, were you need a little more lines. With short functions you can very quickly read through the code for something of interest.

  3. Give good names to your functions. Never stop thinking about the name of a functions if you see it in code. Often it requires long time before you come up with a good function name.

  4. The same goes for variable names. Give it a name, so nobody has to think twice about its purpose.

  5. Each function should have one purpose. Only one. That purpose should be clear when you read the functions name. For example 'read_fifo_burst' is quite a bad name. What does it do? It reads a fifo in burst mode. What kind of fifo? What burst mode? Also when you look into your function, it doesn't just read the fifo. It also prints statements to the serial monitor, opens a file on the sd card and even has the potential to lock up your whole program, because if the file can't be opened you end up in an endless loop.

So your functions real name should be something like: readImageIntoBufferAndWriteTheDataToTheSDCardAndBlockWhenTheFileCantBeOpenedAlsoPrintOutLogMessages

Now that would be an awfull name for a function. It is better to split the function into smaller ones:

int imageSize = readImageIntoBuffer(imageBuffer, bufferSize);
writeImageToSdCard(filename, imageBuffer, imageSize);

As for your clean-up attempt. Just take a look at these lines of your code:

// Pin for  day/hour/month for each file that are stored in the sd
const uint8_t SD_CS = 10;
#define SD_CS 9

What is your CS line connected to? Pin 10 or pin 9? Now this is something you should clean-up.

UKHeliBob:

    auto sensorValuesFilename = generateSensorValuesFilename();

storeSensorValuesToSdCard(humidity, temperature);



Are we allowed to see these two functions or are they secret ?

These functions are from my pseudo-code example. I guess the OP just throw that in and tried to compile.

I just took a look at the ArduCAM library. Until now I thought you had written the code by yourself, but it seems it's just one of the library examples?

Lol no i didnt write myself the code i cant do that , yea its one of the library example that i have modified , i have sucessfull about the write of the umidty dand the temperature in the txt file , i jsut have to fix a problem with the char.

i have put the cs in 9 yea i have delete the line where it say its connected to 10

thanks for ur help