I am a mechanical engineer by education and have been working with electronics for some time, but barely any code. I am trying to automate a test fixture and I need to log results and do some arithmetic, which is why I am not planning on using a dedicated PLC. Plus, I like getting to work with arduino.
This may seem like a long explanation, but I just want to be thorough. I don't need help with all the coding, as I know some myself. I just a hand with some specific parts. Your time is appreciated.
Essentially, I want to use arduino to move a stepper motor to a particular location. The stepper motor is going to push on a handle and disengage an electrical contact, and then connect another electrical contact after a bit more forward movement. It will then retract to a limit switch, which will trigger some loop in the code and make the motor repeat it's action a total of 3 times before returning home.
I want to program the arduino to home the motor, extend it, and retract it. When the first contact disengages I want it to start an internal timer, and when the second engages I want it to stop the timer and record the result. I want it to loop 3 times, average the results, and display them on an LED screen. It must also measure the distance the motor moved between disengagement and engagement, and do the same.
Finally, I also want it to measure the "bounces" of the second (engaged) contact defined as such:
Count 1 bounce if second contact lasts <.005ms.
Count contact engaged if second contact lasts >.1ms
My main problems lie with how to program the ardunio to tell the program to loop at a specific part 3 times (with one push of the start button), and how to program the definition and counting of the "bounces".
The rest of the code I am familiar enough to handle. Any help is very appreciated. If any information is unclear or missing please let me know.
My main problems lie with how to program the ardunio to tell the program to loop at a specific part 3 times
The Arduino can't "tell the program" to do anything. The Arduino can execute some code when a specific condition is met. If that code involves a loop that iterates three times, so be it.
Count 1 bounce if second contact lasts <.005ms.
5/1000 of a millisecond is 5 microseconds. You can use micros() to record the time when the contact is made and when it is broken. Measuring the interval then isn't tricky.
PaulS thank you for the reply. I understand that the arduino simply executes code, that was bad syntax on my part.
Would I write a subroutine to loop 3 times, and just call the subroutine 3 time in a row? Or is there a better way to loop? I want to save each loops' time value and average them out. I am assuming I would write some code that saves each time value as a, b, and c respectively and then program the arduino to add all three and divide by 3, then log the result. I am really looking for the best way to loop it with that specification in mind.
As for measuring bounces, I will try using micros ().
I think you need to specify your problem more succinctly. I'm not sure if this is correct - but it will give you the idea
initializeStepperPosition
move motor so it presses a switch
move motor so it releases the switch
record the time (micros() ) on switch release
continue motor movement so it presses another switch
record the time (micros() ) when switch is pressed
do something with bounces (your description is not clear)
calculate results
repeat three times
This seems to be something that could be done using a FOR loop that repeats 3 times
I'm guessing that the stepper only needs to be initialized once when the program starts.
You will need to keep track of the state of the system - for example stepper extending, stepper retracting because within each movement of the motor you will need to be checking other things.
Another possibility, which may be essential, is to use an interrupt to capture the exact moment that the switches operate. Nick Gammon has a good tutorial on interrupts.
One thing you must completely avoid is the use of delay() to manage your timing. Use millis() as in planning and implementing and in several things at a time.
Your reply along with Paul's seem to be similar, recording time with micros(). I have been reading up on the function and it will work perfectly for what I am trying to do. My plan is:
// Define start of program. Define LCD Screen. Define All pins.
Motor moves, hits switch (Intiate using push button.)
int Time1 === micros() //when the arduino receives the input from the switch
Motor moves, releases switch
int Time 2 === micros() //when the arduino stops receiving the input
int TimeA === Time2 - Time1
Repeat 3 times
int Time === ((TimeA + TimeB + TimeC)/3)
If (some number < Time < some number) {
lcd.print("Good unit, " + Time);
} Else {
lcd.print("Bad Unit, " + Time);
} //END
I will rewrite to include it in the FOR() loop properly, but I need to work out exactly how to set my increment for this particular application.
You are right, each test only requires the stepper to be initialized once.
I will look at that tutorial. That seems like a possibility for sure. I will post after I check it out and think about it.
Thanks for posting, especially with all the links. I appreciate all the help I can get.
May I suggest you run the test 4 times because it is much easier for a microprocessor to divide by 4 rather than 3 for the purpose of calculating an average.
And note that micros() and millis() return unsigned long values and all of your variables associated with them should be defined as unsigned long.
I think you are seeing the beginning of the possibilities of automating testing.
Editorial: All the really interesting jobs are multidisciplinary!
So think about learning 'C' and Arduino as tools you will have for future jobs and projects. Look at "Mechatronics"..
You will need to understand arrays of data, data types, arithmetic operators etc. etc. As you are a professional, I would suggest a book like "Beginning C for Arduino:" On Amazon HERE:
DISCLAIMER: I am the technical editor for the upcoming second edition of this book....