Creating a library

I’ve got my program written, and have changed my coding to a modular style. I’m hoping now to take the next step and change everything I’m doing to Libraries to assist my coding now and in the future. Have been pouring over google searches on how to create libraries and turning my current code into a library, and have tried about 10 different times to create a library. Does anyone have a phenomenal guide, or would someone be willing to assist? Just one of my functions even so I could compare to what I’m doing and see why what I’m doing is wrong?

Here’s a simple function of mine if anyone wants to take a crack at it, which calls other functions from within and would give me a good idea of how my other functions would interact with the library

void Reset() {

  if (reset == 0) {   
    if (millis()==0000) {
      previousGrowLightsMillis=0;
      previousPumpMillis=0;
      Float();
      lightSchedule();
      reset = reset +1;
      previousCautionMillis=0;
      if (digitalRead(cautionLight == LOW)) {
        cautionLightStatus = "OFF";
      }
      if (digitalRead(cautionLight == HIGH)) {
        cautionLightStatus == "ON";
      }
      Serial.println("Board has been reset.");
      Serial.print("Reset value: ");
      Serial.println(reset);
     }  
  }
  else {
    if (millis() == 10000) {
    reset = 0;
    Serial.print("Reset value: ");
    Serial.println(reset);
    Serial.println("Reset ready for board rollover");
    }
  }
 }

code.txt (9.42 KB)

YouTube

    if (millis()==0000) {

I'm almost certain that this statement will never evaluate to true.

    if (millis() == 10000) {

How likely is it that this function will be called exactly 10 seconds after the Arduino resets?

I really don't think you are ready to make a library, yet.

@PaulS

  1. I created this function to help my timing if the board resets to 0, and cannot testify if it has/has not worked.
if (millis() == 0000)
  1. This function has ran just fine every time so far when I re-start the board.
if (millis() == 10000)

I really don't think you are ready to make a library, yet.

I agree/disagree. I'm still learning and very new to programming but I'm a very hands on learner, and comparing my 10 failed attempts to a correct version of the code that I'm actually playing with would be very beneficial.

I personally am trying to only ask for assistance after trying myself and hitting multiple failures and don't know which direction to go. I've definitely learned quite a bit from my attempts, but I know I'm missing a key piece of information and am unable to find it through online/YouTube tutorials. I can post one of my failed .h and .cpp files for you if you'd like.

If I'm that far away from being ready to create a library, would you be able to help guide me in the direction of what steps I should be learning first/the key pieces of information I need to seek out and learn prior to learning how to create libraries? I can provide my entire code to aid your evaluation of where I'm at in my coding knowledge if that would help.

The first step would be to have a working program that does what you expect it to do.

Looking at the very small snippet of code you posted, its hard to see that the code is doing anything meaningful millis() fo instance will only be at 0 once every 49.7 days or so.

srnet:
The first step would be to have a working program that does what you expect it to do.

Looking at the very small snippet of code you posted, its hard to see that the code is doing anything meaningful millis() fo instance will only be at 0 once every 49.7 days or so.

Through .ino files, my code does work, and I even have the programming running atm at home. I will attach the entire code to the main post to help ya'll understand what everything is doing

mrkyl:
2) This function has ran just fine every time so far when I re-start the board.

if (millis() == 10000)

Do I presume you're calling Reset() from loop() to test it? And you have no other code in loop?

So in that case, on a 16MHz board, you have 16,000 CPU cycles to actually catch the precise moment millis() == 10000.

Now try:

void loop()
{
  Reset();
  delay(5);
  //Or put any sufficiently complex bit of code here...
}

I wager you may not be so lucky...

If you want to check for reset, put your code in setup(). You will catch it every time, and not have false positives after 49.7 days (0x100000000 ms).

@pcbb

Here is my setup and loop:

void setup() {
  Serial.begin(9600);
  Serial.println("Enter 'Command' for a list of commands.");  // Initialize communication with user
  Serial.println();
  pinMode(growLights , OUTPUT);
  pinMode(circulationPump , OUTPUT);
  pinMode(cautionLight , OUTPUT);
  pinMode(waterLevelFloat , INPUT_PULLUP);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);


}

void loop() {
  if (CheckSerial()) DoCommand(inputBuffer); // Used to communicate with user through serial.
  Reset(); // Reset timing values and schedules of board timer rolls over to ensure proper operation
  Timing(); // Timing for light schedule & run intervals
  Float(); // Float and pump schedule

}

Also the entirety of my code is now attached to my main post via notepad file if that helps.

Also the entirety of my code is now attached to my main post via notepad file if that helps.

It does not. There are issues with the code that you first posted. If you address whose issues, and replace the original code, you make people that commented on the code look like idiots. I, for one, will no longer participate in a thread where the original post has been modified, for anything more than correcting typos.

Aside from your incorrect use of the millis() function that has already been mentioned, I'd say, in general, that library code needs to be self-contain. Don't count on using global variables from the main program as the library will be compiled as a separate file. Same goes with functions referenced in the library. External stuff needs to be passed in as function arguments. The code you've posted so far fails those criteria.

One question, why is the code written like this;

if (millis()==0000)

Is that the same as;

if (millis()==0000)
if (millis()==000)
if (millis()==00)
if (millis()==0)

?

why is the code written like this

Clearly, it's written that way to show that OP means octal 0, not decimal 0. 8)

@srNet
Yes, I should have written it as
If(millis()==0).
I cannot honestly say why I wrote it as "0000". Besides the fact that I wrote it this way when I was first starting out. I can only assume I never changed it as my code progressed since I received no compilation errors from it

@gfvalvo that makes a lot of sense. Thank you

@PaulS I understand your point. Posting my entire code though shouldn't really change the question I had asked. I was merely asking for assistance in creating libraries, and for good references. The function I posted was to provide a function that I desire to turn into part of a library, and if someone was willing to assist, I could see what I've been doing wrong trying time and time again. I posted the entirety of my code, as it was referenced that just having this snippet doesn't give you enough information to completely help as you cannot see what I'm trying to accomplish. Next time, I will make sure I include my attachment with the entirety of my code, and then just the snippet of the code I'm trying to work with in the description.
I am very proficient with relay logic and controls, but am quite new to coding. Hence why I've begun learning coding as a control board takes up significantly less room in a control box. Even though my improper use of millis() did not pertain to the question, I do appreciate when y'all pick my work apart, as I do always welcome constructive criticism. Now I know, that I failed and can do better on my future work

mrkyl:
Here is my setup and loop:

So as I thought: if those things you are doing in loop() take more than 1ms your millis() == 10000 check is FUBARed.