Need help with code for sensors and motors. Thx

I am working on a conveyor system at my job. We have short boxes and tall boxes. tall boxes go straight and short boxes go left. There is a turn table that sends the boxes left when they are detected. In this project there are four sensors, two motors and one turntable to consider for the particular section that I am working on.

There is a sensor and motor before the height detector. This sensor controls whether any boxes should go through or if the conveyor is busy.

There is a height detector which consists of two sensors. One sensor high and one sensor low.
Then the turn table which operates on air pressure. High keeps it straight for tall boxes and low sends the small boxes left.

After the turn table is another sensor that shows when the box has passed the turn table so that we can send the next box through.

I have just been using “if statements” but I seem to be canceling out what I need this code to do. I really need help with this one guys/gals. This is my first actual career job and I want to be impressive and valuable to this company.

I really need some example programs to get me rolling on this project.

HERE IS WHAT I HAVE SO FAR

void setup(){

  Serial.begin(9600);
  
  pinMode(52, INPUT_PULLUP);
  pinMode(50, INPUT_PULLUP);
  pinMode(48, INPUT_PULLUP);
  pinMode(46, INPUT_PULLUP);
  pinMode(34, OUTPUT); 
  pinMode(32, OUTPUT); 
  pinMode(30, OUTPUT); 

}

void loop(){

  int sensorVal = digitalRead(52);
  int sensorVal2 = digitalRead(50);
   int sensorVal3 = digitalRead(48);
    int sensorVal4 = digitalRead(46);
 
  Serial.println(sensorVal);
  
               //IF TALL BOX - GO STRAIGHT                        
  if (sensorVal2 & sensorVal3 == HIGH) //This is the height detection tower. 
  {                                    //sensor 1 is for the tall boxes 
                                       //sensor 2 is for the short boxes
    
                //PIN 34 IS TURN TABLE (HIGH IS STRAIGHT AND LOW IS LEFT)
    digitalWrite(34, HIGH);  // THIS SECTION GOES HIGH BUT THEN BACK LOW
                             // I NEED FOR IT TO STAY HIGH UNTIL SENSOR 3 IS TRIGGERED    
    delay(100);             // AND THE SAME FOR WHEN IT GOES LOW, TO STAY LOW UNTIL SENSOR 3 IS TRIGGERED
  } 
               //IF SMALL BOX - GO LEFT
   if (sensorVal == LOW & sensorVal2 == HIGH)
   {
     digitalWrite(34, LOW);
     delay(100);
   }
   
   
   
   //NOW I NEED TO WRITE SOMETHING FOR MY OTHER TWO VARIABLES
   // BASICALLY IF SENSOR 3 (WHICH IS AFTER THE TURN TABLE)
  //HAS NOT BEEN TRIGGERED YET THAN MOTOR 1 (WHICH IS BEFORE
 //THE HEIGHT DETECTOR) CANNOT MOVE. AND ONCE SENSOR 3 HAS 
 // GONE HIGH, THEN MOTOR 1 CAN GO HIGH. 


     }

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

See below the code that I wrote based on arduino example. This program is for a conveyor system at work. It’s not very complicated. I just don’t know my if statements very well. Please take a look and help me if you can. Thanks

void setup(){

  Serial.begin(9600);
  
  pinMode(52, INPUT_PULLUP);
  pinMode(50, INPUT_PULLUP);
  pinMode(48, INPUT_PULLUP);
  pinMode(46, INPUT_PULLUP);
  pinMode(34, OUTPUT); 
  pinMode(32, OUTPUT); 
  pinMode(30, OUTPUT); 

}

void loop(){

  int sensorVal = digitalRead(52);
  int sensorVal2 = digitalRead(50);
   int sensorVal3 = digitalRead(48);
    int sensorVal4 = digitalRead(46);
 
  Serial.println(sensorVal);
  
               //IF TALL BOX - GO STRAIGHT                        
  if (sensorVal2 & sensorVal3 == HIGH) //This is the height detection tower. 
  {                                    //sensor 1 is for the tall boxes 
                                       //sensor 2 is for the short boxes
    
                //PIN 34 IS TURN TABLE (HIGH IS STRAIGHT AND LOW IS LEFT)
    digitalWrite(34, HIGH);  // THIS SECTION GOES HIGH BUT THEN BACK LOW
                             // I NEED FOR IT TO STAY HIGH UNTIL SENSOR 3 IS TRIGGERED    
    delay(100);             // AND THE SAME FOR WHEN IT GOES LOW, TO STAY LOW UNTIL SENSOR 3 IS TRIGGERED
  } 
               //IF SMALL BOX - GO LEFT
   if (sensorVal == LOW & sensorVal2 == HIGH)
   {
     digitalWrite(34, LOW);
     delay(100);
   }
   
   
   
   //NOW I NEED TO WRITE SOMETHING FOR MY OTHER TWO VARIABLES
   // BASICALLY IF SENSOR 3 (WHICH IS AFTER THE TURN TABLE)
  //HAS NOT BEEN TRIGGERED YET THAN MOTOR 1 (WHICH IS BEFORE
 //THE HEIGHT DETECTOR) CANNOT MOVE. AND ONCE SENSOR 3 HAS 
 // GONE HIGH, THEN MOTOR 1 CAN GO HIGH. 


     }

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

if (sensorVal2 & sensorVal3 == HIGH) //This is the height detection tower.

What do you think that this statement is doing? It is not testing that both sensor values are HIGH. It is logical-and-ing the two sensor values and comparing the result to HIGH. I seriously doubt that that is meaningful. Try:
if (sensorVal2 == HIGH && sensorVal3 == HIGH) //This is the height detection tower.

Okay, cool I'll try it right now. Thanks

I wrote

if (sensorVal2 == HIGH && sensorVal3 == HIGH)
if (sensorVal == LOW && sensorVal2 == HIGH)

The turn table goes High and low but it does not stay high.
I need it to always be high until a small box comes through.

I think you are getting confused because you can't remember which sensor or sensor pin are for tall/short, etc. boxes. Your comments in the code don't match up with the code itself.
Look at this...

            //IF TALL BOX - GO STRAIGHT                       
  if (sensorVal2 & sensorVal3 == HIGH) //This is the height detection tower.
  {                                    //sensor 1 is for the tall boxes
                                       //sensor 2 is for the short boxes

So which one is sensorVal1? You aren't checking for it. You are checking for SensorVal2 and sensorVal3, right?
Even if you are checking the right pins, you are only confusing yourself if you don't use meaningful names.

How about

    if (shortBox == HIGH && tallBox == HIGH) {

And instead of numbers, how about names for the pins?

const byte tallBoxPin = 52;
const byte shortBoxPin = 50;

// or
#define tallBoxPin 52
#define shortBoxPin  50

// then

pinMode(shortBoxPin, INPUT_PULLUP);

// and

// and with your new variable names

   shoortBox = digitalRead(shortBoxPin);

Now wouldn't that be a LOT easier to keep track of?

sensorVal2 & sensorVal3 == HIGH

should be

sensorVal2 == HIGH & sensorVal3 == HIGH

You always need to have both otherwise is assumes that sensorVal2 is an expression on its own and will evaluate true if !=0. This is probably working correctly at the moment as HIGH is 1 (from memory), but if you don't get into the habit of doing it explicitly you will cause bugs that are really hard to see and track down.

You may also want to consider using

if (expression )
{
  do something
}
else if (expression)
{
  do something else
} else if ...

This can make more efficient and understandable code if the cases are all mutually exclusive.

To control motors on the basis of sensors, you should probably use a Finite State Machine style of coding and eliminate the delays in your code, or you will eventually find the software unresponsive.

I second what lar3ry says, but would go one step further. Instead of:

const byte tallBoxPin = 52;
const byte shortBoxPin = 50;

// or
#define tallBoxPin 52
#define shortBoxPin  50

// then

pinMode(shortBoxPin, INPUT_PULLUP);

// and

// and with your new variable names

   shortBox = digitalRead(shortBoxPin);

I would make a minor change to:

const byte TALLBOXPIN= 52;
const byte SHORTBOXPIN= 50;

// or
#define TALLBOXPIN52
#define SHORTBOXPIN50

// then

pinMode(SHORTBOXPIN, INPUT_PULLUP);

// and

// and with your new variable names

   shortBox = digitalRead(SHORTBOXPIN);

By making the constants uppercase, it becomes easier to see and understand the role they play in the code.

sensorVal2 == HIGH & sensorVal3 == HIGH

should actually be

sensorVal2 == HIGH && sensorVal3 == HIGH

The single & is a bitwise AND but the double && is a logic AND for use in if statements.

The single & is a bitwise AND but the double && is a logic AND for use in if statements.

Yes, very true. Introducing my own hard to track bugs!

Spend some time pretending to be the system and work out what states you need and
the flow between states. For instance after a short box is detected you need to stay in
some sort of short-box state until it has gone past the turntable and been detected beyond
it - you cannot base your decisions just on the inputs.

Whilst doing this work out all the situations that should not be possible and add code
to check for them and output a warning message (defensive programming - check even
for erroneous conditions that you think are impossible...) The point is that somewhere
down the line when a sensor or motor fails the code will be less likely to "bash on
regardless" - it should stop and report that's something is wrong...

While testing you simulate various failures of sensors and motors to see if there are
any really bad things that can happen (box pile-up!) and think about whether adding
more checks / sensors can prevent it.

This is awesome information guys! I am an electronics wanna be, so I am not the best at this. But I have finally got this thing working based from the information that you guys have given me. Hopefully my boss will be impressed with the function that is being added to our conveyor system. Unfortunately it will be replacing a human who usually moves the box to the other lane by hand. But the good thing is that we are short staffed in one of the packing lanes, so he will still be employed.

Thanks so much for all of your help XD