I made a sketch, but then I lost it. However, I uploaded it to the Arduino before losing it. Is there any way I can get it back?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
It should be possible as long as the security bit isn't set. This question was asked on EE a while back.
But you won't get the Arduino code you wrote back. The code is compiled into assembly and you'll have to convert that back to C yourself.
This answer doesn't directly answer the question, but still will result in the same end result.
The Arduino IDE uses temporary directories to store build files, including the original sketch as well as the HEX and intermediate files.
On a Mac, these are in /var/folders by default, and on a Windows machine they are in …\Local Settings\Temp\ (which depending on the version of Windows could be in several places).
I've found that especially in Windows, these build files don't get deleted when you close the sketch or IDE, so they may exist on your machine for far longer than you would think.
Also, by default, Crashplan, Backblaze and Time Machine back-up these locations, so even if they have been deleted, they may still be in a backup.
The advantage here is that you will recover C code rather than ASM.
I think there is another way to do this, without extracting the code from the Arduino board. Remenber the date you create the sketch, and look for it in C:\Users\User\AppData\Local\Temp\ (For example : C:\Users\User\AppData\Local\Temp\untitled4390292704786567977.tmp\sketch_jan19a ). You could find your unsaved code unless it was deleted by some "freeing space program". Good luck!
You can download the hex dump of the file, but there is no easy way to get all the C++ code and comments. If you can understand assembly, you could recreate the program, but that would take a while. There are dissasemblers available for some devices, i don't know about arduino.
Unless your objective is to simply copy the identical code to another device, the simple answer is: no
As has been pointed out in other answers, you can turn the hex code on the device back into assembler code.
For example (once you have copied the code from the device back onto your disk):
Run on one of the bootloader files I get these results (in part):
As you can see, not particularly helpful for reproducing what your sketch did. There are likely to be one of two scenarios:
In the "short sketch" case, you are better off just rewriting from scratch. That would be faster, almost certainly. In the "long sketch" case - it just isn't worth it. None of the variable names are preserved, and the way the compiler optimizes code, even the structure of the code would be hard to determine.