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

    Hello Edward.

     

    Now button (that replace the reed sensor but the code remains unchanged) and the servos (I have only one here but it should work with two as well) moves accordingly.

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

     

    This is the code:

     

    //Level_crossing
    
    
    #include "parameters.h"
    
    
    Servo myservo1; // create servo object to control a servo
    Servo myservo2;
    
    
     void setup (){
    
    
      pinMode (RED2, OUTPUT) ;// define LED as output interface
      pinMode (RED1, OUTPUT) ;// define LED as output interface
      pinMode (YELLOW, OUTPUT) ;// define LED as output interface
      
      pinMode (OBSTACLE_AVOIDANCE_SENSOR_PIN, INPUT_PULLUP);
      
      myservo1.attach(8);
      myservo2.attach(7);
      }
    
    
     void loop (){
     
      // When the obstacle avoidance sensor detects a signal, LED flashes
      if (digitalRead(OBSTACLE_AVOIDANCE_SENSOR_PIN) == LOW) {
        myservo1.write(SERVO_ALERT); // tell servo to go to position in variable 'pos'
        myservo2.write(SERVO_ALERT);
        flashLights();
        }
        else {
          myservo1.write(SERVO_PAUSE);
          myservo2.write(SERVO_PAUSE);
          digitalWrite (RED2, LOW);
          digitalWrite (RED1, LOW);
          digitalWrite (YELLOW, LOW);
        }
    }
    
    
    // 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
    }

     

    And this is parameters.h

     

    // Constants and application parameters
    #include 
    
    
    #define LONG_DELAY 2000     // Long delay in LED flashing sequence
    #define MID_DELAY 1000      // Mid duration delay
    #define SHORT_DELAY 200     // Short delay in LED flashing sequence
    
    
    #define RED2 11   // RED2 LED
    #define RED1 10   // RED1 LED
    #define YELLOW 9  // Yellow LED
    
    
    #define RED_FLASHING_CYCLE 5  // Red LEDs flashing loop duration
    
    
    #define SERVO1 7    // Pin servo 1
    #define SERVO2 8    // Pin servo 2
    
    
    #define SERVO_ALERT 90    // Servo position alert (maybe vice-versa)
    #define SERVO_PAUSE 0     // Servo position pause
    
    
    #define OBSTACLE_AVOIDANCE_SENSOR_PIN 3

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

    Hello Edward.

     

    Now button (that replace the reed sensor but the code remains unchanged) and the servos (I have only one here but it should work with two as well) moves accordingly.

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

     

    This is the code:

     

    //Level_crossing
    
    
    #include "parameters.h"
    
    
    Servo myservo1; // create servo object to control a servo
    Servo myservo2;
    
    
     void setup (){
    
    
      pinMode (RED2, OUTPUT) ;// define LED as output interface
      pinMode (RED1, OUTPUT) ;// define LED as output interface
      pinMode (YELLOW, OUTPUT) ;// define LED as output interface
      
      pinMode (OBSTACLE_AVOIDANCE_SENSOR_PIN, INPUT_PULLUP);
      
      myservo1.attach(8);
      myservo2.attach(7);
      }
    
    
     void loop (){
     
      // When the obstacle avoidance sensor detects a signal, LED flashes
      if (digitalRead(OBSTACLE_AVOIDANCE_SENSOR_PIN) == LOW) {
        myservo1.write(SERVO_ALERT); // tell servo to go to position in variable 'pos'
        myservo2.write(SERVO_ALERT);
        flashLights();
        }
        else {
          myservo1.write(SERVO_PAUSE);
          myservo2.write(SERVO_PAUSE);
          digitalWrite (RED2, LOW);
          digitalWrite (RED1, LOW);
          digitalWrite (YELLOW, LOW);
        }
    }
    
    
    // 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
    }

     

    And this is parameters.h

     

    // Constants and application parameters
    #include 
    
    
    #define LONG_DELAY 2000     // Long delay in LED flashing sequence
    #define MID_DELAY 1000      // Mid duration delay
    #define SHORT_DELAY 200     // Short delay in LED flashing sequence
    
    
    #define RED2 11   // RED2 LED
    #define RED1 10   // RED1 LED
    #define YELLOW 9  // Yellow LED
    
    
    #define RED_FLASHING_CYCLE 5  // Red LEDs flashing loop duration
    
    
    #define SERVO1 7    // Pin servo 1
    #define SERVO2 8    // Pin servo 2
    
    
    #define SERVO_ALERT 90    // Servo position alert (maybe vice-versa)
    #define SERVO_PAUSE 0     // Servo position pause
    
    
    #define OBSTACLE_AVOIDANCE_SENSOR_PIN 3

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

    Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

     

    C:\Users\Edward\Desktop\rr patterns\my edit crossing bell and lights\RR_Crossing_Bell_Synched_my_edit_1\RR_Crossing_Bell_Synched_my_edit_19\RR_Crossing_Bell_Synched_my_edit_19.ino:3:30: fatal error: parameters.h: No such file or directory

     

         #include "parameters.h" 

     

                                  ^

     

    compilation terminated.

     

    exit status 1

    Error compiling for board Arduino/Genuino Uno.

     

    This report would have more information with

    "Show verbose output during compilation"

    option enabled in File -> Preferences.

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

    how do I add H parameter to library?

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

    No need library. With the IDE open, on the top-right side, you find "add new tab"

    Then write down "parameters.h" and paste the second block of code in the empty file. Take a look at the images below.

     

    1.

    image

    2.

    image

     

    3.

    image

     

    Enrico

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

    ok now i get

    Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

     

    In file included from C:\Users\Edward\Desktop\rr patterns\my edit crossing bell and lights\RR_Crossing_Bell_Synched_my_edit_1\RR_Crossing_Bell_Synched_my_edit_19\RR_Crossing_Bell_Synched_my_edit_19.ino:4:0:

     

    parameters.h:2: error: #include expects "FILENAME" or <FILENAME>

     

                                     #include 

     

                                               ^

     

    exit status 1

    #include expects "FILENAME" or <FILENAME>

     

    This report would have more information with

    "Show verbose output during compilation"

    option enabled in File -> Preferences.

     

    i have to got to dr appointment now will be back in 2 hrs if you are still up we can continue if not maybe tomorrow. Thank you Enrico for your help you have been a blessing :-)))

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

    Sorry there was an error pasting my code.

     

    #include <Servo.h>

     

    (moved here so you include only one file in the .ino main application)

     

    Enrico

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

    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 but if your ever around Detroit Michigan definitely look me up I would be glad to take you out for a few beers!!!

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

    It was a pleasure, I have done just what hundreds of people do here. Trust in sharing knowledge.

     

    Enrico

    • 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