Having lots of issues with reading inputs (Need help with the code)

Hello everyone, I am currently in my final year in collage and I am building an automated conveyor for a school project. I am currently having issues with my code not being unable to read inputs. I know I have set entire circuit correct. As of now I am able to rotate my stepper motor back and forth but I don’t want the motor to turn until a sensor is trigger HIGH. I have tried to switch my sensor to a digital and analog input but still getting the same result. Any guidance would be greatly appreciated.

int PUL = 7; //Pulses to turn stepper motor
int DIR = 6; //High=Right / Low=Left
int ENA = 5; //Enable Sorting Motor
int In_Pos_Sensor = A0; //Part in position to sort
int Sensor_3;

void setup()
{
  Serial.begin(9600);
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (In_Pos_Sensor, INPUT);


}

void loop() 
{
  Sensor_3 = digitalRead(In_Pos_Sensor);

  if(Sensor_3 == HIGH)
  {  
  for (int i = 0; i < 6400; i++) //Forward 5000 steps
  {
    digitalWrite(DIR, LOW);
    digitalWrite(ENA, HIGH);
    digitalWrite(PUL, HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL, LOW);
    delayMicroseconds(50);
  }
  }

  if(Sensor_3 == LOW)
  {
  for (int i = 0; i < 6400; i++) //Backward 5000 steps
  {
    digitalWrite(DIR, HIGH);
    digitalWrite(ENA, HIGH);
    digitalWrite(PUL, HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL, LOW);
    delayMicroseconds(50);
  }
  }
}

Motor_Test.ino (966 Bytes)

What drives "In_pos_sensor"?

Is it actively driven (e.g. the output of a buffer IC) or is it a normally-open limit switch or ... ?

You may need to declare the input mode as "INPUT_PULLUP" depending on what's driving it.

It is an industry laser sensor. It operates 10 - 30VDC and Im converting the output voltage down to 5VDC before it goes to the Arduino. So it N/O and when it's triggered its closed

The way your code is written, it appears the motor will be constantly turning backwards as long as the sensor input is LOW, and will reverse and turn forward as long as the sensor input is HIGH.

What are the actual results you are getting when running that code?

Janevski:
It is an industry laser sensor. It operates 10 - 30VDC and Im converting the output voltage down to 5VDC before it goes to the Arduino. So it N/O and when it's triggered its closed

So you'll want to set that pin up as INPUT_PULLUP then or add an external pull-up to 5V.

@david_nc makes a good point. Do you want to react to the level (i.e. the switch is low now) or to a change of state of the switch (i.e. the switch changed from high to low)?

The way my motor is working with the current code that is written is, it's not even acknowledging the conditions of the if statements and only executing the for statements. So it is rotating back and forth the 6400 steps.

You are going to have a very bad response time on the input, each of the FOR loops is taking 640000 microseconds just for the delay statements, not taking into account the execution time for the other instructions, so the input is only being read every 7/10 of a second or so. If the sensor turns on and off fairly rapidly, you will likely miss it on the input.

Changed it to INPUT_PULLUP and the motor is reacting to how I expect it to thank you guys. Now I can play around with code a little myself to get the desired results, once again thanks. Now if it’s not to much of a bother, I plan on adding 2 more sensors that will trigger as the boxes move down the conveyor. These 2 sensors will differentiate between the height of said boxes. I want to remember which of those 2 sensors are triggered so that when they reach the 3rd sensor, the motor will rotate either left or right.

david_nc:
You are going to have a very bad response time on the input, each of the FOR loops is taking 640000 microseconds just for the delay statements, not taking into account the execution time for the other instructions, so the input is only being read every 7/10 of a second or so. If the sensor turns on and off fairly rapidly, you will likely miss it on the input.

You are correct on the response time, what would I have to change to make the reading of the input much smoother.

I'm not that familiar with driving stepper motors with an Arduino, but my first thought would be to only go through a few steps each time through the loop. That would allow the code to go through the loop more often, so the inputs get read more often, but would require that you keep track of how many steps the motor had gone through if you want a specific total amount. Also, there is a Stepper library that might make controlling your motor a bit easier, Arduino - Stepper

The additional sensors and motor shouldn't be too difficult to implement, the way you stated the problem is basically how you would code it. Keep reading the inputs for the two height sensors until you get the data from them, store it, then when you get to the third sensor, take the appropriate action based on the stored information.

I think you need to implement a state machine to allow the two height sensors to be monitored for packages coming even as the motor is moving to sort the next package.

Perhaps something along these lines:

int pinPulse = 7;           //Pulses to turn stepper motor
int pinDir = 6;             //High=Right / Low=Left
int pinEna = 5;             //Enable Sorting Motor
//
int pinPosSort = A0;         //Part in position to sort
int pinHeightSnsA = A1;     //height sensor 'A'
int pinHeightSnsB = A2;     //height sensor 'B'

byte
    Sensors;
    
//bit positions used in Sensors as flags    
#define SORT_FLAG           0x01
#define HEIGHT_A_FLAG       0x02
#define HEIGHT_B_FLAG       0x04

#define ACTIVE_LEVEL_HA     HIGH    //tailor to suit
#define ACTIVE_LEVEL_HB     HIGH    //tailor to suit

#define ST_MTR_WAIT         0
#define ST_MTR_DRIVE        1

void setup()
{
    Serial.begin(9600);
    //
    pinMode( pinPulse, OUTPUT );
    pinMode( pinDir, OUTPUT );
    pinMode( pinEna, OUTPUT );
    pinMode( pinPosSort, INPUT_PULLUP );
    pinMode( pinHeightSnsA, INPUT_PULLUP );
    pinMode( pinHeightSnsB, INPUT_PULLUP );

    //init height and sort flags to zero
    Sensors = 0;
  
}//setup

void loop() 
{
    ReadSensors();
    SortMotorDrive_SM();
    
}//loop

void ReadSensors( void )
{    
    //read the height sensors and 'or' flags when a sensor
    //reads 'active'
    if( digitalRead( pinHeightSnsA ) == ACTIVE_LEVEL_HA )
        Sensors |= HEIGHT_A_FLAG;
    if( digitalRead( pinHeightSnsB ) == ACTIVE_LEVEL_HB )
        Sensors |= HEIGHT_B_FLAG;

    //if the sort position sensor is active, set the SORT flag
    //SORT_FLAG is used to advance the state machine from WAIT to DRIVEW
    if( digitalRead( pinPosSort ) == HIGH  )
        Sensors |= SORT_FLAG;
            
}//ReadSensors

void SortMotorDrive_SM( void )
{
    static int
        numSteps;
    static byte
        stateMotor = ST_MTR_WAIT;

    switch( stateMotor )
    {
        case    ST_MTR_WAIT:
            if( !(Sensors & SORT_FLAG) )
                return;

            Sensors &= ~SORT_FLAG;

            //set the direction of the sort motor based on the height sensor readings
            //they're in bit positions 0 and 1
            switch( Sensors & 0b00000011 )
            {
                case    0:
                    //height sensor 'A' read low
                    //height sensor 'B' read low
                    digitalWrite( pinDir, LOW );         //tailor to suit
                break;
    
                case    1:
                    //height sensor 'A' read high
                    //height sensor 'B' read low
                    digitalWrite( pinDir, LOW );         //tailor to suit
                break;
                
                case    2:
                    //height sensor 'A' read low
                    //height sensor 'B' read high
                    digitalWrite( pinDir, HIGH );        //tailor to suit
                break;
                
                case    3:
                    //height sensor 'A' read high
                    //height sensor 'B' read high
                    digitalWrite( pinDir, HIGH );        //tailor to suit
                break;
                        
            }//switch
            
            digitalWrite( pinEna, HIGH );
            numSteps = 6400;
            //clear the current height flags, ready for the next package coming down the pipe
            Sensors &= ~(HEIGHT_A_FLAG | HEIGHT_B_FLAG);
            //start moving the motor
            stateMotor = ST_MTR_DRIVE;
            
        break;

        case    ST_MTR_DRIVE:
            digitalWrite( pinPulse, HIGH );
            delayMicroseconds(50);
            digitalWrite( pinPulse, LOW );
            delayMicroseconds(50);
            //
            numSteps--;
            if( numSteps == 0 )
                stateMotor = ST_MTR_WAIT;
        
        break;
            
    }//switch
    
}//SortMotorDrive_SM