Programming the conveyor with Arduino

I'm programming the conveyor with Arduino.
The conveyor has a sensor (input 7) at one end of the conveyor.
Every time the sensor is on, I want the stepping motor to transfer the part out of the conveyor with a certain step.

The problem is if there is another part that

drops into the conveyor, I want the program to reset the counter (which is "i" to zero) to let the conveyor transfer the second part out of the conveyor properly.

Would you please help to advise?

Thanks,

Here are the codes.

void setup() {
pinMode(7,INPUT_PULLUP); // sensor
pinMode(9,OUTPUT); // set Pin9 as PUL
pinMode(8,OUTPUT); // set Pin8 as DIR
Serial.begin(9600);
}
void loop() {

if(digitalRead(7)==LOW)
{
for (int i=0; i<10000; i++)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delayMicroseconds(120);
digitalWrite(9,LOW);

   }

}

}

there needs to be a delay after each setting of the pulse pin

How can I set the for loop more than 32500 loops?

void setup() {
pinMode(7,INPUT_PULLUP); // sensor
pinMode(9,OUTPUT); // set Pin9 as PUL
pinMode(8,OUTPUT); // set Pin8 as DIR
Serial.begin(9600);
}
void loop() {

if(digitalRead(7)==LOW)
{
for (int i=0; i<32500; i++)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delayMicroseconds(120);
digitalWrite(9,LOW);
  if(digitalRead(7)==LOW)
      { i=0;
      }
   }
}

}

If you make the variable i an unsigned int data type, i can count to 65535. Or make it unsigned long to count to 4,294,967,295.

See Arduino data types.

consider

#define pinSen  A1
#define pinDir  10
#define pinStep 11

#define Delay       delay
#define PulseUsec   100

void setup () {
    pinMode (pinSen,  INPUT_PULLUP); // sensor
    pinMode (pinStep, OUTPUT);
    pinMode (pinDir,  OUTPUT);

    digitalWrite (pinStep, HIGH);
    digitalWrite (pinDir,  HIGH);

    Serial.begin (9600);
}

int  i = 0;

void loop () {
    if (digitalRead (pinSen) == LOW)
        i = 32500;

    if (0 < i)  {
        while (i--)  {
            digitalWrite (pinStep, LOW);
            Delay (PulseUsec);

            digitalWrite (pinStep, HIGH);
            Delay (PulseUsec);
        }
    }
}

Many thanks for the answers.
They are very useful.

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