Sensor programming

Hi everyone, I am pretty new at this, I hope someone can help me learn a bit more.

If I have two sensors, say one for metal and one for non-metal and the output of these two sensors are inputs to drive a stepper motor one way or the other, for a sorting application. When the motor drives I don't want the sensors to effect anything until the arm attached to the motor goes to its position and then back again. I don't want the arm to be switching the sensor as it moves.

I was thinking about the delay function but I think that would delay the entire program for the set time. Is there any way of 'latching' on the output to the stepper motor for a set time so it could complete its action and return back to its starting point to identify the next object? Or is there a way to take the sensors out of the equation for a set time without effecting the output to the stepper motor. Sorry if this is a stupid question or has been covered before.

Any help at all would be much appreciated.

Thanks

You should write down in detail what you want your machine to do in the order you wan it done. That will form the basis of your sketch. For example:

If the "metal" sensor has triggered: Run the "Metal" stepper to the right for 1/4 turn. Wait there three seconds Run the "Metal" stepper to the left for 1/4 turn.

If the "non-metal" sensor has triggered: Run the "Non-Metal" stepper to the left for 1/4 turn. Wait there three seconds Run the "Non-Metal" stepper to the right for 1/4 turn.

You might want to add some limit switches so you can move motors until they reach the correct position.

Thanks very much for your reply. That is a good way of thinking about it.

Take one sensor for example, an inductive one. So a metal ball gets sensed so I get an input to the Arduino from the inductive sensor. This then tells the output to the stepper motor to drive to a defined point. If the arm that moves the metal ball is itself metal and switches the sensor as it passes it will it effect the output? Or does the output finish its task before reading another input?

I am worried that the sensor might be activated by something other than the ball and the motor would always stay in one place, get stuck.

If the arm that moves the metal ball is itself metal and switches the sensor as it passes it will it effect the output? Or does the output finish its task before reading another input?

You are the programmer so you get to decide what the stepper does and when.

Thank you for your reply, I am not sure if I am explaining my question very well.

I want the arm to go one way and then back to its original position which is fine but I was wondering will the sensor give nuisence switching. I want the sensor to give the input then drive the motor then return to the original position. The arm will pass over the sensor so the sensor will go high and low a few times from the mechanical parts moving past it as it dumps the ball.

Can I use the delay function to stop the sensor giving the input again after its first initial sensing? Or is this not how it works? I am thinking about it from an electrical relay/PLC background so you would need to latch the output to the motor after the switch makes the first time so the motor doesn't keep switching on and off and would probably use a limit switch to tell it it has reached its position and drive back the other way.

Sorry if this is a stupid question, if someone could link some info on this it would be very much appreciated. I am just unsure where to find it

will the sensor give nuisence switching

Only if your code lets it.

Can I use the delay function to stop the sensor giving the input again after its first initial sensing?

The strictly correct answer to this is yes, but it would do you no good. delay() stops anything happening in the program, hence the answer above, but it would also stop the stepper moving and switch activation being sensed.

What you would do is use millis() for timing. Record the start time of an activity using millis(), such as starting the motor, and disable the sensor input. Then, every time through loop() check to see if the required period has passed. If not go round loop() again, perhaps checking limit switches. When the required period has passed stop the motor, maybe reverse it to go to its home position and start timing again. When the required period has elapsed stop the motor and re enable the sensor.

Limit switches and running the motor until the limits are reached is a better solution than relying on timing. So, disable the sensor, start the motor, run to the limit, run back to the home position and re enable the sensor.

Don't use the delay() function because it prevents the Arduino doing other things during the delay period. Look at how time is managed in a non-blocking way using millis() in several things at a time.

You may also be interested in stepper motor basics

...R

UKHeliBob: What you would do is use millis() for timing. Record the start time of an activity using millis(), such as starting the motor, and disable the sensor input. Then, every time through loop() check to see if the required period has passed. If not go round loop() again, perhaps checking limit switches. When the required period has passed stop the motor, maybe reverse it to go to its home position and start timing again. When the required period has elapsed stop the motor and re enable the sensor.

Limit switches and running the motor until the limits are reached is a better solution than relying on timing. So, disable the sensor, start the motor, run to the limit, run back to the home position and re enable the sensor.

Great, thank you for that, I will look into that more and try come up with a solution.

Robin2: Don't use the delay() function because it prevents the Arduino doing other things during the delay period. Look at how time is managed in a non-blocking way using millis() in several things at a time.

You may also be interested in stepper motor basics

...R

Great, thank you for that, I will look into that more and try come up with a solution. Thank you very much for pointing me in the right direction. Much appreciated.