Notifications
Clear all

Can't find Object I created

22 Posts
5 Users
1 Reactions
306 Views
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

I was following an example I saw online

and tried to create a timer object.

 

I created a Timer.H file and a Timer.ccp file and put them in a folder called Timer (see attached screen shots).

 

However, they do not show up in the sketch menu for included files for calling from the Arduino IDE.

In the attached picture it shows the directory structure. and the file names as well as the sketch menu. I closed and reopened the IDE

missing timer files

 



   
Quote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

I red online that I have to do it as a zipped file I tried that but get the following error message.  I have the files in a src directory and a property file.

error

 

 



   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2042
 

Hi @lom,

  Sorry, I am not sure what you actually doing. Your screenshot looks somewhat different the Arduino IDE I have. I guess you have rearranged it.

  When you say you 'created' the files, do you mean you have typed them in with an editor, etc.?

  If so, you want to introduce them directly (not zipped or anything) into the Arduino IDE, so that they are tabbed windows alongside your sketch ght.ino window. 

Did you use the (normally) top of screen menu item Sketch -> Add File ? to add each of the files to the project?

---------

I think the "need" for a zipped file is mistaken. Perhaps that referred to introducing a prebuilt library from a third party to the Arduino IDE?

Also, I note your files appear to be held in OneDrive, the Microsoft cloud. That probably isn't a problem, but only this morning I read this causes problems in a different microcontroller development environment, so if you still have issues, you might like to try keeping them on your local drive.

Best wishes, Dave



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@lom 

Where did you read online that you have to do it as a zipped file?

What project are you working on?

 



   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

@davee 

sorry for the confusion.

the first screen shot is a composite picture of windows file manager showing the files Timer.H and Timer.ccp and the directory they are in (top Red box) and a screen shot or the IDE showing that the files do not appear in the sketch library.

i googled what the problem might be and that is where it said it had to have zipped files which include a src directory, examples etc. 



   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

The project I am working on is just having leds blink at different rates. I found an example on line using the millis()function.

instead of creating the timer in each sketch I was trying to make a timer object I could include. 



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@lom 

Hope I am not out of line here if it is a desire to work it out yourself.

If so tell me to back off and I will do so and ignore the example below.

I see AI as no different than a google for answers as the information comes from the same place.

Like a google reference AI can still be wrong sometimes, just in a more confident way.

This is an AI example which I always make clear not pretending the code is mine.

// basic timer class

#ifndef TIMER_H
#define TIMER_H

#include <Arduino.h>

class Timer {
  private:
    unsigned long interval;
    unsigned long previousMillis;

  public:
    Timer(unsigned long interval) {
      this->interval = interval;
      previousMillis = 0;
    }

    void setInterval(unsigned long interval) {
      this->interval = interval;
    }

    bool isReady() {
      unsigned long currentMillis = millis();
      // is important — it safely handles millis() overflow (which happens after ~49 days).
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        return true;
      }
      return false;
    }

    void reset() {
      previousMillis = millis();
    }
};

#endif

 

 

// Usage in sketch

#include "Timer.h"

const int led1 = 5;
const int led2 = 6;

Timer timer1(500);   // 500 ms
Timer timer2(1000);  // 1000 ms

bool led1State = LOW;
bool led2State = LOW;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  if (timer1.isReady()) {
    led1State = !led1State;
    digitalWrite(led1, led1State);
  }

  if (timer2.isReady()) {
    led2State = !led2State;
    digitalWrite(led2, led2State);
  }
}

 

 



   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

Thanks l’ll try that



   
ReplyQuote
(@mike-parsons)
Member
Joined: 2 years ago
Posts: 29
 

@davee @Lom   Hi. Not sure if this is relevant, but I had some major problems with One Drive when I moved over to my new computer. Afraid my memory's a bit hazy on what happened. I created an Arduino folder in my documents and copied all the files from my old computer. I think  that whenever I opened a sketch via explorer, One drive would make a copy and keep it in its own file called Arduino, and my file was pushed down the tree. I ended up with like a ghost folder of my own stuff and I had to drill down into the C drive to find it. It took me quite a while to realize what was going on because sometimes the files were all there and other times they were not, depending on which Arduino folder I clicked on. Eventually, I got CoPilot to help me reassemble all my files back into one folder and then to get One Drive off my computer. I prefer to back up on to an external hard drive anyway. As I said, I'm not sure if this is relevant to your situation, so sorry if I've wasted your time, but it may be worthwhile having a wee look further down the tree.

Good luck



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
 

@lom 

I created a Timer.H file and a Timer.ccp file and put them in a folder called Timer (see attached screen shots).

However, they do not show up in the sketch menu for included files for calling from the Arduino IDE.

The problem you're encountering is due to the build process the Arduino IDE uses. Without going into the details of how and why, the short description is that the process is fairly rigid in where it looks for the files for the sketch. It looks in the sketch folder, and it looks in the libraries folders where the IDE installed any libraries. The include files (header files) must be in one of those two locations. Any source files in the sketch folder will be compiled. All other source files must come from a library (which will be compiled if necessary.)

Unless you're willing to create an Arduino library package and install it. you can simply copy the include and source file for the timer into the sketch folder.

instead of creating the timer in each sketch I was trying to make a timer object I could include.

Sorry, unless you make a library package, I don't think this can work. You might get away with it if you can create a "header only" library and use relative file naming, e.g.,

#include "../timer.h"

but even then, it's a fragile solution.

You can use a subfolder in the sketch folder to hold the library files and use relative file naming to access them, e.g.,

#include "mytimer/timer.h"

The IDE will automatically detect and compile any source files in the subfolder

HTH


The one who has the most fun, wins!


   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

@TFMMcCarthy

i think you are correct it wants a library package. I was following the Arduino how to create a library that did not work.

 

IMG 2618

Where can I fund how to create a library package. 



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@lom 

I never realized from your first post that you wanted to know how to write a library that others could install. That is too advanced for me! I might install a library written for the Arduino IDE to take advantage of code written by others that I couldn't easily write myself such as advanced servo control or controlling LED strips and so on.

From your first post I assumed you just wanted to learn how to write object orientated code.

Good luck with your software project.

 



   
ReplyQuote
 Lom
(@lom)
Member
Joined: 1 month ago
Posts: 27
Topic starter  

You were correct, it was just for my self I don’t know enough to code for primer people to use. But I did want to start to build up a modular library of objects I could call



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
 

Posted by: @robotbuilder

That is too advanced for me!

Stop saying this, you! Just stop it right now!

You're much more advanced than you give yourself credit for. And you're much more capable as well.

You just have to change your perspective from, "It's really hard!" to "It's a lot of fun discovering this stuff!"

Not lecturing. just sayin', is all. 🙂


The one who has the most fun, wins!


   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
 

@lom 

Working on it.

Can you provide the timer include file and source file? It will help.


The one who has the most fun, wins!


   
ReplyQuote
Page 1 / 2