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 7025 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
  • javagoza
    javagoza over 3 years ago

    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 and the falling edge of the other.

    You can use interrupt service routines to detect both the rising edge transition and the falling edge transition.

    image


    dougw has a good project on the subject.

    dougw
    The Logical Project
    Intro I always had a lot of fun designing digital circuits that used Boolean logic, but these days it is rare to see designs with lots of discrete gates. With this little project I'm going to have some…
    By dougw over 4 years ago in Digital Fever > Blog
    14 comments


    I based on the same for this project that you see in the video. In this case I needed to detect the direction of movement, it's a different case but it uses the same quadrature encoder principle using a DIY magnetic linear encoder with two magnetic reed switches and a magnet.

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

    In this blog you can get the complete source code for this solution.

    javagoza
    VenTTracker #06 -  Window Sensor Prototype
    One of the main components of our ventilation routine monitoring project is the sensor for the status of doors, windows and blinds. During this week we have given a boost to the creation of that sensor…
    By javagoza over 4 years ago in Design For A Cause 2021 > Blog
    2 comments

    • Cancel
    • Vote Up +5 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • javagoza
    javagoza over 3 years ago

    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 and the falling edge of the other.

    You can use interrupt service routines to detect both the rising edge transition and the falling edge transition.

    image


    dougw has a good project on the subject.

    dougw
    The Logical Project
    Intro I always had a lot of fun designing digital circuits that used Boolean logic, but these days it is rare to see designs with lots of discrete gates. With this little project I'm going to have some…
    By dougw over 4 years ago in Digital Fever > Blog
    14 comments


    I based on the same for this project that you see in the video. In this case I needed to detect the direction of movement, it's a different case but it uses the same quadrature encoder principle using a DIY magnetic linear encoder with two magnetic reed switches and a magnet.

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

    In this blog you can get the complete source code for this solution.

    javagoza
    VenTTracker #06 -  Window Sensor Prototype
    One of the main components of our ventilation routine monitoring project is the sensor for the status of doors, windows and blinds. During this week we have given a boost to the creation of that sensor…
    By javagoza over 4 years ago in Design For A Cause 2021 > Blog
    2 comments

    • Cancel
    • Vote Up +5 Vote Down
    • Sign in to reply
    • Cancel
Children
  • colporteur
    colporteur over 3 years ago in reply to javagoza

    I like the debounce idea for use on reading the sensors. I reviewed your application and got lost. Not a programmer. I then went in search of simplified code examples. I'm thinking of trying to assemble code for a test first to see if I can get a working model. I need some of that anti-aging formula to grasp this stuff:)

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

    For example 

    In setup set the two ISRs

    // detect rising edge for switch A
    attachInterrupt(digitalPinToInterrupt(s1ArrivalPinA), isrArrivalSwitchRising, RISING);
    
    // detect falling edge for departure
    attachInterrupt(digitalPinToInterrupt(s1DeparturePinB), isrDepartureSwitchFalling, FALLING);

    Then define the two ISR, two functions

    void isrArrivalSwitchRising() {
       state = TRAIN_ARRIVED 
    }
    
    void isrDepartureSwitchFalling() {
       state = TRAIN_DEPARTED 
    }

    and in your loop method check for state changes

    if (state==TRAIN_ARRIVED) {
       state == TRAIN_PARKED;
       doMyArrivingStuff();
    }
    
    if(state==TRAIN_DEPARTED) {
       state == TRAIN_RUNNING;
       doMyDepartureStuff();
    }

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

    Did you hear that? That was my head hitting the desk:) I'm envious of the knowledge, yet my age creates limits on what I can use or retain.

    attachInterrupt(digitalPinToInterrupt(s1ArrivalPinA), isrArrivalSwitchRising, RISING);

    This call, if the sensor input s1ArrivalPinA is RISING (i.e. HIGH) run the isr routine? I will get this a little bits at a time.

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

    When the input signal goes from low to high the processor will call that subroutine once, meanwhile you do not have to do anything, only wait, it is not necessary to be polling the state of the port. When the event occurs, the interruption is triggered, the processor stops doing what it was doing and calls that ISR. That's why the ISR has to be a routine that runs quickly, we can't consume many processor cycles in that routine.

    it's a good idea to do a debounce as I do in my project, here for simplicity I'm not dealing with bouncing.

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

    It may appear I am picking and choosing topics. I confess I am, but it is not random, I have an end goal in mind. I'm a requirement guy. Is it nice to do or is there a requirement? I had to go look up the acronym ISR. It struck a cord with me. I'm just not sure if it fits.

    The operation of the train would be based on the state of the sensor. In my case would the ISR be looking for changes in the sensor states and perform an action based on the state? That essentially describes what I am trying to do.

    The sensors are HIGH when active (i.e. train parked over top) and go low when the train moves away.

    I thought I had the logic worked out with just two sensors, Departure and Arrival. The gap between the train cars caused some anomalies. With Two departure and two arrival sensors, the gap in the train cars is eliminated. I was looking to do a simple OR but it doesn't appear to be as simple as I thought.

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

    My proposal was not to focus on the state of the sensors but on the change of state, that from my point of view simplifies the logic.

    An Arrival is a change of state of the Arrival sensor signal from low to high (RISING).

    And one Departure is a state change of the departure sensor signal from high to low (FALLING).

    Everything that happens in between I don't care.

    Not all pins can trigger interrupts. In your case, I think you are using an Arduino MEGA board, it has 6 pins (2, 3, 18, 19, 20, 21) with the possibility of enabling interrupts in case I2C is not used with pins 20 and 21. In that case you would have 4 pins only enabled for interruptions.

    https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
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