How do people manage different versions of a sketch?

As far as I can see the Arduino IDE expects to find a sketch called "myprog.ino" in a directory called "myprog".

If I evolve myprog.ino into myprog2.ino that seems to require creating a new directory called myprog2 which gets very tedious after a few evolutions of the code.

I'm wondering if anyone else has a better way to manage this process so that all the related sketches can be in the same directory but I can choose whichever version I want to compile and upload. Often when I'm at version 4 it's useful (or necessary) to go back to version 2 and start over.

...R

One way to do this is via #ifdef and/or using const int at the beginning of the program. You then edit the program to enable which features you want to use.

#define DO_ITEM1 0
#define DO_ITEM2 1
#define DO_ITEM3 0

#if DO_ITEM1
void item1 ()
{
  // code to do first item
}

#else
#define item1()
#endif

#if DO_ITEM2
void item2 ()
{
  // code to do first item
}

#else
#define item2()
#endif

#if DO_ITEM3
void item3 ()
{
  // code to do first item
}

#else
#define item3()
#endif

void loop ()
{
  item1 ();
  item2 ();
  item3 ();
}

For example, I use this in my telegraph key shutter release program, which I have a telegraph key act as a button. Because my DSLR takes some time to focus when in live view mode, I needed to send out the focus signal to the camera while the telegraph key is held down. When the key is released after being held down, the microprocessor will then turn on both the focus and fire signals on at the same time for a short period. The focus and fire signals feed into opto-isolators, which in turn are connected to the wired shutter release of the camera.

Sometimes I have a potentiometer hooked up to vary how to fire both the focus and fire signals, other times, I just want to use a preset amount, and I use #if to select the action (MrmButton_analog is my wrapper around the analogRead function, MrmButton_nop just returns the same value).

After the camera is fired are the special effects. Sometimes I use a buzzer to spell out FIRE in morse code, sometimes I blink one of the lights to send FIRE in Morse code, and sometimes I don't bother with the special effects. I have a switch to control whether to do the special effect or not, so I can disable it in the field if it is getting annoying.

I also have debugging print/printlns controlled by the const int do_print at the header. If the value is 0, the compiler will optimize away everything inside of:

const int do_print = 0;

void loop ()
{
  // do something interesting
  if (do_print)
    {
      Serial.println ("I'm here");
    }
  // more stuff
}

Now, a more complex method would be to separate the code into separate header files in the same directory, and use #include "..." to include the code. You would use #if or just comment out the #include to select whether to include the header. A simple approach would be be to keep the headers in the same directory.

You could also move to creating libraries and provide each version as a separate class. I do that with my button wrapper library, where I have wrappers for different types of buttons. For example, I have different methods for digital buttons that use internal pullups or don't, and the wrapper function returns a consistent 0 for the button being off and 1 for it being on, even though when you use a pullup, it returns LOW instead of HIGH. As I mentioned before, I have a function for reading from a potentiometer, and one for returning a constant value.

I also keep all of my code under a source code control system, and I can go back to any particular version of the source. I use cvs to keep all of my code and system config files, and I keep the files on my external web server, so any of my home or work systems, I just do cvs update to keep the files updated across my systems.

You can download my ShutterRelease code and the button library here:

I use Git. GitHub is good for online sharing and collaboration.

Thanks Michael - but I don't want to have options within a piece of code. I want to have completely separate, and fully self contained programs and be able to choose to upload version1 or version2 or version3.

Thanks Jack - at this stage I have no interest in collaboration or sharing. And in another context I have looked at Git but I could never find any documentation that enabled me to understand it.

...R

I haven’t gotten into this issue yet, but expect to at some point. My first thoughts would be to use a deep folder structure. I’ve already experienced that you can have a sketch’s folders at least one hierarchical level away from the root of your sketch folder. Probably even deeper because of the depth of the user library examples folder.

Not exactly ideal, but should be doable for the Lone Programmer. :smiley: And less complicated for us hobbyists than running and maintaining a local CVS. :wink:

Do you have that integrated with the IDE, or are you manually checking data in/out every time?

Robin2:
Thanks Jack - at this stage I have no interest in collaboration or sharing. And in another context I have looked at Git but I could never find any documentation that enabled me to understand it.

There is no requirement to share or to use the GitHub site, nothing needs to ever leave your PC. Well you would want to use the site to download the software. I learned mostly from Pro Git, which is available online for free in several formats, or in hardcopy form:
http://git-scm.com/doc
http://git-scm.com/book

KirAsh4:
Do you have that integrated with the IDE, or are you manually checking data in/out every time?

I'm not aware of a way to integrate it. Checking out is not necessarily needed every time, although switching branches, making a commit, pushing repos, etc. requires use of the Git GUI tools or command-line interface.

Robin2:
As far as I can see the Arduino IDE expects to find a sketch called "myprog.ino" in a directory called "myprog".

If I evolve myprog.ino into myprog2.ino that seems to require creating a new directory called myprog2 which gets very tedious after a few evolutions of the code.

I'm wondering if anyone else has a better way to manage this process so that all the related sketches can be in the same directory but I can choose whichever version I want to compile and upload. Often when I'm at version 4 it's useful (or necessary) to go back to version 2 and start over.

...R

Move your program to the sketchbook folder. Load it from there and just hit save as (new name) to save different versions. All the versions are then saved in the sketchbook folder.

[quote author=Jack Christensen link=topic=167926.msg1250972#msg1250972 date=1369344228]
I'm not aware of a way to integrate it. Checking out is not necessarily needed every time, although switching branches, making a commit, pushing repos, etc. requires use of the Git GUI tools or command-line interface.[/quote]

Hmm, would be nice if the IDE had a way to call an outside program, much like AS6 can (when configured.)

I have a folder of different projects.
Within each project folder, I save the sketches for that project.

I have experimented based on the advice so far. Based on Sembazuru’s suggestion I have created a directory (called TSV) in my sketchbook directory in which I have created a directory called TestSaveVersions and in that directory (have you fallen asleep yet?) I have created a file called TestSaveVersions.ino.

Then, as suggested by Henry_Best i can do a “save as” which saves the project with a new name (TestSaveVersions2) in a new directory in the directory TSV.

This is neater than having all the versions in the top level sketchbook folder but it still means a proliferation of directories - which I don’t like.

I would much prefer to have all the .ino files in one directory - but that doesn’t seem to be an option - unless someone has another suggestion.

…R

In my Arduino preferences, I have it set to ArduinoProjects.
Then there is a folder for each project.
Each project then has multiple versions of sketches.
The project folder is listed when I open File:Sketchbook, and then the various drafts under that.
If you have all .ino's at the same level, then File:Sketchbook opens a list that is too big and you can't see them all.
Most of the time I just use File:Open and browse to what I want anyway because I can't recall where I left something.

Thanks Crossroads. I can't figure out from your post whether your system is different from the one I outlined in my previous post. Could you provide more information please?

...R

Maybe we’re saying the same thing then? Here’s an example.