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 Please I need help Writing code
  • 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 51 replies
  • Answers 9 answers
  • Subscribers 396 subscribers
  • Views 4265 views
  • Users 0 members are here
  • need help coding
  • need code written
Related

Please I need help Writing code

theshay
theshay over 7 years ago

I am trying to write code for an automatic railroad crossing gates for my grandsons train table i built. I have no clue what i am doing. What i can say is im using 2 servos with external power 4 red leds that need to flash and a speaker for bell ding which i was hoping to use a sound file to play back my wire config is 2,3 for leds  2 servos 5,6 I also have a 10 second record-able device for train horn which uses push button for playback. i wanted to cut off the push button and use the arduino r3 to act as the push button pin 8. Also i will have 2 reed switches one to activate gates and lights and one to deactivate pins 4,7 this is what i have so far I get the lights to flash but im lost from there can some one help me write this code?

 

@@@@

int rd1LEDPin=2; //Declares rd1LED as int and set to 2

int rd2LEDPin=3; //declairs rd2LED as int and set to 3

 

int rd1onTime=250; //light 1 on time

int rd1offTime=250;  // light 1 off time

int rd2onTime=250; //light 2 on time

int rd2offTime=250;  // light 2 off time

 

const int REED_PIN = 4; // Pin connected to reed switch

const int REED_PIN = 7; // Pin connected to reed switch

const int LED_PIN = 13; // LED pin - active-high

 

 

void setup() {

  pinMode(rd1LEDPin, OUTPUT);// sets the second pin for output

   pinMode(rd2LEDPin, OUTPUT);// sets the third pin for output

 

  Serial.begin(9600);

  // Since the other end of the reed switch is connected to ground, we need

  // to pull-up the reed switch pin internally.

  pinMode(REED_PIN, INPUT_PULLUP);

  pinMode(LED_PIN, OUTPUT);

}

 

void loop() {

  digitalWrite(rd1LEDPin,HIGH); //turns led 1 on

  digitalWrite(rd2LEDPin,LOW); // turns led 2 off

  delay (rd1onTime); //wait

  delay(rd2offTime); //wait

 

  digitalWrite(rd1LEDPin,LOW); //turns led 1 off

  digitalWrite(rd2LEDPin,HIGH);//turns led 2 on

  delay (rd1offTime);//  wait

  delay(rd2onTime);  //wait

 

  int proximity = digitalRead(REED_PIN); // Read the state of the switch

  if (proximity == LOW) // If the pin reads low, the switch is closed.

  {

    Serial.println("Switch closed");

    digitalWrite(LED_PIN, HIGH); // Turn the LED on

  }

  else

  {

    digitalWrite(LED_PIN, LOW); // Turn the LED off

 

}

  • Sign in to reply
  • Cancel

Top Replies

  • theshay
    theshay over 7 years ago in reply to andywest +4 suggested
    ok i changed it. please for give me im an old man trying to do something for my autistic grandson he loves trains. so i build him a table for his birthday. only thing left to do is make the crossing gates…
  • jomoenginer
    jomoenginer over 7 years ago in reply to theshay +3 suggested
    Considering your lack of code development background, gabbing code examples might be causing more of an issue for you than it may solve. My suggestion for you is to write out what you want the code to…
  • theshay
    theshay over 7 years ago in reply to balearicdynamics +3
    Thank you so very much Enrico You are truly an honor and a scholar. May the guiding light be with you! I cant thank you enough for helping me. you really got me out of a jam. :-))) Not sure if you travel…
Parents
  • balearicdynamics
    0 balearicdynamics over 7 years ago

    Hello Edward,

     

    I am sorry to miss your question, thanks to tariq.ahmad  now I am on the piece image Don't worry we can easily solve your problem; BTW, maybe many users say that the problems are easy. I always found easy the problems I already know the solution image

     

    I ask you a favor, please. Take a look in the editor features and add the code with Syntax Highlighting so I can easily copy and past it in one Arduino board here to make some test and solve it faster. It was my browser rendering wrond your sources, now I can see them correctly. So, go ahead.

     

    As far as I see lights may flash with your code but this approach IMHO may create difficulties implementing the other part, better if we use a couple of separate functions. Let me know as I am almost available to follow you these days.

     

    Wait for your code to try it together. image What is the Birthday deadline?

     

    Enrico

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

    As waiting for your code I mean your last update after the mcb1 suggestions eventually applied.

     

    Enrico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • 14rhb
    0 14rhb over 7 years ago in reply to theshay

    Edward,

     

    That is a great setup you have there.

     

    Rod

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

    Enrico,

         No problem brother I appreciate your help. still plenty of time i have almost two weeks to finish up. I ordered a few more things off line to add they should be here by the weekend. I am adding camping tents and campers around the two fire pits, one is on the mountain looks like a black circle, it is actually lighted just not wired yet. to wire everything will only take me 1 day, I also ordered more street lights to put around the track those are all working lights too just like the ones in the beach parking lot. the empty corner to the lower right is where the petrol station is going. I am building that with foam core board and printed designs. All of the grass is stained saw dust instead of spending 30 bucks or more just for fake grass. trees are branches cut from a bush and glued deer moss and painted. The reeds along the lake side are chip brushes taken apart then glued the bristles into clusters.  All of the miniature people I ordered off wish.com for 2 bucks free shipping. Arduino card from wish for 5 bucks and a clear case for Arduino 2 bucks on wish. The street lamps I got 20 for 10 bucks on wish.comThe parking-lot and roads are printed then glued on foam core. The power to run everything with plenty of add on room is a 200watt computer supply converted into 3v, 5, 12v outputs all packed neatly into a service box used to be old fuse box for my garage. the train, not all shown I have had since the mid 80's. The slot car track I bought 2 years ago for my grandson to play with when he visits. He keeps asking me where it is and can he play with it. I,m so excited I can't wait to see the look on his face when he sees what grandpa has made for him. :-))

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

    Hi Edward,

     

    I am trying to setup your circuit on my Arduino and run your code now.

     

    Enrico

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

    Hi, is this the most recent code you have issues on?

     

    Than you. Enrico

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

    well to be honest ive been trying to write the code my self since we last talked What i put together needs the two switches and servos added I was following a couple tutorials I can give you that it doesnt matter what ever is easier for you

    • 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 balearicdynamics

    /* Railroad crossing using two reed switches one turns on 2 turns off
     *  two servos to rais and lower gates from 0 degrees(gates closed) to 50 degrees(gates open)
     *  sw1 will activate loop(lights sound and lower gates).
     *  sw2 will stop loop and raise gates
     */
    //
    #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
    #define control_pin1 14    //Control Pin is Pin 14 (A0) Low/Ground is OFF reedswitch
    #define control_pin2 16    //Control Pin  16 (A2) reed switch 2
                             // OPEN (Unattachd) Pin is off
    #define bell_pin    15    // D15 == A1  This is the signal for the single Bell
    
    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);
    }
    
    // the loop function runs over and over again forever
    void loop() {
        if (digitalRead(control_pin1)==LOW) {
            digitalWrite(led_pin1, HIGH);   // turn the LED on (HIGH is the voltage level)
            digitalWrite(led_pin2, LOW);   // turn the LED off (low is the voltage level)
            digitalWrite(bell_pin, LOW);    // turn the LED off (low is the voltage level)
            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 {
        (digitalRead(control_pin2)==LOW)
        digitalWrite(led_pin1, LOW);     // turn the LED off
        digitalWrite(led_pin2, LOW);     // turn the LED off
        }
    }

    this is what i wrote it worked until i tried to ad switches and servos first loop should run until sw2 is activated then shut off loop

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

    im honestly trying to learn how gave it my best shot

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

    Hi,

     

    don't worry, it's better trying and making a lot of errors but learning than copying and pasting. So I have started fro your last code, cleaning it a while and optimizing step by step. I'll keep you updated.

     

    First, I have created parameters.h header file. All the constant that you define as variables are better put as #define, it saves memory and processor time. Then I have used some loop to make the flash LEDs function shorter.

     

    The new flashing sequence is here:

     

    // LED flashing sequence
    void flashLights() {
      int j;
    
    
      // Light yellow LED
      digitalWrite(YELLOW, HIGH); // turn the LED on (HIGH is the voltage level)
      delay(LONG_DELAY); // wait for a second
      digitalWrite(YELLOW, LOW); // turn the LED off by making the voltage LOW
    
    
      // Two red LEDs cycle
      for(j = 0; j < RED_FLASHING_CYCLE; j++) {
        
        digitalWrite(RED2, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(SHORT_DELAY); // wait for a second
        digitalWrite(RED2, LOW); // turn the LED off by making the voltage LOW
        
        digitalWrite(RED1, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(SHORT_DELAY); // wait for a second
        digitalWrite(RED1, LOW); // turn the LED off by making the voltage LOW
      } // End LEDs flashing loop
     
      digitalWrite(YELLOW, HIGH); // turn the LED on (HIGH is the voltage level)
      delay(MID_DELAY); // wait for a second
      digitalWrite(YELLOW, LOW); // turn the LED off by making the voltage LOW
    }

     

    Going ahead image

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

    Until now, the new function for LEDs works fine. Please can you tell me the issue ? Are the two servos not working?

     

    Enrico

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

    At line 43, you wrote

     

    (digitalRead(control_pin2)==LOW)

     

    but it seems that you need to change lines 43-45 to

     

        if(digitalRead(control_pin2)==LOW){
             digitalWrite(led_pin1, LOW);     // turn the LED off
             digitalWrite(led_pin2, LOW);     // turn the LED off
         }

    The "if" was missing

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

    At line 43, you wrote

     

    (digitalRead(control_pin2)==LOW)

     

    but it seems that you need to change lines 43-45 to

     

        if(digitalRead(control_pin2)==LOW){
             digitalWrite(led_pin1, LOW);     // turn the LED off
             digitalWrite(led_pin2, LOW);     // turn the LED off
         }

    The "if" was missing

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

    yeah i probably deleted when i was trying to add the servos

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

    also wasn't sure on the switches if low was off or on obviously they are momentary switches

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

    also notice the bell pin is for my DFPlayer

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

    See image:

    image

    • 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