Father, son project lego/arduino

Evening all
First time I have ever posted on any forum so please don't flog the hell out of me.

I have spent the last 2 months just about every night reading previous posts and trying to get my head around writing a code for the project i have in mind with my son. i can get the individual components to work on their own but integrating them together is going to send me to an early grave.

i have a basic level of coding but this project is tearing my hair out and the worst thing is that my 5 year old son keeps asking me every morning if the ding dings are working today (ding dings are his words for level crossing)

the project is a level crossing lights/ bell / boom gates/ ultrasonic integration

breakdown of process
state = static awaiting trigger input

trigger = train passes through the ultrasonic "gate"
(i chose to use ultrasonics due to the new lego trains also use ir and didn't want interference and the room has variable ambient light conditions so i thought light dependent resistors would be subject to calibration issues)

action= two leds flash in 1 second alternating flash sequence, boom gates lower (servo motor)
and a electronic bell receives a 50 millisecond pulse every second

I am using a relay board to control the leds which total 8 and the bell which is 12 volt powered

this sequence continues until the train clears the ultrasonic gate and 5 seconds passes to allow the train to clear the level crossing
I contemplated having an additional ultrasonic gate the other side of the level crossing to signal when the crossing was clear but have given up trying to figure that one out.

if anyone can assist with this project i would be greatly appreciative. I have never let anything beat me so far but beginning to think i may have bitten off more than i can chew with this one

if anymore info is needed please ask

Can you post the code you have so far?

Please post the code that you have tried. Include a description of what the code actually does (versus what you have stated that it should do). Also a schematic of your wiring so that we know what parts that you are using and how everything is connected.

(i chose to use ultrasonics due to the new lego trains also use ir and didn't want interference and the room has variable ambient light conditions so i thought light dependent resistors would be subject to calibration issues)

I think that was an error. The IR that the LEGO train uses is modulated, and the IR you could use for the gate can be not modulated so the two would not interfere.

When you post your code and schematic could you also say exactly what it does and why you think it is wrong.

Hi again
this is as far as i can get with integrating the codes together

this part is the beginning of the code
my thoughts were that i have a ultrasonic “gate” spanning the distance across the lego tracks measuring about 125mm / 5 inches
when the train passes through the “gate” the ultrasonic detects a range below 8 cm/ 2.5 inches
and then starts the sequence of everything else happening
lights start to flash at 1 second alternating flashes
bell sounds with every second
boom gates close

once the distance within the gate is back above the trigger distance the function will stop and everything will revert back to its static state
lights off
bell off
boomgates in raised position

i know the code i have included with this post is very basic but its at this point I start tearing my hair out
i have used digitalWrite at the end of the code just for testing purposes(light comes on when i move my hand within 8cm of the ultrasonic)
when i remove this section of the code and start to write in other functions everything turns to crap

i was just starting with the lights and building the code up one function at a time

cheers guys

#include <NewPing.h>

int TRIGGER = 12;
int ECHO = 11;
int led = 2;

NewPing sonar(TRIGGER, ECHO);

void setup() {
// put your setup code here, to run once:

Serial.begin (115200);
pinMode (led, OUTPUT);

void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
int distance = sonar.ping_cm();

Serial.print (distance);
Serial.println (“cm”);

if (sonar.ping_cm() < 8)
digitalWrite (led, HIGH);
digitalWrite (led, LOW);

delay (250);


ultrasonic_start3_best_so_far.ino (527 Bytes)

sorry forgot to add
my setup is as follows

arduino uno board
pin 2 to led
pin 12 to echo of ultrasonic module
pin 13 to trigger of ultrasonic module

I'm happy to re-route as necessary if someone suggests any improvements and i know i haven't included any pin out for the other led, bell or servo control as yet because i cant get the code to work. happy to put the additional outputs to any pins

thanks again

How much help are you looking for? Is this a learning exercise or do you just want to get it working?

I'd suggest that you're going to need a state machine and thus you will have to learn to use millis rather than delay. The blink without delay example will show you this.

Is the rangefinder working? Do you get proper ranges in serial monitor? Does the LED light when the range is less than smiley face?

when i remove this section of the code and start to write in other functions everything turns to crap

It is the code where everything turns to crap that we want to see along with a schematic. A schematic means a diagram of the circuit, not a word description nor a physical fritzing layout diagram crap.

As you have said what you have posted works so we are not interested in that bit.

In that code what is the variable distance doing? You set it up and never use it. Maybe is is distance you could be compairing with cool smile not getting th distance again.

Please read he how to use this forum sticky post. It will tell you how to post code correctly to avoid things like the cool smile effect.

Surprised no-one said it already: please put your code between code [/] tags. Then at least it’s readable and without funny faces.
Don’t use delay(). Use millis() for timing. See the “blink without delay” example for an implementation of this.
Add a variable that tells your system when it last saw a train.

You would get something like this (note: this is not meant to compile, it’s incomplete pseudo code but should get you the correct idea on how to chain it all together):

unsigned long int lastSeenTrain;

void loop() {

  if (sonar.ping_cm()  < 8) lastSeenTrain = millis();

  if (millis() - lastSeenTrain) < 5000 {
  else openGates();


closeGates() tells your servo to put the gates down.
doDingDing() sends out the ping pulses - again no delays. Can be as simple as this:

unsigned long int dingDingMillis;

void doDingDing() {
  if (dingDingMillis - millis()) > 1000) dingDingMillis = millis();
  if (dingDingMillis - millis()) < 50 doDing();

where doDing() is replaced by whatever you do to make the bells ring. LEDs can flash in a similar manner.

For your hardware: relays may not be the best solution here, they make a lot of noise and are slowish. Consider switching them for MOSFET based electronic switches.

hi guys
Following up on everyone's questions
yes the rangefinder is working. the led does light whenever i move an object closer than 8 cm to the sensor
and it goes off when i move the object away from the sensor

sorry about the smiley face bit i didn't know that happened

how does one post a schematic diagram?

in terms of how much help I'm looking for? I have always loved a challenge and really don't want to back away from this one, just going to need help with getting my head around this. I do want to write the code myself and learn in the process so that in the future i may be able to help someone else.

I'll spend time today playing around with using the millis function today.
feel free to offer further advice

one thing that really helped me was a thread somewhere on here about organizing your code. Also learn to create your own functions. Making your own functions makes a long sketch infinitely easier to read and understand.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Your schematic is best in jpg or png format, either CAD or a picture of a hand drawn circuit.
You then post it as an image.

Tom... :slight_smile:

how does one post a schematic diagram?

You hit the reply box at the end of the thread, not use the quick reply. Then at the lower left their is a triangle, hit that and you will be able to attach an image file.

Even better if you after that add it to the body of the post.
See this image guide