Start and stop a stepper motor with two photo eyes

Arduino UNO
Hi I am new to coding, i have pieced together code from two different codes and with a lot of time got it to work.
I can not solve the last problem with the code, I will try to explain the best I can.
The code runs a stepper motor with a Analog (10k pot) speed control.
I have two photo eyes, EYE #2 to pin 2 and EYE #3 to pin 3.
When I trip EYE #3 (OFF to ON to OFF) the motor runs, then I trip EYE#2 the motor stops.
But if I trip EYE #3 and hold it on (off to on) then trip EYE #2 the motor will not stop.
What I need is to trip EYE #3 (OFF to ON) the motor will run and then the input for EYE #3 is ignored,
so when I trip EYE #2 the motor will stop, also EYE #2 needs to reset EYE #3 to allow
it to start the motor running for the next cycle. The problem is the EYE #3 is seeing the object when the motor
needs to stop. The EYE #3 is clear of the object when it needs to run the motor again.

[code]
[int spd = A0;

int driverPUL= 7;
boolean running= false;

const int buttonPin2= 2;       // Photo eye 2
const int buttonPin3 =3;       // Photo eye 3

int buttonState2= 0;
int buttonState3=0;
int pd = 500;
 
void setup() {
 
  pinMode(driverPUL, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3,INPUT);
    }

void loop() {
  buttonState2= digitalRead(buttonPin2);
  buttonState3= digitalRead(buttonPin3);
  if (buttonState2==HIGH){                  // Photo eye 2 stops stepper motor
     running=true;
     
  }
  if (buttonState3==LOW){                   // Photo eye 3 starts stepper motor
    running= false;
   
  }
  if (running){
    digitalWrite(driverPUL, LOW);
 
  }
  else{
    digitalWrite(driverPUL,LOW);
 

pd= map ((analogRead(spd)),0,1023,800,100);

digitalWrite (driverPUL, HIGH);
delayMicroseconds (pd);
digitalWrite (driverPUL, LOW);
delayMicroseconds (pd);

}
}  [code]

[/code]

Are you sure you have the closing } for the ELSE in the correct place - the code formatting suggests not. Use the Auto-format tool to indent code consistently.

Your description is very confusing because you refer to EYES but in the program they are called BUTTONS

A description of what the project is intended to do will make your questions much easier to understand.

...R

Nice schematic. FYI you can download KiCad for free and get a full blown schematic capture program. You have made a good start, however I would suggest you spend time with each of the codes you want to combine, getting them to work and understanding what they do. At that point combining will not be such a monumental task. Do it one piece at a time and get it to work. When you run out of pieces the project will be finished!

What is this NPN and PNP photoeye business? I never heard of that...

You're setting running = false based on 'is EYE3 == LOW'. Set running = false based on the *change of state * of EYE3 from HIGH to LOW.

(IDE -> file/examples/digital/state change detection)

This will make EYE3 behave like a very fast off-on-off.

If the output voltage from the PNP switch is 12V, then you are putting 5.7 volts on pin 3 and causing more than 3mA to flow through the pin protection diode.
Post datasheet for EYE # 3.

JCA34F:
If the output voltage from the PNP switch is 12V, then you are putting 5.7 volts on pin 3 and causing more than 3mA to flow through the pin protection diode.
Post datasheet for EYE # 3.

Yes I know, but I think that the volts are 12 volts to pin 3 through a 2k resistor this controls the current. I have had the Arduino uno on for more than a hour, no problems. I know you can put 24 Vdc through a LED with a max input volts of 2.2 volts as long you use a 1k resistor. Also the Arduino max input current to pin’s is 20ma.

Robin2:
Are you sure you have the closing } for the ELSE in the correct place - the code formatting suggests not. Use the Auto-format tool to indent code consistently.

Your description is very confusing because you refer to EYES but in the program they are called BUTTONS

A description of what the project is intended to do will make your questions much easier to understand.

...R

Yes I know this is my project and I have about 120 hours trying to find the answer searching the forum for a one shot input or a set and reset input. There are 100s of ways to do this. I have no clue on what program language to use. It could take me years to learn all the language and how to set it correctly. I just thought that some one with years of experience that knows could give me a direction. To answer your question I just left the button words but it could say photo eye, on the program it is labeled ( button pin 2. // photo eye 2) about the ELSE not sure where it goes I just coped this program. It is very hard to explain how exactly is supposed to work. I will tell you the program does work very well, I have tested for hours the two photo eyes start and stop the Stepper motor, the 10k potentiometer controls the speed very well. There is just one glitch in the program that I can not solve. Eye 3 starts the motor but if I hold my hand in front of the eye and try to stop the motor Eye 2 it will not stop unless I take my hand away from eye 3. Like I said very hard to explain.

aarg:
What is this NPN and PNP photoeye business? I never heard of that...

Sorry this is a transistor output same as high or low in the Arduino. NPN how I remember is the two “N” is the output of the sensor or photo eye or all electronic inputs or outputs. N = 0 volts or - or low or sink. The PNP is the opposite were “P” is 5 volt or + or high or source, just different ways to say the same thing. I hope this helps.

Rddesign:
It is very hard to explain how exactly is supposed to work.

I did not ask you how the program is supposed to work.

What I would like to know is what the project is intended to do - and you must know that. For example is it a project to wash a car or to hang wallpaper, or what?

...R

Your variable names are confusing. Running is used to control whether the stepper steps, but running is false when it does which seems odd. Use of button when you mean eye has already been mentioned, but again it adds to the confusion when trying to understand your code.

I realize you cobbled this together from examples, but a bit of find and replace on the names would help.

Ok what is it for, it is to control a Label machine I did not know that it would help in the programming. Photo eye 3 see the quart bottle moving down the conveyor, this starts the stepper motor feeding out the label to meet with the bottle.
Photo eye 2 is a label sensor that reads the gap in the label and stops the stepper motor from feeding the label further.
This is a label machine that was given to me from a friend that the electronics are old and not replaceable. Just trying to
Get it to work again. If you look at YouTube label applier this may help.

wildbill:
Your variable names are confusing. Running is used to control whether the stepper steps, but running is false when it does which seems odd. Use of button when you mean eye has already been mentioned, but again it adds to the confusion when trying to understand your code.

I realize you cobbled this together from examples, but a bit of find and replace on the names would help.

Robin2:
Are you sure you have the closing } for the ELSE in the correct place - the code formatting suggests not. Use the Auto-format tool to indent code consistently.

Your description is very confusing because you refer to EYES but in the program they are called BUTTONS

A description of what the project is intended to do will make your questions much easier to understand.

...R

Ok let’s just forget about the photo eyes and use the code descriptions . I push button 3 then release the stepper motor runs “say around 500 rpm”. Then I push button 2 then release and the stepper motor stops. This all works great, but not exactly what I want this is the problem I can not solve. I push button 3 and hold it down, then I push button 2 the stepper motor will not stop. So I need to have button 3 as a one shot if you hold the button down the Arduino Only see the input as a momentary this way button 2 can stop the stepper. Only when you release button 3 and push it again does it input to the Arduino.

Take a look at the IDEs state change example. It sounds like you need to detect when button 3 BECOMES pressed, not when it IS pressed.

Rddesign:
Ok what is it for, it is to control a Label machine I did not know that it would help in the programming. Photo eye 3 see the quart bottle moving down the conveyor, this starts the stepper motor feeding out the label to meet with the bottle.
Photo eye 2 is a label sensor that reads the gap in the label and stops the stepper motor from feeding the label further.

That's a huge help.

To relate that to the program ...

Button3 is what detects the bottle coming into range of the label system.
Button2 is what detects when the end of a label is detected.

But the description in Reply #12 is still somewhat at odds with this because it refers to you pushing buttons.

Going by what I have quoted here it seems to me that when button3 is triggered the stepper motor should run (to feed the label) and when button2 is triggered it should stop.

Why is there a need to hold a button? And if the button really is a photo detector, how can it be held?

I hope you are getting the idea that using the proper words to describe the parts will make the whole thing easier to discuss and to debug. If that means changing the variable names in the program then do it.

...R

Yes it does say buttons but wildbill was confused that I left in the code buttons but defined the buttons as photo eyes. So to make it easer
I said forget the eyes and I will use buttons. They work the same way. Now to answer your question the photo eye 3 to start the stepper and feed the label, eye 3 can see the bottle after the label needs to stop ( like holding the button ) Then it will feed out two labels. If eye 3 is ignored by the Arduino after the first input ( OFF to ON to OFF) then only one label will be dispensed.

Rddesign:
eye 3 can see the bottle after the label needs to stop ( like holding the button ) Then it will feed out two labels.

So how does Eye3 detect that there is a change of bottle?

My guess is that you should start the motor when Eye3 changes from no-bottle to bottle. That way it won't have any impact when the label has been dispensed. Something like this pseudo code

previousBottleDetectorState = bottleDetectorState;
bottleDetectorState = digitalRead(bottleDetectorPin);
if (bottleDetectorState != previousBottleDetectorState and bottleDetectorState == value for detecting a bottle) {
   code to start Motor;
}

And note how there is not an eye or a button in sight

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.