Loop certain number of times then do action

Now I am even more confused because nothing in the sketch that you have posted outputs anything to cutPin

You could do something like this

void loop()
{
    int relayState = digitalRead(relayPin);
    if (relayState == HIGH)
    {
        for (int i = 0; i < numberOfSteps; i++)
        {
            digitalWrite(stepPin, HIGH);
            delayMicroseconds(100);
            digitalWrite(stepPin, LOW);
            delayMicroseconds(100);
        }
        digitalWrite(cutPin, HIGH);
        delay(100);
        digitalWrite(cutPin, LOW);
    }
}

but I don't know what you actually want to do to cutPin

for me it just looks like there is missing a 10kOhms pulldown resister from pin 2 to ground

I see the problem:
Your number of steps exceeds the size of an int.
Make numberOfSteps a const unsigned int.

Good spot @jim-p

I actually ran the code.

That's cheating !

I will be trying that once I order a pulldown resistor

Why wait.
You can try it now.

Still not clear what comes from the PLC relay. Is it just a contact closure to ground or does it actually output 24V and 0V?
You may need actually need apullup and not a down

Pullup resistors are included for free with the purchase of an Arduino:

https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial/

I ended up finding a 5 post relay and got it to work

I would be interesting in seeing your code and schematic

Me too , there is still a lot of confusion.

The problem was the code not the relay, so I can't see how a different relay would help

check what size of number can be stored in which variable type.
This also depends on the controller you are using. On an AVR based 8bit controller like used on an Uno R3 the maximum value for int is much smaller than 50000!

Already been there!

I had changed the code and made the value a const unsigned int, but also changed the relay to a 5 post instead of using a stepdown resistor, that way I would have a distinct high or low input as you were saying. I can try and quickly draw a schematic at some point so you can get a better understanding of the system. Currently do not have a cad program with electrical schematic capabilities so it will be hand drawn.

That is not a problem

Please also post your revised sketch, using code tags when you do.

You changed your constant to unsigned int but you did not change the variable you use in the for loop. It is still an int

If you had turned on all warnings in the IDE (which you should do). the compiler would have been happy to tell you.

C:\sketch_oct16a.ino: In function 'void loop()':
C:\sketch_oct16a.ino:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < numberOfSteps; i++) {
                     ~~^~~~~~~~~~~~~~~
C:\sketch_oct16a.ino:20:5: warning: iteration 32767 invokes undefined behavior [-Waggressive-loop-optimizations]
     for (int i = 0; i < numberOfSteps; i++) {
     ^~~
C:\sketch_oct16a.ino:20:23: note: within this loop
     for (int i = 0; i < numberOfSteps; i++) {
                     ~~^~~~~~~~~~~~~~~
...

Another good catch!

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