if statement with multpile inputs

First post here... :o ... I hope it's in the right section..... I'm relatively new to Arduino programing though I have made a few projects using generic Arduino nanos.

I built a can crusher ages ago with automotive relays and switches. It works pretty good but its slow with a liner actuator as the crushing ram.

I have built a distance sensing traffic light using a real traffic fixture. I have it in my garage to help me stop while backing up to avoid bumping the wall. This works OK but I think my cheap sensor makes it unreliable.

I have built a battery operated remote control 'toolbox' lock system using a relay module, reset button, automotive door lock motors, a speaker for the chirp, and an LED for visual. I also have an electric bypass key in case something goes wrong. This seems to work pretty good.. I havent however installed it yet.. Ive made a new model of the tool box lock using a Servo to actuate the locking arm.

..

I want to use the linear actuator from my original crusher to build a sun tracker for my solar panels. (I'm sure to ask questions later on this one

..
So now I want to build a new can crusher.

I am using a generic Arduino nano, pneumatic cylinder, a selection of NPN type sensors, and a push button start.. All these parts were given, bought or scavenged off of equipment that was being thrown out..

I'll list how I expect it to work using some of the language from the code I put together. (I know I need to work on learning a lot)

My code is pretty well commented.

I run the Signal test once in setup to verify the signals.

/**********************************************************************************************************************************************/

Push the button: (Cycle_start). ----This is a generic 2 wire momentary push button.

Now check sensors:

(Can_present)check if a can is present. ----This is a prox switch - Fotek SP-05N

(Bwd_sensor)check cylinder is backward. ----This is a cylinder mounted prox switch - SMC D-Y59A

(Jam_sensor)Check for a jam. ----This is a laser sensor shooting thru the crush zone - BANNER QS186LE/QS18VN6R

If a can is present, the cylinder is backward, and there isn't a jam, Start cycle. otherwise do nothing

Turn on a (Red_led) and sound a tone(piezoPin) as an alarm. ----Generic LED's and speaker-

Next activate the (Fwd, output) to trigger the relay Coil for a short time.---- the relay is a generic 5V 2 Channel DC 5V Relay Module with Optocoupler High/Low Level Trigger Expansion Board.

Then deactivate (Fwd, output) as this is shifting an air valve and said valve stays direction last coil was triggered. ----The air solenoid is a -SMC NVSF2200-5FZ-

Now watch for (Fwd_signal) sensor: ----This is also a -SMC D-Y59A-

Once the forward signal is made, activate the (Bwd, output) a short time to reverse the cylinder.

Then deactivate (Bwd, output) as this is the same -SMC NVSF2200-5FZ- as above.

Once the (Bwd_sensor) is made, wait for button press to start cycle again.

/**********************************************************************************************************************************************/

My main issue is when I press the button is skips the sensor checks in the if statement and just goes to activating the relay thus starting the cycle.

Here is my poorly written code..

code/

/*
  Can crusher Program Test.
  24v solenoid valves(for air valve).
  double relay module.
  Push button to start.
  Sensors for Fwd, Bwd, Jam, and Can present in receiver.
*/

int Fwd = 2;                                      //Set Pin 2 Forward Coil Voltage (Relay)
int Bwd = 3;                                      //Set Pin 3 Backward Coil Voltage (Relay)

const int Start_Cycle = 4;                        //Set pin 4 As Start cycle pin
const int Jam_signal = 5;                         //Set Pin 5 as Jam Signal
const int Can_present = 6;                        //Set Pin 6 as Can Present Signal
const int Bwd_signal = 7;                         //Set Pin 7 as Back Signal
const int Fwd_signal = 8;                         //Set Pin 8 as Forward Signal

int piezoPin = 10;                                //Set Pin 10 for piezo buzzer

const int Green_led = 11;                         //Set Pin 11 for Green LED output
const int Red_led = 12;                           //Set Pin 12 for Red LED output

void setup()

{
  pinMode(Start_Cycle, INPUT_PULLUP);             //Start_Cycle is an INPUT
  pinMode(Fwd_signal, INPUT_PULLUP);              //Fwd_signal is an INPUT
  pinMode(Bwd_signal, INPUT_PULLUP);              //Bwd_signal  is an INPUT
  pinMode(Jam_signal, INPUT_PULLUP);              //Jam_signal is an INPUT
  pinMode(Can_present, INPUT_PULLUP);             //Can_present is an INPUT
  pinMode(Green_led, OUTPUT);                     //Set Green_led is an OUTPUT
  pinMode(Red_led, OUTPUT);                       //Set Red_led is an OUTPUT
  pinMode(Fwd, OUTPUT);                           //Set Fwd as an OUTPUT
  pinMode(Bwd, OUTPUT);                           //Set Bwd as an OUTPUT
  pinMode(piezoPin, OUTPUT);                      //Set piezoPin as an OUTPUT
  Serial.begin(9600);                             //Start Serial connection
  
  //Sensor tests
  Serial.print("Fwd Signal: ");
  Serial.println(digitalRead(Fwd_signal));        //Sensor Test
  Serial.print("Bwd Signal: ");
  Serial.println(digitalRead(Bwd_signal));        //Sensor Test
  Serial.print("Jam Signal: ");
  Serial.println(digitalRead(Jam_signal));        //Sensor Test
  Serial.print("Can Present: ");
  Serial.println(digitalRead(Can_present));       //Sensor Test
  Serial.print("Start Cycle: ");
  Serial.println(digitalRead(Start_Cycle));       //Sensor Test
  Serial.println("End Of Tests");
}

void loop()
{

  digitalWrite (Green_led, HIGH);                 //Turn On Green LED (Ready For Can)
  digitalWrite(Red_led, LOW);                     //Turn Off Red LED

  while (digitalRead(Start_Cycle) == HIGH);       //Wait for Button Press
  {
    //Do Nothing
  }
  if (digitalRead(Start_Cycle) == LOW && digitalRead(Bwd_signal) == LOW && digitalRead(Can_present) == LOW && digitalRead(Jam_signal) == LOW)
    //If Start button is pressed, cylinder is back, can is present, and no can is Jammed, Start cycle!
  {
    Serial.println("CAUTION CRUSHER ACTIVATED");  //Print message to monitor
    digitalWrite (Green_led, LOW);                //Turn off LED
    tone(piezoPin, 500, 800);                     //Produce tone
    delay(1000);                                  //Wait
    tone(piezoPin, 1000, 400);                    //Produce tone
    digitalWrite(Red_led, HIGH);                  //Turn on Red LED
    delay(2000);                                  //Wait
    digitalWrite(Fwd, HIGH);                      //Turn on Fwd Coil
    delay(500);                                   //Wait
    digitalWrite(Fwd, LOW);                       //Turn off Fwd Coil
    delay(1000);                                  //Debounce
  }

  /*****************************************************************************************************************************************/

  while (digitalRead(Fwd_signal) == HIGH);        //Uses the Fwd signal as a wait for cylinder to retract 
  {
    //Do nothing
  }
  if (digitalRead(Fwd_signal) == LOW);            //If Fwd signal
  {
    Serial.println("CAN IS CRUSHED");             //Display "Crushed"
    delay(250);                                   //Wait
    Serial.println("CAUTION SYSTEM RETRACTING");  //Display "Caution message"
    digitalWrite(Bwd, HIGH);                      //Turn on Bwd COIL
    delay(500);                                   //Wait
    digitalWrite(Bwd, LOW);                       //Turn off Bwd Coil
    tone(piezoPin, 1000, 400);                    //Produce second tone
    delay(600);                                   //wait
    tone(piezoPin, 500, 800);                     //Double tone
    delay(100);                                   //Debounce
  }

}

/code

I'm sure there are better ways to do a lot of this than using while statements and well a lot of things. I'm still learning. I have tried looking for the answer to this.. This Reading & comparing multiple digital input signals - Programming Questions - Arduino Forum seems to be the closest. I need to re-verify the sensor states. But the Jam sensor I technically want open as in no can present.. So any constructive criticism is welcome.

Looks like I might have posted in the wrong section If a Mod could move this to

Programming Questions

I would be grateful.
Thank You

Classic example of code that would need a debounce.

Pressing button breaks out of the first “while” but the subsequent “if” reads a bounce.

I wouldn’t bother with having the button checking code in the “if” anyway which is just asking for problems.

You started to use state variables which is awesome, but you really should commit to using them for this kind of project. Using while/break statements is a slippery slope.

Redwolf947:
Looks like I might have posted in the wrong section If a Mod could move this to

Programming Questions

I would be grateful.

There are many people who simply do not read the "Programming Questions" section as it is too busy and they do not have the time. So if it was moved there that may be most unfortunate!

Slumpert:
Classic example of code that would need a debounce.

Pressing button breaks out of the first “while” but the subsequent “if” reads a bounce.

I wouldn’t bother with having the button checking code in the “if” anyway which is just asking for problems.

You started to use state variables which is awesome, but you really should commit to using them for this kind of project. Using while/break statements is a slippery slope.

I understand while/break statements are a slippery slope :wink: .

Here is my deal, if I only look at one or 2 sensors in my 'if statement' all works fine. In my case I'm not asking the nano to do anything else so I don't mind it sitting "idle" while waiting for the button.

What I need or want to know is if the proper syntax of the stated 'if statement' is correct? and does it matter if all signals are HIGH or LOW? How do I properly write that statement?

I have a 1 sec debounce at the end of each section. Do I have this in the wrong place?

What I need or want to know is if the proper syntax of the stated 'if statement' is correct? and does it matter if all signals are HIGH or LOW? How do I properly write that statement?

I can find nothing wrong with this logic, and when I test it with some wires to ground from the pins, the conditional statement is entered properly.

if (digitalRead(Start_Cycle) == LOW && digitalRead(Bwd_signal) == LOW && digitalRead(Can_present) == LOW && digitalRead(Jam_signal) == LOW)

My main issue is when I press the button is skips the sensor checks in the if statement and just goes to activating the relay thus starting the cycle.

The conditional statement based on 4 inputs being LOW at the same time is not wrong. I would check the wiring and state of the sensor check inputs.

I'm sure there are better ways to do a lot of this than using while statements and well a lot of things.

You're correct about that.

Put your 4 pins to be checked on 4-5-6-7, which is PORTD.
Then

if ( (PORTD && 0xF0) ==0 ){ // all 4 are low
// do whatever
}

Just for clarity, and possibly a issue,

In your If statement , you end without a “;”

Just for clarity, and possibly a issue,
In your If statement , you end without a ";"

That is correct. The if conditional statement should not end with a ;

Indeed, the next conditional should not end in the ; as well.

if (digitalRead(Fwd_signal) == LOW);            //If Fwd signal
  {

See Nick Gammon's Trap #13 Don't put a semicolon at the end of every line
http://www.gammon.com.au/tips

cattledog:
I can find nothing wrong with this logic, and when I test it with some wires to ground from the pins, the conditional statement is entered properly.

if (digitalRead(Start_Cycle) == LOW && digitalRead(Bwd_signal) == LOW && digitalRead(Can_present) == LOW && digitalRead(Jam_signal) == LOW)

The conditional statement based on 4 inputs being LOW at the same time is not wrong. I would check the wiring and state of the sensor check inputs.

You’re correct about that.

cattledog,
Thank you much for your input. Now what if one of the conditional statements is set to HIGH but is not an input_pullup? The way the laser works I’m having issues with it having the opposite signal than I require but I can wire it differently using the white wire… i think ::slight_smile:

if (digitalRead(Start_Cycle) == LOW && digitalRead(Bwd_signal) == LOW && digitalRead(Can_present) == LOW && digitalRead(Jam_signal) == HIGH)

CrossRoads,

CrossRoads:
Put your 4 pins to be checked on 4-5-6-7, which is PORTD.
Then

if ( (PORTD && 0xF0) ==0 ){ // all 4 are low

// do whatever
}

Would you please explain this deeper?

Slumpert, and cattledog,

Slumpert:
Just for clarity, and possibly a issue,

In your If statement , you end without a “;”

cattledog:
That is correct. The if conditional statement should not end with a ;

Indeed, the next conditional should not end in the ; as well.

if (digitalRead(Fwd_signal) == LOW);            //If Fwd signal

{




See Nick Gammon's Trap #13 Don't put a semicolon at the end of every line
http://www.gammon.com.au/tips

So I should remove the semi colon from the condition?

Also thanks for the link that info should prove to be helpful

Well off the the Workbench to test some of this. Again thanks for any and all input.

Looks like I had both white wires hooked up on the laser.. Might have been an issue. I also removed the semi colon. Code works as intended. I now see I need to make a condition if there is a jammed can what the program should do. As it sits it must see the forward signal to reset.

I haven't tried CrossRoads example yet as I'd like to understand it better before just using it.

I Plan however to try to update and or improve this code to using State changes with the button.

I haven’t tried CrossRoads example yet as I’d like to understand it better before just using it.

See
https://www.arduino.cc/en/Reference/PortManipulation

Grumpy_Mike:
See
Arduino - PortManipulation

Thanks Grumpy_Mike! Now I see. I may just try that at some point to see how it works..

Here is the first part of my program I found I had a problem with an extra semicolon.

/*
  Can crusher Program Test.
  24v solenoid valves(for air valve).
  double relay module to switch valve direction.
  Push button start.
  Sensors for Fwd, Bwd, Jam, and Can present in receiver.
  Added Humidity & Temp sensor with RTC and LCD.
  Added Insert can message and Jam message  
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <DHT.h>

#define DHTPIN  3
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

byte degreef[] = {0x1C, 0x14, 0x1C, 0x00, 0x07, 0x04, 0x06, 0x04};
byte clock[] = {0x00, 0x0E, 0x15, 0x17, 0x11, 0x0E, 0x00, 0x00};

RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27, 20, 4);               //Set the LCD address to 0x27 for a 20 chars and 4 line display

const int Start_Cycle = 9;                        //Set pin 2 As Start cycle pin

const int Bwd_signal = 5;                         //Set Pin 4 as Back Signal
const int Can_present = 6;                        //Set Pin 5 as Can Present Signal
const int Jam_signal = 7;                         //Set Pin 6 as Jam Signal
const int Fwd_signal = 8;                         //Set Pin 7 as Forward Signal

int Fwd = 16;                                     //Set Pin A2 Forward Coil Voltage (Relay)
int Bwd = 17;                                     //Set Pin A3 Backward Coil Voltage (Relay)
int piezoPin = 12;                                //Set Pin 10 for piezo buzzer
int Green_led = 10;                               //Set Pin 11 for Green LED output
int Red_led = 11;                                 //Set Pin 12 for Red LED output

int chk;
float hum;                                        //Stores humidity value
float temp;                                       //Stores temperature value

void setup()
{
  pinMode(Start_Cycle, INPUT_PULLUP);             //Start_Cycle is an INPUT
  pinMode(Bwd_signal, INPUT_PULLUP);              //Bwd_signal  is an INPUT
  pinMode(Can_present, INPUT_PULLUP);             //Can_present is an INPUT
  pinMode(Jam_signal, INPUT_PULLUP);              //Jam_signal is an INPUT
  pinMode(Fwd_signal, INPUT_PULLUP);              //Fwd_signal is an INPUT
  pinMode(Fwd, OUTPUT);                           //Set Fwd as an OUTPUT
  pinMode(Bwd, OUTPUT);                           //Set Bwd as an OUTPUT
  pinMode(piezoPin, OUTPUT);                      //Set piezoPin as an OUTPUT
  pinMode(Green_led, OUTPUT);                     //Set Green_led is an OUTPUT
  pinMode(Red_led, OUTPUT);                       //Set Red_led is an OUTPUT
  lcd.createChar(0, degreef);                     //Create Character
  lcd.createChar(1, clock);                       //Create Character
  lcd.home();                                     //Home lcd 
  lcd.write(0);                                   //Write command for custom character

  lcd.init();                                     //Initialize the lcd
  lcd.backlight();                                //Turn on backlight
  lcd.clear();                                    //Clear and home lcd cursor
  lcd.print("Initialising...");                   //Print message
  delay(1500);                                    //Wait
                                //Sensor tests
  digitalWrite (Green_led, HIGH);                 //Turn on Green LED
  digitalWrite (Red_led, HIGH);                   //Turn on Red LED
  lcd.clear();                                    //Clear and home lcd cursor
  lcd.print("Fwd Signal: ");                      //Print message
  lcd.print(digitalRead(Fwd_signal));             //Sensor Test
  delay(1500);                                    //Wait
  lcd.setCursor(2, 1);                            //Set cursor
  lcd.print("Bwd Signal: ");                      //Print message
  lcd.print(digitalRead(Bwd_signal));             //Sensor Test
  delay(1500);                                    //Wait
  lcd.setCursor(4, 2);                            //Set cursor
  lcd.print("Jam Signal: ");                      //Print message
  lcd.print(digitalRead(Jam_signal));             //Sensor Test
  delay(1500);                                    //Wait
  lcd.setCursor(6, 3);                            //Set cursor
  lcd.print("Can Present: ");                     //Print message
  lcd.print(digitalRead(Can_present));            //Sensor Test
  delay(1500);                                    //Wait
  lcd.clear();                                    //Clear and home lcd cursor
  lcd.print("Start Cycle: ");                     //Print message
  lcd.print(digitalRead(Start_Cycle));            //Sensor Test
  delay(1500);                                    //Wait
  lcd.setCursor(0, 1);                            //Set cursor
  lcd.print("********************");              //Print message
  lcd.setCursor(4, 2);                            //Set cursor
  delay(1500);                                    //Wait
  tone(piezoPin, 1800, 300);                      //Play Tone to test Speaker
  lcd.print("End Of Tests");                      //Print message
  delay(1000);                                    //Wait
  digitalWrite (Green_led, LOW);                  //Turn off Green LED
  digitalWrite (Red_led, LOW);                    //Turn off Red LED
  lcd.createChar(0, degreef);                     //Create custom Char - degrees with F
  lcd.createChar(1, clock);                       //Create custom Char - clock
  Wire.begin();                                   //Start 'Wire'
  RTC.begin();                                    //Start 'RTC'
  dht.begin();                                    //Start 'dht'

  //RTC.adjust(DateTime(__DATE__, __TIME__));     //This line sets the RTC to the date & time this sketch was compiled
}
[code]

Wouldn't all fit in one post.. As my post count is low I had to wait 5 min :slight_smile: .. This button system for my crusher works well and is very generic. Thanks those who helped for your help.. Again I'm sure there are better ways of doing what I have done here, but this works!

void loop()
{
  {
    hum = dht.readHumidity();                     //Read data and store it to variables hum
    temp = dht.readTemperature(true);             //Read data and store it to variables temp true = read in Fahrenheit
    lcd.clear();                                  //Print temp and humidity values to lcd
    lcd.setCursor(2, 0);
    lcd.print("Humidity: ");
    lcd.print(hum);
    lcd.print("%");
    lcd.setCursor(4, 1);
    lcd.print("Temp: ");
    lcd.print(temp);
    lcd.write(0);
    DateTime now = RTC.now();
    lcd.setCursor(11, 2);
    lcd.write(1);
    lcd.print("");
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.setCursor(0, 2);
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.day(), DEC);
    lcd.print('/');
    lcd.print(now.year(), DEC);
    lcd.print(' ');
    delay(1000);                                    //End of print temp and humidity values to lcd

  }
    if (digitalRead(Bwd_signal) == LOW && digitalRead(Jam_signal) == HIGH) 
    //If cylinder is back, and a can is Jammed, flash "Jammed can"
  {
    lcd.setCursor(4, 3);                          //Set cursor
    lcd.print("Jammed Can!!");                    //Print message
    digitalWrite(Green_led, LOW);                 //Turn off LED
    digitalWrite(Red_led, HIGH);                  //Turn on LED
    delay(500);                                   //Wait
    digitalWrite(Red_led, LOW);                   //Turn off LED
    delay(500);                                   //Wait
  }
  else if (digitalRead(Bwd_signal) == LOW)        //If Bwd_signal is made
  {
    digitalWrite(Red_led, LOW);                   //Turn off LED
    digitalWrite(Green_led, HIGH);                //Turn on LED
    lcd.setCursor(4, 3);                          //Set cursor
    lcd.print("Insert Can!!");                    //Print message
    delay(1000);                                  //Wait
  }
  if (digitalRead(Start_Cycle) == LOW && digitalRead(Bwd_signal) == LOW && digitalRead(Can_present) == LOW && digitalRead(Jam_signal) == LOW)
    //If Start button is pressed, cylinder is back, can is present, and no can is Jammed, Start cycle!
  {
    lcd.setCursor(1, 3);
    lcd.print("CRUSHER ACTIVATED!");              //Print message
    digitalWrite(Green_led, LOW);                 //Turn off LED
    tone(piezoPin, 1800, 800);                    //Produce tone
    delay(1000);                                  //Wait
    tone(piezoPin, 1800, 400);                    //Produce tone
    digitalWrite(Red_led, HIGH);                  //Turn on Red LED
    delay(500);                                  //Wait
    digitalWrite(Fwd, HIGH);                      //Turn on Fwd Coil
    delay(500);                                   //Wait
    digitalWrite(Fwd, LOW);                       //Turn off Fwd Coil
    delay(100);                                  //Debounce
  }
  if (digitalRead(Fwd_signal) == LOW)             //If Fwd signal is made retract
  {
    tone(piezoPin, 1800, 400);                    //Produce tone
    delay(500);                                   //wait
    tone(piezoPin, 1800, 800);                     //Produce tone
    lcd.setCursor(2, 3);                          //Set cursor
    lcd.print("CAN IS CRUSHED!!");                //Print message
    delay(1000);                                  //Wait
    lcd.setCursor(1, 3);                          //Set cursor
    lcd.print("SYSTEM RETRACTING!");              //Print message
    digitalWrite(Bwd, HIGH);                      //Turn on Bwd COIL
    delay(500);                                   //Wait
    digitalWrite(Bwd, LOW);                       //Turn off Bwd Coil
    delay(100);                                   //Debounce
  }
}

Here is a picture.. If I can I plan to add a chute to the top and mod the program to auto feed..

Edit-10/20/19 :Added code so picture shows in post..

Good job getting this completed. Thanks for getting back to us. +1

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

cattledog:

Good job getting this completed. Thanks for getting back to us. +1

I have a video of the function before I added the "can jam" alarm.. But I don't have a hosting site to post from.

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Well not exactly haha. A lot of this was hand drawn then modified when I hand made the circuit board.
If I can find my notes with the accompanying diagrams(crude at best) I can maybe upload them..