Haven't had a chance to look through your code yet, gotta run... but glad you got it working. I'll check it out as soon as I get a chance.
Cheers
Congratulations Michael on your problem-solving. Could we see a video of your train elevator in action?
Tom
So - it's been a steep learning curve and a months-long, often frustrating process to get to this point. But my project works well now, and I have a lot of satisfaction in being able to say, "I designed and built and wrote the code for this gizmo!"
Great to hear!
Just like golf, programming can be very frustrating, but can also offer a lot of fun and enjoyment when one gains knowledge and confidence, and then starts to look at ways to refactor and create cleaner, more elegant code and solutions to problems.
Having said that, I had a quick look at what you've posed above, I'd like to offer the following tips - You are free to ignore them, of course.
1) The Arduino library offers some really neat functions, that are too often overlooked, but they can really help to improve ones code for not only elegance, but also readability - I've used the constrain() function to refactor your "accel()" function as follows:
void accel() { if (millis() - previousAccelMillis > accelInterval) { accelValue += accelIncrement; analogWrite(MDPWM, constrain(accelValue, 0, maxSpeed)); previousAccelMillis = millis(); } }
Here I employ the "constrain()" function, doing away with the conditional "if()" statement. The beauty of this function is not only that it cleans up to code from four lines to two, but it is also self documenting as to it's intent, simply by way of it's function name, thus no need for a comment.
2) It's said that functions should do one thing only, and do that one thing well. I agree with this mantra in principle, so I would refactor it down even further as shown in the following example:
void accel() { accelValue += accelIncrement; analogWrite(MDPWM, constrain(accelValue, 0, maxSpeed)); }
As you can see, I have removed the timing aspect of the function using the "millis()" function, because I think that should more likely reside outside of this function, but as a wrapper over any required code in the main loop() function, though as noted earlier, you would have to redesign your main program code, should you choose to take that route.
Anyhow, hope this gives you something to think about in future.
Cheers.
@hayttom In my last posts, I tried to include a video of the actuator in action using the 'my media' button, but the 23-second-long video exceeded the 10 MB file size limit. Is there some other trick to posting videos here?
Hi Michael,
I don't have a good answer to your actual question but what about posting a link to a Youtube video? Here's mine.
I hope your machinery is quieter than mine.
Sincerely,
Tom
Tom
@frogandtoad "...cleaner and more elegant code..." is something that I welcome. Thanks for the suggestions. I didn't know about the constrain function. I will definitely modify both my accel() and decel() functions using constrain. Also, I have seen '+=' in other people's code, but I didn't understand what it did. Now I know and will be using this operator in several places. I learned something new today... thanks to frogandtoad!
@hayttom Maybe click here for video. This is a link to my Google Drive. As you can see/hear, my mechanism is noisy as well, but it should get the job done. What you see is just the actuator. I have yet to assemble the elevator's track boards and cable/pulley/counterweight system. The track boards are tear-drop-shaped loops that are made up of several sections glued together. I cut the track board sections on my CNC router from 4x8 sheets of OSB and glued them together. My next step is to lay and wire the track on the boards. I'll also be installing IR detectors under the tracks before assembling everything.
BTW - if you are using stepper motors to power your projects, have you explored the AccelStepper library? It can be found in the Arduino IDE Library Manager. It can give smooth acceleration/deceleration to your transfer table and lift projects.
Michael, I like the sound of your mechanism more than mine!
I will look into the AccelStepper library. By using Bill's black box philosophy I can do that independently of my other devices.
Tom
Tom