Code for Arduino - flashing lights

Post all your model railway electronic problems, solutions and discoverys here.
User avatar
dubdee1000
Posts: 891
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Code for Arduino - flashing lights

Postby dubdee1000 » Sun Jan 06, 2019 9:25 pm

I'm new to coding, so struggling to pick it up as i go along. I'm finding some of it quirky.

What i wanted to do was to use a hall sensor to flash an led and mimic the arcing of a third rail. The EMU would be fitted with a magnet so that as it went over the sensor, it would be detected and flash the led. What i really wanted to do was to flash the LED a couple of times, at differing time intervals. And once i got this going, to use the Arduino to flash several LEDs around the layout using other Hall sensors. Adding another circuit was pretty easy once the first one was setup

To some extent, this was successful, the pass of the magnet starts the program, the LED gives a long flash, then a couple of shorter ones (which could easily be changed it you wanted). You can see it here :

https://youtu.be/ruY6W--0WIw

However, i used "delay" when i coded this, so the arduino won't light the other LED's until its completed the sequence of the first one to activate.

You can see the second circuit not starting when the pass is too fast here:

https://www.youtube.com/watch?v=iT_BMZlSwBU


I think this is crude - a timer would perhaps be better. But what i wanted to avoid was the LED blinking on-off-on-off in a regular fashion like so many of the tutorials do. I also wanted LED1 to flash differently to LED2, differently to LED3 etc

In reality, this isn't going to make much difference since i'm not intending to put the sensors very close to one another so unless the EMU is moving really fast, the program will complete before the bogie magnet goes over the next sensor, but it would be nice to have the option. Can anyone advise on how to code this better? Thanks,

The code is here:


const int LedPin= 8;
const int HallPin= 2;
const int LedPin1= 9;
const int HallPin1= 4;


void setup() {
pinMode(LedPin, OUTPUT);
pinMode(HallPin, INPUT);
pinMode(LedPin1, OUTPUT);
pinMode(HallPin1, INPUT);

}

void loop() {
int value=digitalRead(HallPin);

if (value == LOW)

{

digitalWrite(LedPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(LedPin, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(LedPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LedPin, LOW); // turn the LED off by making the voltage LOW
delay(200); // wait for a second
digitalWrite(LedPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LedPin, LOW); // turn the LED off by making the voltage LOW
}
else {
digitalWrite(LedPin, LOW);
}

{ int value=digitalRead(HallPin1);

if (value == LOW)

{
digitalWrite(LedPin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(LedPin1, LOW); // turn the LED off by making the voltage LOW
delay(200); // wait for a second
digitalWrite(LedPin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LedPin1, LOW); // turn the LED off by making the voltage LOW
delay(300); // wait for a second
digitalWrite(LedPin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LedPin1, LOW); // turn the LED off by making the voltage LOW

}

MikeTrymTrains
Posts: 17
Joined: Mon Jul 02, 2018 9:58 pm

Re: Code for Arduino - flashing lights

Postby MikeTrymTrains » Fri Jan 11, 2019 7:39 pm

Hi I don't have a hall sensor to try the code that I put tougher. Can you try it to see if it work with the hall sensor if it works maybe we can add random flashes to it and more sensors.

Code: Select all

const int LedPin = 8;
const int HallPin = 2;
const int LedPin1 = 9;
const int HallPin1 = 4;

void setup()
{
  pinMode(LedPin, OUTPUT);
  pinMode(HallPin, INPUT);
  pinMode(LedPin1, OUTPUT);
  pinMode(HallPin1, INPUT);
}

void loop()
{
  static int state = 1; // initial state is 1, the "idle" state.
  static unsigned long ts;  // To store the "current" time in for delays.

  switch (state)
  {
    case 1:
      if (digitalRead(HallPin) == LOW)
        state = 2;
      break;
    case 2:
      digitalWrite(LedPin, HIGH); // Turn on the light
      ts = millis();  // Remember the current time
      state = 3;  // Move to the next state
      break;
    case 3:
      if (millis() - ts > 200)
      {
        state = 4;
      }
      break;
    case 4:
      digitalWrite(LedPin, LOW); // Turn off the light
      ts = millis();  // Remember the current time
      state = 5;
      break;
    case 5:
      if (millis() - ts > 500)
      {
        state = 6;
      }
      break;
    case 6:
      digitalWrite(LedPin, HIGH); // Turn on the light
      ts = millis();  // Remember the current time
      state = 7;  // Move to the next state
      break;
    case 7:
      if (millis() - ts > 50)
      {
        state = 8;
      }
      break;
    case 8:
      digitalWrite(LedPin, LOW); // Turn off the light
      ts = millis();  // Remember the current time
      state = 9;
      break;
    case 9:
      if (millis() - ts > 200)
      {
        state = 10;
      }
      break;
    case 10:
      digitalWrite(LedPin, HIGH); // Turn on the light
      ts = millis();  // Remember the current time
      state = 11;  // Move to the next state
      break;
    case 11:
      if (millis() - ts > 50)
      {
        state = 12;
      }
      break;
    case 12:
      // We only get here when forced from outside.
      digitalWrite(LedPin, LOW); // Turn off the light
      state = 1;  // Return to the "idle" state.
      break;
  }

  static int state2 = 101; // initial state is 101, the "idle" state.
  static unsigned long ts2;  // To store the "current" time in for delays.

  switch (state2)
  {
    case 101:
      if (digitalRead(HallPin1) == LOW)
        state2 = 102;
      break;
    case 102:
      digitalWrite(LedPin1, HIGH); // Turn on the light
      ts2 = millis();  // Remember the current time
      state2 = 103;  // Move to the next state
      break;
    case 103:
      if (millis() - ts2 > 200)
      {
        state2 = 104;
      }
      break;
    case 104:
      digitalWrite(LedPin1, LOW); // Turn off the light
      ts2 = millis();  // Remember the current time
      state2 = 105;
      break;
    case 105:
      if (millis() - ts2 > 200)
      {
        state2 = 106;
      }
      break;
    case 106:
      digitalWrite(LedPin1, HIGH); // Turn on the light
      ts2 = millis();  // Remember the current time
      state2 = 107;  // Move to the next state
      break;
    case 107:
      if (millis() - ts2 > 100)
      {
        state2 = 108;
      }
      break;
    case 108:
      digitalWrite(LedPin1, LOW); // Turn off the light
      ts2 = millis();  // Remember the current time
      state2 = 109;
      break;
    case 109:
      if (millis() - ts2 > 300)
      {
        state2 = 110;
      }
      break;
    case 110:
      digitalWrite(LedPin1, HIGH); // Turn on the light
      ts2 = millis();  // Remember the current time
      state2 = 111;  // Move to the next state
      break;
    case 111:
      if (millis() - ts2 > 50)
      {
        state2 = 112;
      }
      break;
    case 112:
      // We only get here when forced from outside.
      digitalWrite(LedPin1, LOW); // Turn off the light
      state2 = 101;  // Return to the "idle" state.
      break;
  }
}

User avatar
TimberSurf
Posts: 2334
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: Code for Arduino - flashing lights

Postby TimberSurf » Fri Jan 11, 2019 9:06 pm

You can also replace the fixed times with randomised times, but control the range that they can be

randNumber = random(100,500); // generate random number between 100 & 500

Sometimes the random in Arduino is less so, but can be better if this is used: https://www.arduino.cc/reference/en/lan ... andomseed/
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
dubdee1000
Posts: 891
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Re: Code for Arduino - flashing lights

Postby dubdee1000 » Sat Jan 12, 2019 1:58 pm

Many thanks both. Mike - i don't know what you drink but i owe you a pint of it. Code compiled perfectly and works a charm. I was amazed to see the complexity of it. I've really enjoyed using the arduino, but i've found the coding to be a challenge.

See the video in action:

https://youtu.be/HsEBgmHnLYw

The next stage is physically build some more circuits and then revise the code to match. I think around 8 circuits should be enough to cover the layout, so that'll be this afternoon's challenge. Once i get it all working, i'll put the shrink ray on it

User avatar
dubdee1000
Posts: 891
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Re: Code for Arduino - flashing lights

Postby dubdee1000 » Sat Jan 12, 2019 4:02 pm

And just to prove the now simultaneous triggering and function. I might vary some of the timing so they're not all the same :

https://youtu.be/cdMhTtqw6Ek

MikeTrymTrains
Posts: 17
Joined: Mon Jul 02, 2018 9:58 pm

Re: Code for Arduino - flashing lights

Postby MikeTrymTrains » Sat Jan 12, 2019 4:14 pm

Thanks for posting the video it's good to see it working. If you don't understand the code I can help you and add more sensors and leds code to it. I understand a bit of programming but I just find bits of code around the internet and see if I can use it with what I need it to do. Here is a link to the code I use with your project. https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/ It might help explain the code to you better than I can.

User avatar
TimberSurf
Posts: 2334
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: Code for Arduino - flashing lights

Postby TimberSurf » Sat Jan 12, 2019 4:42 pm

JFYI, the other Timb and I started a thread on arduino's here that makes interesting reading and Rudy has just started one here

There is a dozen ways to program what you want! A lot boils down to personally preferred methods!
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
dubdee1000
Posts: 891
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Re: Code for Arduino - flashing lights

Postby dubdee1000 » Sat Jan 12, 2019 4:51 pm

I know where you're coming from re the copying and pasting of code, but man I've struggled with this over the last week, people pointing me towards blink without delay when that really didn't help. I really struggled to get my head about the 'currentmillis = millis' and whilst some would explain it, they explained it in such a way that didn't really help. I'm not blaming them - I know I need to put the effort in myself, but its a bit like learning greek - you struggle to understand not just the words, but the way the language is constructed.

I followed your code perfectly - it was really clear and logical, plus importantly, easy to adapt. There are perhaps ways of writing it in a shorter way, but i doubt i would have followed this, so i was more than happy to see it in such a detailed way.

I'll add the code for one more circuit and then put this to bed pending some super bright leds and mini hall sensors coming.

MikeTrymTrains
Posts: 17
Joined: Mon Jul 02, 2018 9:58 pm

Re: Code for Arduino - flashing lights

Postby MikeTrymTrains » Sat Jan 12, 2019 9:24 pm

I understand how you fill I still don't know how the blink without delay works I looked on the Arduino forums for hours to try to find out how to do it but not able to solved it. The only thing I found was the link to the website that I posted. It took me six months to find the website after I got my Arduino and now my projects have grown bigger from two Arduino boards running different code down to one board running loads of code. It's been fun doing it but I can see why so many people give up with it. When I first look at using the Arduino I though the forums are good and a lot of people that can help. All I wanted was traffic lights and pedestrian crossing and to stop the Faller trucks and bus. But all the help was learn blink without delay that I can not understand. I'm able to now change code and get projects running. But it has slow down my train layout build :) . Here is a link to the early stages using two Arduino boards I can now use one board and all the bugs have been sorted out :D . https://www.youtube.com/watch?v=eVsJr6-w5-0

User avatar
dubdee1000
Posts: 891
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Re: Code for Arduino - flashing lights

Postby dubdee1000 » Sun Feb 03, 2019 11:02 pm

Thanks Mike, for running your eye over the code.

I desoldered the hall sensor from the board and test fitted it into the track bed to test. It is very reliable in operation and works well with the train going in one direction, but in reverse, it is evident that the effect is too long, the bursts too slow. Fortunately, that's an easy fix and just needs the code tweaking. The next stage is to fix it and the other sensors/LEDs into the track more permanently.

In action:
https://youtu.be/LkfrgZQyoaE


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 1 guest