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 theshay

    oh and the birthday is may second.Thats why im freaking out to get this finished i have been building the table for 3 months its down to the finishing touches, applying trees and detail and getting these crossing gates to work. I tried going through a few tutorials an how to code but i cant learn fast enough. Its tough to teach an old dog new tricks. LOL

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

    Ok, I should finish something before then this evening I will try to put your code on Arduino and see what it does.

     

    Enrico

    • Cancel
    • Vote Up +2 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! This helps me a lot image

     

    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 found a different set up real close to what i want. I dont know how to add the reed switches and eliminate the yellow light

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

    [code]
    //Level_crossing
    
    #include <Servo.h>
     Servo myservo1; // create servo object to control a servo
     Servo myservo2;
     // a maximum of eight servo objects can be created
     int pos = 0; // variable to store the servo position
     int led1 = 11 ;// define RED2 LED Interface
     int led2 = 10 ;// define RED1 LED Interface
     int led3 = 9 ;// define Yellow LED Interface
     int buttonpin = 3; // define the obstacle avoidance sensor interface
     int val ;// define numeric variables val
     void setup ()
     {
     pinMode (led1, OUTPUT) ;// define LED as output interface
     pinMode (led2, OUTPUT) ;// define LED as output interface
     pinMode (led3, OUTPUT) ;// define LED as output interface
     pinMode (buttonpin, INPUT) ;// define the obstacle avoidance sensor output interface
     myservo1.attach(8); // attaches the servo on pin 9 to the servo object
     myservo2.attach(7); // attaches the servo on pin 8 to the servo object
     }
     void loop ()
     {
     val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
     if (val == LOW) // When the obstacle avoidance sensor detects a signal, LED flashes
     {
     myservo1.write(90); // tell servo to go to position in variable 'pos'
     myservo2.write(90);
     flashLights();
    
     }
     else
       
     {
     myservo1.write(0); // tell servo to go to position in variable 'pos'
     myservo2.write(0);
     digitalWrite (led1, LOW);
     digitalWrite (led2, LOW);
     digitalWrite (led3, LOW);
     }
    }
    void flashLights() {
     digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(2000); // wait for a second
     digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
      
      digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(200); // wait for a second
     digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
    
     digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
     delay(1000); // wait for a second
     digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
    }
    [/code]

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

    image

    • Cancel
    • Vote Up +1 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
  • 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
Reply
  • 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
Children
  • 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

    the servos did not work and i need to add 2 reed switches 1 for on 1 for off also i need to activate a sound card with a relay

    • 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