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 4275 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
  • theshay
    0 theshay over 7 years ago in reply to balearicdynamics

    I appreciate you taking the time to help me with this your awesome!!

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

         Not sure if you had a chance to look at the new code and schematic I posted. I think it would be easier to modify because it is closest to what i am looking for. it only needs the switches added. Unfortunately Ive been trying to figure it out myself but just not getting it. Everyone has advice but I don't think they understand I know nothing about writing code. I wish i had more time to learn, going forward I will be doing more tutorials Because I really like the possibilities of the Arduino system and am interested in learning but for now im in a jam. I appreciate any and all help you can give me. Again I would like to thank you for your time, as I'm sure you have better things to do.

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

    Hello Edward,

     

    here it is midnight. I should solve a problem of mine then I will check your source to my Arduino. I think I can do it tomorrow morning...

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

         Ok I really appreciate you helping me. I wasn't for sure if you had seen the new stuff i posted. it is 6:30pm here im usually up all nite as I am retired. I will post some pics of the table and a video once I get it operating correctly. I am currently running all my wire for lights ect.. servos and crossing lights are already ran just need to hook to Arduino.Again I thank you very much and look forward to hearing from you.

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

    image

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

    Great! Really, a nice scenario image I finally I am closing my stuff tonight and thanks God tomorrow morning I can put your code on an Arduino, then test what's happens. I hope I have not delayed too much and this can be helpful to you.

     

    Enrico

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