Cannot save sketch!

Text copied from a post from user spierepf last year (closed, so I couldn't see the solution!).
This is exactly what just happened to me!

"I recently lost a sketch when I closed the IDE.
It asked me to "Save your sketch to open it again later."
I clicked "Save As..."
Entered a name and clicked "Save".
Everything closed as expected.
But when I reopened the IDE the sketch was not saved."

When I modify a line (a comment) & save, when I re-open the sketch, the line is missing. I just see what I had yesterday.
If I open the ino file with Notepad, add the comment, save, then open the ino file with IDE, it's OK.
To save a sketch now, does that mean I have to copy/paste the sketch into a txt file, then change the suffix? Not very funny!
Note that the latest version of the compiled sketch is running on the hardware, & restarts every time I open IDE.
I guess there's no easy way to reverse-engineer the code?

TIA for a fix!

RWK

Welcome to the forum

The topic that you refer to did not have a solution. @spierepf never responded to the questions that they were asked in relation to the problem and the topic (Cannot save sketch in 2.0.1) was automatically closed by the forum software 6 months later. The topic only has the initial message and a reply asking for more details that was never replied to

Hi @dick-san. I'm sorry about the problem with saving in the IDE.

Please add a forum reply here to tell us which operating system you are using (e.g., "Windows")?

I ask because I would like to give you the appropriate instructions for your operating system.

That is correct. It is usually possible to download the program from the Arduino board, but the program in the board is a binary, not the Arduino code it was compiled from. You can disassemble the binary to assembly language code, but translating that assembly into the original Arduino sketch would be very difficult and time consuming.

However it might be possible to recover your sketch a different way. Arduino IDE copies your sketch to a temporary folder during the compilation process. It is possible the full sketch is still stored in that folder. It will be in a folder under this location:

<system temporary folder>/arduino/sketches/

(where <system temporary folder> is the standard folder for temporary files used by your operating system)

You can try doing a search through that folder for the filename of your sketch.

Here is an example of a path on a Windows system:

C:\Users\<username>\AppData\Local\Temp\arduino\sketches\3A9AF02994178C2791F98B37AF39F346\MySketch\MySketch.ino.cpp

Note that a .cpp file extension has been appended to the original sketch filename.

Please note that this data is temporary. It can be overwritten by Arduino IDE or deleted by your operating system at any time. So if you are going to try to recover the sketch, you should do it as soon as possible. You should also be careful to avoid performing actions that would trigger the loss of the data before attempting the recovery:

  • Do not compile or upload your sketch in Arduino IDE.
  • Do not close Arduino IDE.
  • Do not shut down or restart your computer.
  • Do not perform any operations to automatically free up disk space.

If you find the sketch file in the temporary folder, you will notice Arduino IDE made some changes in the code. These changes are minimal so it will be easy to convert the code back to your original sketch. We can help you out with that if you have trouble.

Thanks for replying so fast!
I'm using Windows 10 Home, version 22H2.

Because I kept trying all kinds of things to save the sketch, there's no chance of finding something useful in the temp folder!

I had another problem, where I was getting a whole list of "redefined" errors. On searching for a solution, I saw that it is caused by having 2 sketches in the same folder (my case).
That's fixed now, & IDE is saving updates correctly.
Maybe the "save" problem was also caused by having the 2 sketches in the same folder?
RWK

I'm glad it is working now. If you experience the problem again, please let us know and we'll try to find what is causing the failure to save the sketch.

Because many sketches (especially the more simple introductory ones) consist of only a single .ino file, a common misconception is that the .ino file is the Arduino sketch. This is incorrect. An Arduino sketch is a folder, which may contain multiple code files forming a single program.

When you open one of these multi-file sketches in Arduino IDE or Arduino Cloud, an editor tab will open for each of the files of the sketch.

So if you have two .ino files in a single folder and each of those .ino files was intended to be its own complete program (rather than the two files being a single program when combined by the compiler), that does typically cause the "redefined" errors you experienced. However, even though such a sketch won't compile, Arduino IDE should not have any trouble saving it. So having multiple .ino files in a sketch is not expected to cause the saving problem you experienced.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.