Random Strange Library I got

Hey, so today I was just coding on the cloud IDE when I just plugged in a 3rd party device, and then just set my board as an Arduino Uno because it is similar to it. Then all of a sudden I saw a random library that I swore I didn't put inside of my code at all the library is called "arduino_secrets.h" I have never seen this before and I swear that I never made this post to get attention I just want some answer to this weird thing.

arduino_secrets.h isn't a library. It's just a header file in your sketch directory that usually contains your WiFi's SSID and password definitions.

Oh, so it is just a library for functions to keep our information safe. If so I just wonder how it got there or it is just my head acting weird.

It's not a library. It's just a header file. As to how it got there, damifino. :slight_smile: I don't use the IDE. Maybe there's some keystroke that automagically inserts it.

Oh ok, thanks for the help. Sorry if I wasted you time.

Here's a Arduino Uno R4 WiFi example, showing how arduino_secrets.h is normally used.

There are instructions telling you to enter your sensitive data in the arduino_secrets.h tab.

This is what arduino_secrets.h looks like:

You add your secret credentials between the inverted commas.

1 Like

That's cool, thanks for showing me this could be used for many ideas I have. :slight_smile: :+1:

1 Like

What makes you think that anybody would think that this question is for seeking attention?

You have not wasted somebody's time if you understand that that include does not necessarily means that it's a library.


What is known as a sketch is a directory with an ino file that has the same name as the directory.

That directory can however contain multiple files, e.g. arduino_secrets.h. In your case you don't have that file in your sketch directory and a compile will fail.

Multiple files in a sketch directory allow you to organise your sketch so you will not end up with thousands of lines of code in one file.

Take as an example the below demo sketch

/*
Demo multiple files
*/

int anArray[10];

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Multiple file demo 0.1"));
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
}

void loop()
{
  delay(2000);
  // update an element in the array
  uint8_t indexToUpdate = random(0, (sizeof(anArray)/sizeof(anArray[0])));
  anArray[indexToUpdate] = random(0,3000);
  // print the array
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
  
}

/*
  Print the content of an integer array
  In:
    Array to print
    Number of elements in the array
*/
void printArray(int *array, uint8_t numElements)
{
  for(uint8_t cnt=0; cnt<numElements; cnt++)
  {
    Serial.print(F("Element "));
    Serial.print(cnt);
    Serial.print(F(" contains the value "));
    Serial.println(array[cnt]);
  }
}

You can imagine that if you have multiple functions (some long, some short) that you end up with a lot of code in one file.

How you divide your code is up to you, usually one will place all functions related to e.g. a sensor in a separate file. For this explanation, I have simply created a file for print functions; the new file is called print.ino

Click the plus next to the current tab and select the option marked in red.

Type a name for the new file.
image

And press <enter>
image

Cut the printArray() function from the main sketch and paste it in the print.ino file. When you verify / upload the sketch, those two files will be combined into one big file by the so-called arduino builder and that big sketch will be compiled.

Now assume that you also have an array of floating point numbers that you want to print.

You can add a function to print an array of floating point numbers in print.ino.

/*
  Print the content of an integer array
  In:
    Array to print
    Number of elements in the array
*/
void printArray(int *array, uint8_t numElements)
{
  for(uint8_t cnt=0; cnt<numElements; cnt++)
  {
    Serial.print(F("Element "));
    Serial.print(cnt);
    Serial.print(F(" contains the value "));
    Serial.println(array[cnt]);
  }
}

/*
  Print the content of a float array
  In:
    Array to print
    Number of elements in the array
*/
void printArray(float *array, uint8_t numElements)
{
  for(uint8_t cnt=0; cnt<numElements; cnt++)
  {
    Serial.print(F("Element "));
    Serial.print(cnt);
    Serial.print(F(" contains the value "));
    Serial.println(array[cnt]);
  }
}

And use it in the main ino file

/*
Demo multiple files
*/

int anArray[10];
float floatArray[2] = {3.14, 2.71};

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Multiple file demo 0.1"));
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
  printArray(floatArray, sizeof(floatArray)/sizeof(floatArray[0]));
}

void loop()
{
  delay(2000);
  uint8_t indexToUpdate = random(0, (sizeof(anArray)/sizeof(anArray[0])));
  anArray[indexToUpdate] = random(0,3000);
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
  
}

As you can see, your main file stays small which allows you to keep the overview.

The use of multiple ino files makes life easy. There are however situations where that does not work due to bugs in the arduino builder. In that case you will have to resort to using .h and .cpp files.

It unfortunately looks like one can not create .cpp files in the web editor. So you have to create an empty .cpp file on the PC and import that; the .h file can be created from the menu in the web editor.

Your tabs will now look like
image

The content
main sketch

/*
Demo multiple files
*/

// include our print functions
#include "print.h"

int anArray[10];
float floatArray[2] = {3.14, 2.71};

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Multiple file demo 0.1"));
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
  printArray(floatArray, sizeof(floatArray)/sizeof(floatArray[0]));
}

void loop()
{
  delay(2000);
  uint8_t indexToUpdate = random(0, (sizeof(anArray)/sizeof(anArray[0])));
  anArray[indexToUpdate] = random(0,3000);
  printArray(anArray, sizeof(anArray)/sizeof(anArray[0]));
  
}

print.h

#ifndef _PRINT_H_
#define _PRINT_H_
// function prototypes
void printArray(int *array, uint8_t numElements);
void printArray(float *array, uint8_t numElements);
#endif

print.cpp

// required include
#include <Arduino.h>

/*
  Print the content of an integer array
  In:
    Array to print
    Number of elements in the array
*/
void printArray(int *array, uint8_t numElements)
{
  for(uint8_t cnt=0; cnt<numElements; cnt++)
  {
    Serial.print(F("Element "));
    Serial.print(cnt);
    Serial.print(F(" contains the value "));
    Serial.println(array[cnt]);
  }
}

/*
  Print the content of a float array
  In:
    Array to print
    Number of elements in the array
*/
void printArray(float *array, uint8_t numElements)
{
  for(uint8_t cnt=0; cnt<numElements; cnt++)
  {
    Serial.print(F("Element "));
    Serial.print(cnt);
    Serial.print(F(" contains the value "));
    Serial.println(array[cnt]);
  }
}

In this scenario, the files will not be combined into one big file before being compiled. Just like the ino file, the .cpp file will be compiled separately. This can be seen in the console output

Compiling sketch...
/home/builder/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/builder/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/builder/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard /tmp/arduino-build-572AE98AD777D29D23A399151CB4C421/sketch/print.cpp -o /tmp/arduino-build-572AE98AD777D29D23A399151CB4C421/sketch/print.cpp.o
/home/builder/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/builder/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/builder/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard /tmp/arduino-build-572AE98AD777D29D23A399151CB4C421/sketch/new_sketch_1731134096956.ino.cpp -o /tmp/arduino-build-572AE98AD777D29D23A399151CB4C421/sketch/new_sketch_1731134096956.ino.cpp.o

Note:
Going into the details of include and cpp files is outside the scope of this post.

Just in case anyone thought it was false I am not sure how anyone might think it is false it is just my thinking that added that. :man_shrugging: Also thanks for taking the time to give me a bigger in depth conversation about files, I didn't even know these existed :slightly_smiling_face:.