Railroad grade crossing project

Please see photo. I am about to dive into this project. It will have IR sensors, flashing LEDs, and servos for the gates.

I am a little intimidated about incorporating all these in one program. I am afraid of state machines. Any advice on a simplistic form of detection on the track?

I was thinking of just getting everything operating in a manual mode first.

Thanks!

Kennedy

161A5974-9286-435E-85E6-166F6744B60D.png

What video? Game of Thrones?

Had to change to a screenshot

I am afraid of state machines.

There is nothing to be afraid of.

Start by writing down each state that the program can be in. Presumably there will be at least

VEHICLES_CROSSING
WARNING IN PROGRESS
VEHICLES STOPPED

and possibly others. It is your project so you know the details

Seasea19:
Had to change to a screenshot

See Simple image insertion

Hi,
OPs pic.
161A5974-9286-435E-85E6-166F6744B60D.jpg
Tom... :slight_smile:

161A5974-9286-435E-85E6-166F6744B60D.jpg

Seasea19:
I am a little intimidated about incorporating all these in one program.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

I am afraid of state machines.

Don't be. "State machine" is just a fancy name for a system that uses one or more variables to keep track of progress through a series of actions.

...R

I am trying to modify the blink without delay example to alternate 2 LEDs pins (13 and 12) to flash alternately. Only pin 13 flashes. is the issue with the "if" statements

created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin13 =  13;// the number of the LED pin
const int ledPin12 =  12;


// Variables will change:
int ledState13 = LOW;// ledState used to set the LED
int ledState12 = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 800;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState13 == LOW) {
      ledState13 = HIGH;
    } else {
      ledState13 = LOW;


      // if the LED is off turn it on and vice-versa:
    if (ledState12 == HIGH) {
      ledState12 = LOW;
    } else {
      ledState12 = LOW;
    }
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin13, ledState13);
    digitalWrite(ledPin12, ledState12);
  }
}

Seasea19:
I am trying to modify the blink without delay example to alternate 2 LEDs pins (13 and 12) to flash alternately. Only pin 13 flashes. is the issue with the "if" statements

created 2005

by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin13 =  13;// the number of the LED pin
const int ledPin12 =  12;

// Variables will change:
int ledState13 = LOW;// ledState used to set the LED
int ledState12 = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 800;          // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
    if (ledState13 == LOW) {
      ledState13 = HIGH;
    } else {
      ledState13 = LOW;

// if the LED is off turn it on and vice-versa:
    if (ledState12 == HIGH) {
      ledState12 = LOW;
    } else {
      ledState12 = LOW; //<------------- shouldn't this be HIGH?????
    }
    }

// set the LED with the ledState of the variable:
    digitalWrite(ledPin13, ledState13);
    digitalWrite(ledPin12, ledState12);
  }
}

In the code you posted I pointed where I think the mistake lies (I put the comment in the above quote). :wink:

Hope that helps....

They both come on then alternate once. Then resumes flashing pin 13 only.

if (ledState12 == HIGH) {
ledState12 = LOW;
} else {
ledState12 = LOW; //<------------- shouldn't this be HIGH?????
}

I made that change, correction pin 13 is on constantly. 12 is flashing correctly. These are common anode LEDs.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

So this a railroad grade crossing project. I just need to alternate red pairs of LEDS. I am not using delay because I plan to add in other functions later.

“I made that change, correction pin 13 is on constantly. 12 is flashing correctly. These are common anode LEDs.”

If this is still happening, show us an image of the actual wiring.

Also, show us your current sketch that is not working.

Seasea19:
I am afraid of state machines.

Depends on in which country you live, I suppose! :astonished:

// constants won't change. Used here to set a pin number:
const int ledPin13 =  13;// the number of the LED pin
const int ledPin12 =  12;


// Variables will change:
int ledState13 = LOW;// ledState used to set the LED
int ledState12 = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 800;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState13 == LOW) {
      ledState13 = HIGH;
    } else {
      ledState13 = HIGH;


      // if the LED is off turn it on and vice-versa:
    if (ledState12 == HIGH) {
      ledState12 = LOW;
    } else {
      ledState12 = HIGH;
    }
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin13, ledState13);
    digitalWrite(ledPin12, ledState12);
  }
}

"State Machines" seems to be a way to intimate folks - I get confused when going through the write-ups - but have been grouping and putting similar functions together or as noted above "VEHICLES_CROSSING"

Once you get started the folks are great at pointing or nudging you in a good direction

Good luck - looks like a fun project

:slight_smile:

// constants won't change. Used here to set a pin number:
const int ledPin13 =  13;// the number of the LED pin
const int ledPin12 =  12;


// Variables will change:
int ledState13 = LOW;// ledState used to set the LED
int ledState12 = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 800;           // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState13 == LOW)
    {
      ledState13 = HIGH;
    }
    else
    {
      ledState13 = LOW;
    }

    // if the LED is off turn it on and vice-versa:
    if (ledState12 == HIGH)
    {
      ledState12 = LOW;
    }
    else
    {
      ledState12 = HIGH;
    }
  }

  // set the LED with the ledState of the variable:
  digitalWrite(ledPin13, ledState13);
  digitalWrite(ledPin12, ledState12);

}

OR

// constants won't change. Used here to set a pin number:
const int ledPin13 =  13;// the number of the LED pin
const int ledPin12 =  12;


// Variables will change:
int ledState13 = LOW;// ledState used to set the LED
int ledState12 = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 800;           // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState13 == LOW)
    {
      ledState13 = HIGH;
    }
    else
    {
      ledState13 = LOW;
    }

    ledState12 = !ledState13;

    //    // if the LED is off turn it on and vice-versa:
    //    if (ledState12 == HIGH)
    //    {
    //      ledState12 = LOW;
    //    }
    //    else
    //    {
    //      ledState12 = HIGH;
    //    }
  }

  // set the LED with the ledState of the variable:
  digitalWrite(ledPin13, ledState13);
  digitalWrite(ledPin12, ledState12);

}