element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Troubleshooting why Arduino logical operators doesn't compute
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 26 replies
  • Subscribers 388 subscribers
  • Views 7039 views
  • Users 0 members are here
Related

Troubleshooting why Arduino logical operators doesn't compute

colporteur
colporteur over 3 years ago

image

How would you write the expression to test sensors Departure (A or B) HIGH AND Arrival (A or B) HIGH?

The first else if statement shown below doesn't work. Departure A OR Departure B HIgh AND Arrival A OR Arrival B High. The OR doesn't work. Only when both Departure A AND B are HIGH the serial print. The same applies to the third else if statement both Departure A and B have to be low for the statement to function. What am I coding wrong for the OR?

      if ((digitalRead(s1DeparturePinA) == LOW || digitalRead(s1DeparturePinB) == LOW) &&
          (digitalRead(s1ArrivalPinA) == HIGH || digitalRead(s1ArrivalPinB) == HIGH))
        {
         Serial.println(F("Train on siding 1 status DEPARTURE")); 
         delay(1);
        }
      else if ((digitalRead(s1DeparturePinA) == HIGH || digitalRead(s1DeparturePinB) == HIGH)&&
               (digitalRead(s1ArrivalPinA) == HIGH || digitalRead(s1ArrivalPinB) == HIGH))
        {
          Serial.println(F("Train on siding 1 status IN TRANSIT"));
          delay(1);
        }
      else if ((digitalRead(s1DeparturePinA) == HIGH || digitalRead(s1DeparturePinB) == HIGH) &&
               (digitalRead(s1ArrivalPinA) == LOW || digitalRead(s1ArrivalPinB) == LOW))
        {
          Serial.println(F("Train on siding 1 status ARRIVAL"));
          delay(1);
          s1ArrivalDetect = LOW;
        }
      else if ((digitalRead(s1DeparturePinA) == LOW || digitalRead(s1DeparturePinB) == LOW) && 
               (digitalRead(s1ArrivalPinA) == LOW || digitalRead(s1ArrivalPinB) == LOW) &&
                s1ArrivalDetect == LOW)
        {
          Serial.println(F("Train on siding 1 status STOP"));
          delay(1);       
        trainOneOut = LOW; //train selection flip_flop L=train on siding 2
        s1ArrivalDetect = HIGH;
        digitalWrite(l1_TurnoutL, LOW);  //unset departure and arrival turnouts for train1
        digitalWrite(s1_Power, LOW);        //unset power on for train1
        digitalWrite(l1_TurnoutR, HIGH);  //set departure and arrival turnouts for train2
        digitalWrite(s2_Power, HIGH);        //set power on for train2
        }

  • Sign in to reply
  • Cancel

Top Replies

  • javagoza
    javagoza over 3 years ago +5
    I would use some kind of linear encoder. If in your case you only have to detect the arrival and departure always in the same direction, the logic is reduced to detecting the rising edge of one sensor…
  • Jan Cumps
    Jan Cumps over 3 years ago +4
    First assign each pin's status to a variable' then print that variable to the serial monitor. You can then see if it's related to your logic, or to a pin not being correctly read. Added bonus: if you…
  • javagoza
    javagoza over 3 years ago in reply to colporteur +3
    For example In setup set the two ISRs // detect rising edge for switch A attachInterrupt(digitalPinToInterrupt(s1ArrivalPinA), isrArrivalSwitchRising, RISING); // detect falling edge for departure…
Parents
  • phoenixcomm
    phoenixcomm over 3 years ago

    /members/colporteur ok I use a lot of switches and have found the cheapest and easiest is to use a Schmitt Trigger (.64$ @digikey)/6 = 0.166$ for each section with an R/C filter. take a look at this blog of mine.

    imageimageThis is cheaper than the 555 solution, as for six debouncers but then you need a 555 (0.40$@digikey) * 2 = 0.80$) for your project. This gets pricey as they will require 2x resistors & capacitors, and a lot more soldering.

    Normally I put the R/C networks on an IC header if it is a 16 pin header I can put 4 networks on it. and this is on a Wire-Wrap socket. 

    image

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • colporteur
    colporteur over 3 years ago in reply to phoenixcomm

    It could use some investigation on my part, this schmitt trigger. I'm getting false starts on some animations and would like to find a solution. Got a part number? Looks like you designed a board to accommodate it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to colporteur

    The hardware debouncing will certainly do the job.
    If your Arduino isn't doing a lot, there is also software debouncing. I prefer hardware solutions, like the one phoenixcomm suggests.
    But if your scenario is repeatable (I think it is, because it is a train doing the same thing over and over, at a predictable speed) the software debouncing will do.
    I checked out a number of debounce examples for Arduino before and blogged about it here (many years ago). I'm having troubles to find it back.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • fmilburn
    fmilburn over 3 years ago in reply to colporteur

    There are some Arduino libraries for software debouncing but I haven’t used them. The method I use is described in this blog and seems widely accepted. My code doesn’t use the Arduino IDE but it could be adapted. See the links in the blog for a better explanation and some other methods. 

    This TI documenthas part numbers and how to calculate values for a Schmitt trigger. 

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • colporteur
    colporteur over 3 years ago in reply to fmilburn

    Thanks for the input. I had a read and tried to pencil out an understanding. Got stuck trying to understand but had no one to ask a question of. Back to square one.

    I'm thinking in my case the sensor is not a button. Can I do without the button debounce?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 3 years ago in reply to colporteur

    NOPE! GOTA Have Debounce.  so you have 4 switches, that are open or closed. that's eight states. so let's cut it in half as we only care about the Active States. first I would take a 4 input or-gate and tie it to each switch and to 4 Arduino inputs. take the output of the or-gate go through the Schmitt trigger to an ISR. Now when the ISR is triggered you read the 4 inputs to find the active one.

    KISS Principle (Keep It Simple Stupid) This applies to everybody. 

    OPS! you need two ISRs one for each track. and I would not use the quad input or gate. use 2 2-input or gates. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • fmilburn
    fmilburn over 3 years ago in reply to colporteur

    If you have an oscilloscope you can watch the switch and see if it is bouncing. I would guess that it does. What the software I linked does is check to make sure it doesn’t change state (or bounce as they say) faster than a human can push and let go of a button. Your switch may be chattering for quite some time. If you don’t have an oscilloscope maybe set the Arduino to print in a simple loop for one section of track as fast as it can and look for a period where it changes output from 0 to 1 several times in a row when the train moves. If so, how long does it last?

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • fmilburn
    fmilburn over 3 years ago in reply to colporteur

    If you have an oscilloscope you can watch the switch and see if it is bouncing. I would guess that it does. What the software I linked does is check to make sure it doesn’t change state (or bounce as they say) faster than a human can push and let go of a button. Your switch may be chattering for quite some time. If you don’t have an oscilloscope maybe set the Arduino to print in a simple loop for one section of track as fast as it can and look for a period where it changes output from 0 to 1 several times in a row when the train moves. If so, how long does it last?

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube