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 4258 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
  • andywest
    0 andywest over 7 years ago

    You may want to change the title of your discussion to something more specific than "Please I need help any body". People are more likely to read and answer you that way. Adding relevant tags can also help.

     

    Also, do you have a specific question? What does your code do, and how is that different than what you expected?

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

    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 lights and sound to work. I have no clue how to write code. everyone says it is so easy but not for me apparently.

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

    That sounds like a nice gift.

     

    You included some code in your post. Did you write that? If so then I would say you have some clue. But we need more details about what's working and what isn't before we can help you.

     

    I get the lights to flash but im lost from there

     

    Does that mean that the above code is working fine, but you're not sure how to code for the audio?

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

    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

     

     

     

    void setup() {

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

       pinMode(rd2LEDPin, OUTPUT);// sets the third pin for 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

      

     

    this is the part that works the lights flash I copied most of the code from a tutorial and im trying to put it together it took me 2 days to get the lights to flash properly by playing with the numbers i basically want two reed switches one to activate and one to deactivate I am a retired electrician so i know the basics what resistors to use with what led an so forth. I can wire it up but programming it is a whole different ball game and im clueless. I have less than a month to finish and im starting to panic. He will be 13 I really wanted to go above and beyond and do something special but im beginning to think i may have bitten off more than i can chew :-(

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

    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

     

     

     

    void setup() {

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

       pinMode(rd2LEDPin, OUTPUT);// sets the third pin for 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

      

     

    this is the part that works the lights flash I copied most of the code from a tutorial and im trying to put it together it took me 2 days to get the lights to flash properly by playing with the numbers i basically want two reed switches one to activate and one to deactivate I am a retired electrician so i know the basics what resistors to use with what led an so forth. I can wire it up but programming it is a whole different ball game and im clueless. I have less than a month to finish and im starting to panic. He will be 13 I really wanted to go above and beyond and do something special but im beginning to think i may have bitten off more than i can chew :-(

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

    I'm not sure I have the complete picture, but it sounds like you have two switches. One switch causes the lights to flash, the gate to come down, and the sound to play. The other switch stops the flashing, moves the gate back, and stops the sound. Correct?

     

    Coding for the switches should be fairly easy. You'll want to connect to an available GPIO pin and use digitalRead() instead of digitalWrite(). Then you need to store the results in a variable so when you're going through the loop you can evaluate and determine whether to flash, ring the bell, etc. There is a lot of example code out there; here's a link that might help:

     

    https://www.arduino.cc/en/Tutorial/Button

     

    Several years ago I used an Arduino to simulate button presses on an RF remote for room lighting, so I know it's possible. I'll see if I can dig up the code.

     

    I have no experience controlling servos, but there are many people in this community who are knowledgeable about that aspect.

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

    thank you for helping me i appreciate it:-))

    • Cancel
    • Vote Up 0 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