Optical and Proximity sensor help

I started a new project and decided to use and IR optical sensor for a start stop switch instead of the standard cherry switch to avoid all the pullups and debouncing code. The problem I'm having is the registering of the high and low, in order for me to make a stepper move I have to hold the switch down to break the IR beam the entire time I want the stepper to move and I'm not sure how code it so I don't have to hold it the whole time.(should have stuck with what I know lol).

The second problem has to do with the Proximity sensor and counting the highs and lows, I previously used a hall sensor to do this but with all the extra steps I went through to use it I thought the Proximity switch would be an easier thing to incorporate. The counting doesn't seem to work the same between the two, if I pass by the hall sensor the code will add a count in the serial screen and it only adds a count when the magnetic field changes but when I try this with the Proximity sensor if I leave the triggering arm in front of the sensor it will continuously count until I move it away from the sensor. When I try to incorporate the counting code into the rest of the program it doesn't count at all either so I have something else interfering.
If anyone can nudge me in the right direction it would be appreciated.


//FOOT SWITCH GLOBALS
const int footswitch = 17;
int footswitchState = 1;

//COUNTER 
int staplercounter = 0;
int staplercountercurrentState = 0;
int staplercounterpreviousState = 0;
int staplerSensorcounter = 22;

//Proximity placement sensor
int SwingArmHome = 18;
int SwingArmDown;


#include <AccelStepper.h>
AccelStepper SwingArm(AccelStepper::DRIVER, 32, 33);
AccelStepper Stapler(AccelStepper::DRIVER, 34, 35);


void setup() {
  pinMode(footswitch, INPUT_PULLUP);
  pinMode(staplerSensorcounter, INPUT_PULLUP);
  pinMode(SwingArmHome, INPUT_PULLUP);

  SwingArm.setMaxSpeed(1000);
  Stapler.setMaxSpeed(1000);

}

void loop() {

  Counter();
  
  SwingArmDown = digitalRead(SwingArmHome);
  staplercountercurrentState = digitalRead(staplerSensorcounter);
  footswitchState = digitalRead(footswitch);

  if (footswitchState == LOW && SwingArmDown == HIGH) {
    SwingArm.move(1);
    SwingArm.setSpeed(600);
  }

  if (footswitchState == LOW && SwingArmDown == LOW) {
    Stapler.setSpeed(800);
    Stapler.move(200);
  }
  SwingArm.runSpeedToPosition();
  Stapler.runSpeedToPosition();

}
void Counter() {
  staplercountercurrentState = digitalRead(staplerSensorcounter);     //used to time all events
  if (staplercountercurrentState != staplercounterpreviousState) {    //check the count and add 1
    if (staplercountercurrentState == 1) {
      staplerSensorcounter = staplerSensorcounter + 1;
      Serial.println(staplerSensorcounter);
    }
  }
  staplercounterpreviousState = staplercountercurrentState;
}

Optical sensorhttps://www.amazon.com/gp/product/B07JMDLD84/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

Proximity
https://www.amazon.com/gp/product/B073XDWWHW/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

For your IR switch, you have to use code similar to your Counter() function. It is based on the state change detection example. In the IDE, File->examples-02.digital->state change detection.

You have a Boolean variable, and each time you detect a HIGH to LOW, you toggle that variable. If the variable is true, you are moving, if it is false you stop.

As for the proximity sensor, it says VCC = 6-36 volts so you can't run it from the same 5V as your arduino. Do you have the NPN normally-open version? With the pin declared as INPUT_PULLUP, it will read HIGH when not triggered and LOW when triggered. You have to do the same state change detection again for this pin if you want to count things

Thanks for the reply, I know it was wishful thinking to use the same code as I did before but it was worth a shot. lol
I will work on new code per mentioned example.

I power the Proximity with a 12V power supply and the counter code seems to work when it is by itself but when I add it to the movement code for the steppers it doesn't want to count in the serial window for some reason.
It's a NPN style proximity switch which is why I use the INPUT_PULLUP.

Open collector, you mean?

I thought that's what NPN meant, open collector, as opposed to PNP?

No, absolutely not. NPN and PNP only describe the polarity of a BJT. Actually, a PNP transistor can be used in an open collector configuration, except that the load must derive from a negative voltage, as opposed to the positive voltage that an NPN open collector needs.

I seem to be having trouble reading one of my proximity sensors, I have them wired with the Brown wire going to 12V power supply, Blue wire to 12V GND and black wire to digital pin on the Arduino with Pullup Resistor activated. I have all three wired the same way but one just doesn't register for some reason so I swapped it out for another one I had and it doesn't register either no matter what digital pin I try.
What are the odds that 2 of the 4 I bought are dead, not sure what to do other than try a different brand?
Should I check something else first?

It's a struggle to put together a clear mental image from a description like that. Please make an accurate drawing.

This is what I have.
npn_no

Do you have all your grounds tied together? You mention a 12V ground. Is the 12V supply also powering your Arduino? A complete schematic (even hand drawn) would be good.

By the way, a your resistor labeled "internal pullup" is not in series with the pin so that image is not accurate.

I got it figured out, I assembled this a few months back and didn't have the correct Din rail connector and used one that grounded directly too the rail but not back to the 12V supply. Everything is working properly with that fix.
Those pesky grounds.
Thanks for the help.

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