Good morning all,
I have been tasked with reverse engineering an semi automated hole punching device that my company has been using the past 4 years or so. It consists of an UNO r3 arduino with an adafruit v2.3 motor shield. I am running a single stepper motor, a single servo motor, and a basic 2 x 16 rgb display. 4 on/off buttons control the unit. one for stepper advances to the right, one for stepper advances to the left. one for operating the servo, and one for disengaging the stepper which allows it to manually be moved into the starting position.
Unfortunately, the original designer left next to no information about the sketch used. It is also unfortunate that I have absolutely no experience with writing sketches.
What I would like to do ( if possible) is download the existing code from the working unit and upload it into the new unit. Everything from the older unit has been copied to create the new one.
I fully plan to dive into the sketch writing but simply do not have the time. If there is any information about how to download the sketch I would be extremely greatful beyond belief.
When powered up the Blink sketch runs as it should.
Why reverse engineer the old one? Do they want to change its functions?
Starting from the beginning using basic stepper tutorials, example code looks ok to me.
You should be able to copy the code from one arduino to another using avrdude on the command line, but there is no function within the IDE to do this. DO NOT do this on the working unit until you have successfully accomplished it on a couple of test UNO's, a mistake could erase the original code.
The UNO does not contain the sketch, only the compiled machine language code, so reverse engineering it back to a readable sketch is very difficult.
You can't pull the source code from the Arduino. You can pull the executable from the Arduino.
That will give you a binary or hex file that you can disassemble. After that, you're basically on your own to understand it; there might (!) be an utility to convert it back to C but it will definitely not look like a sketch.
Thanks for the timely response. Unfortunately, its looking like my best bet is to dig in and learn this stuff. Its a very simple operation where one button moves the stepper a set distance ( .10") CW, another does the same thing CCW. A third button engages the servo in a CW/CCW motion as long as button is depressed. A forth button releases the stepper so it can be manually moved to an arbitrary starting position. The video screen logs the number of moves +1, +2, +3, etc and -3, -2, -1. Shouldn't be too hard to figure out, right? Thanks again.
It's likely more effective in the long run to recreate the code, so that when some new functionality is required, you'll be able to add it.
But if you are pressed for time and simply want to replicate the existing device, you can do so as noted above. Just be sure that you heed david_2018's warnings.
Split the project into small parts. Reading a button is one, making a stepper move one step is another. Operating the display is another. Piece by piece You advance towards the wanted system and the functions You told look suitable for a beginner.
No changes at all. I begged em to just let me design a new one but you know "If it aint broke dont fix it". It's basically ( very basic) a hole punching fixture. stepper moves a platform CW/CCW. whem in position an arbor press is manually used to punch a .040" hole in thin walled heat shrink. If needed, the servo gives it a cutting motion. In theory the stepper motor is all that really has to do it's thing. None of the techs ever look at the video counter anyway. The servo is "iffy" in my opinion, too. Is there a specific stepper motor sketch you could recommend to get me started? The main problem I have is a total lack of programming knowledge and understanding. It shames me to say it, but there it is.
Everybody is born as a newbie, a beginner! No shame in that! It's much better You told that instead if swinging terminology and words You don't know the meaning of.
Do some tries, tasting some example code available in the IDE, topics like "stepper basics" etc.
First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to read the program from the Arduino board:
Plug your Arduino board that contains the program you want to read into your computer.
Select your board from the Arduino IDE’s Tools > Board menu.
Select your board’s port from the Tools > Port menu.
VERY IMPORTANT: Unplug your board from your computer.
File > Preferences
Check the box next to "Show verbose output during > upload".
Click the OK button.
Sketch > Upload
Wait for the upload to fail.
Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
That is the part of the command that tells it to write.
Replace that part of the command with the command that tells AVRDUDE to read:
-Uflash:r:readfile.hex:i
That will cause the read file to be named "readfile.hex", which will be saved to whichever folder you run the command from. So now the full command looks something like this:
If the paths in the command contain spaces, wrap the paths in quotes.
Plug your Arduino board into your computer.
Copy and paste the command from the text editor to the command line
If you are using to an ATmega32U4-based board (e.g., Leonardo, Micro, Yun), you’ll need to press and release the reset button on the board immediately after running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about a second after resetting the board before running the command.
Run the command.
Wait for the command to finish successfully.
Writing the file to a board
You can follow a similar procedure to write the file to another Arduino board. Remember that this file was compiled specifically for the Arduino board you read it from. You can’t use it with an Arduino board that has a different configuration. For example, if you read it from an Uno, it is compiled for an ATmega328P running at 16 MHz, and won't work correctly on a board with a different microcontroller or clock speed.
Generate model command
First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to write the program to the Arduino board.
Note: It doesn't matter whether the upload will succeed or fail. You get the command either way.
Plug the Arduino board you want to write to into your computer.
Select your board from the Arduino IDE’s Tools > Board menu.
Select your board’s port from the Tools > Port menu.
Select File > Preferences from the Arduino IDE's menus.
Check the box next to "Show verbose output during: [] upload".
Click the OK button.
Select Sketch > Upload from the Arduino IDE's menus.
Wait for the upload to finish/fail.
Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
In the command line, press Ctrl+V. This will paste the avrdude command.
If you are uploading to an ATmega32U4-based board (e.g., Leonardo, Arduino Micro), you’ll need to press and release the reset button on the board before running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about one second before running the command.
Press Enter to run the command.
Wait for the writing process to finish successfully.
The adafruit motor shield library has examples for driving a stepper motor, that will give you a working example.
It is not that difficult to transfer the code from one arduino to another, provided the original programmer did not get paranoid and disable reading from the flash memory. If you set the Arduino IDE preferences to show verbose output during compile and upload, then upload any code to an UNO, immediately after the text that shows how much memory the sketch and global variables use will be the command for avrdude to upload the sketch hex file. Copy that line, and it can be modified to read from flash memory and save to a file, instead of writing the sketch file to flash memory. Once you have that, you then modify the command again to write your saved file to the 2nd UNO. DO NOT, and I repeat, DO NOT try this with your working unit until you have done it successfully between a couple of test UNO boards - making a mistake can erase the original code.
I see pert has already posted the instructions while I was typing this.
Thanks for the info. I believe that's going to have to happen eventually. I'm wondering how long it would actually take to eat an elephant on bit at a time and I'm hopeful this comes together quicker.