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 I need help fixing an arduino project please
  • 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
  • State Verified Answer
  • Replies 8 replies
  • Answers 4 answers
  • Subscribers 393 subscribers
  • Views 950 views
  • Users 0 members are here
  • rr crossing
  • level crossing gates
  • arduino
  • automatic rr crossing
Related

I need help fixing an arduino project please

theshay
theshay over 7 years ago

I have an arduino project for RR crossing gates and lights. reed switch turns it on reed switch 2 turns it off The problem is The bell rings and the lights flash but for some reason I cannot get the gates to raise and lower. Could someone please help me with this?

#include <Servo.h>

//  control_pin1 signal:  ___┌───┐_____________________________________┌───┐___________
//  control_pin2 signal:  ______________________________┌───┐____________________┌───┐_
//                           ^START                     ^STOP          ^START    ^STOP

#define delta   540         // 0.540 seconds on and off
#define bstrobe  60
#define led_pin1    11      // the Crossing LED is attached to pin 11
#define led_pin2    12      // the Crossing LED is attached to pin 12


int control_pin1 = 14;      // Control Pin for first reed sensor is Pin 14 (A0) Low/Ground is OFF
int control_pin2 = 17;      // control pin for second reed sensor
                            // OPEN (Unattachd) Pin is ON
#define bell_pin    15      // D15 == A1  This is the signal for the single Bell

Servo servo1;               // servo object
int servo1_pin = 10;         // servo pin
Servo servo2;               // servo object
int servo2_pin = 9;         // servo pin
int servo_delay = 40;       // this number decides how fast the servos will move. higher number = slower servos
int position_on = 5;       // position of the servo motor when it's running
int position_off = 45;       // position of the servo motor when it's off
bool gates_down = false;

bool should_run = false;    // this will be a true or false value indicating whether the process should be running or not
int control1 = LOW;         // current value of the first reed switch
int prev_control1 = LOW;    // this will keep track of the previous value of the control pin (neccessary for finding the edge condition)
int control2 = LOW;         // current value of the second reed switch
int prev_control2 = LOW;    // previous value of the second reed switch


void setup() {
  // initialize led_pin as digital output pin
  pinMode(led_pin1, OUTPUT);
  pinMode(led_pin2, OUTPUT);
  pinMode(bell_pin, OUTPUT);
  digitalWrite(bell_pin, HIGH);
  pinMode(control_pin1,INPUT_PULLUP);
  pinMode(control_pin2,INPUT_PULLUP);
  servo1.attach(servo1_pin);
  servo2.attach(servo2_pin);
}

// the loop function runs over and over again forever
void loop() {
    
    prev_control1 = control1;
    control1 = digitalRead(control_pin1);
    
    prev_control2 = control2;
    control2 = digitalRead(control_pin2);
    
    if (control1 == HIGH && prev_control1 == LOW)
    {
        // the loop should only run in the case where there was a change and it was a rising edge (see diagram at top)
        should_run = true;
    }
    
    if (control2 == HIGH && prev_control2 == LOW)
    {
        // the loop should stop when there was a rising edge on the second sensor
        should_run = false;
    }

    if (should_run == true) {
    
        lowerGates();                  // calls the function to turn the servos to the "on" position
        digitalWrite(led_pin1, HIGH);  // turn the LED on (HIGH is the voltage level)
        digitalWrite(led_pin2, LOW);   // turn the LED on (HIGH is the voltage level)
        digitalWrite(bell_pin, LOW);
        delay(bstrobe);
        digitalWrite(bell_pin, HIGH);
        delay(delta);              // wait for a second
        digitalWrite(led_pin1, LOW);    // turn the LED off by making the voltage LOW
        digitalWrite(led_pin2, HIGH);   // turn the LED on (HIGH is the voltage level)
        digitalWrite(bell_pin, LOW);
        delay(bstrobe);
        digitalWrite(bell_pin, HIGH);
        delay(delta);              // wait for a second
    } else {
        raiseGates();                    // calls function to turn the servos to the "off" position
        digitalWrite(led_pin1, LOW);     // turn the LED off
        digitalWrite(led_pin2, LOW);     // turn the LED off
    }
}
    void lowerGates()
    {
        // moves the servos from position_off to position_on
        if (gates_down == false)
        {
            for (int i=position_off; i <= position_on; i++) 
            {
                servo1.write(i);
                servo2.write(i);
                delay(servo_delay); // This should slow down the servos
            }
            gates_down = true;
        }
        
    }
    
    void raiseGates()
    {
        // moves the servos from position_on to position_off
        if (gates_down == true)
        {
            for (int i=position_on; i > position_off; i--) 
            {
                servo1.write(i);
                servo2.write(i);
                delay(servo_delay); // This should slow down the servos
            }
            gates_down = false;
        }
    }

  • Sign in to reply
  • Cancel

Top Replies

  • beacon_dave
    beacon_dave over 7 years ago +3 verified
    Perhaps check the initialisation of your variables for 'position_on' and 'position_off' and how you are using them in your lowerGates() and raiseGates() functions. You have: position_on = 5 position_off…
  • dougw
    dougw over 7 years ago +2 suggested
    Can you show what you have so far? There should be a delay between lights flashing and gate closing.
  • theshay
    theshay over 7 years ago in reply to grgilbert-2fc +2
    yes i will be using dave advice it makes perfect sense also I was reading about the issues with delays. gonna try and work that out next then may possibly try and add 2 turn out to the system as well but…
  • beacon_dave
    0 beacon_dave over 7 years ago

    Perhaps check the initialisation of your variables for 'position_on' and 'position_off' and how you are using them in your lowerGates() and raiseGates() functions.

     

    You have:

       position_on = 5

       position_off = 45

     

    Then in lowerGates() you are setting the value of variable 'i' to 45 then checking to see if 45 is less than or equal to 5 which it isn't so the for loop exits straight away.

     

    I think you want:  

    for (int i = position_off; i > position_on; i--)

    which will count down from 45 to 5 degrees

     

    Similar with raiseGates()

    for (int i = position_on; i < position_off; i++)

    which will count up from 5 to 45 degrees

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • dougw
    0 dougw over 7 years ago

    Can you show what you have so far?

    There should be a delay between lights flashing and gate closing.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • grgilbert-2fc
    0 grgilbert-2fc over 7 years ago

    Looks like Dave Ingles post has the right answer. Your for statements are immediately satisfied so they will never execute the included code. Hence the gates do not move.

     

    I would also recommend learn how to get the delays without using delay() statements. All a delay() statement does is stop the code dead for a period of time. That Arduino could do several crossing gates on several different layout locations. With the delay() statement if the code is working on one crossing and reaches the delay() then another train at another crossing passes a sensor that 2nd train will never be seen. Look at Multiple Turnouts and Other Multitasking – The N Scaler   . It will teach you a lot about writing Arduino code for model railroading.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jomoenginer
    0 jomoenginer over 7 years ago

    Is this the same project you posted for previously and was already given code for?

    https://www.element14.com/community/message/238301/l/please-i-need-help-writing-code#238301

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • theshay
    0 theshay over 7 years ago in reply to jomoenginer

    no this is a project for a friend I copied most of the code from  Model Railroad Hobbyist magazine and made a few changes but could not get the servos to work. I am slowly learning more and more about coding. The earlier project was a gift for my grandsons birthday.  I was thankful to receive the help from everyone. I figured I would give it another shot and help my friend out, he is worse off than me! I really enjoy playing with the arduino I do every tutorial i come across on the web. but when i hit a brick wall i have no one to ask but here :-))

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • theshay
    0 theshay over 7 years ago in reply to grgilbert-2fc

    yes i will be using dave advice it makes perfect sense also I was reading about the issues with delays. gonna try and work that out next then may possibly try and add 2 turn out to the system as well but for now will be pleased with the servos operating. When i hit that brick wall ill be back lol

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • theshay
    0 theshay over 7 years ago in reply to beacon_dave

    thank you so much i knew i had to be missing something simple I will implement this then continue to add my turn outs if all is well, then i will try ti ditch the delays for millis and see how far i get

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • theshay
    0 theshay over 7 years ago in reply to dougw

    Doug I appreciate your reply I am working on dave's suggestion then i will go from there. Thank you

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • 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