I have problem with project

I have a problem with the LEDs flashing.
I need the LEDs to stay ON when needed and not flashing or blink.
How can I solve it? Thanks!

Video project: Arduino Block Signal for Model Trains - YouTube
Program: Arduino Block Signal Sketch - Google Docs

Hi, @
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

We have some members who view on platforms that find it difficult to view off forum locations.

Can you please post a circuit diagram of your project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

So write code that does that. It is almost trivial.

Post what you have written so far. Don't forget the code tags ( the </> icon ) press and paste your code between what pops up.

1 Like

int sensePin1=A0;
int sensePin2=A1;
int RED=6;
int YELLOW=5;
int GREEN=3;
int POWER=8;

void setup() {
Serial.begin(9600);
pinMode(RED,OUTPUT);
pinMode(YELLOW,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(POWER,OUTPUT);

}
enum SIGNALSTATES
{
ST_GREEN,
ST_RED1,
ST_RED2,
ST_YELLOW,
};
SIGNALSTATES signalState=ST_GREEN;

void loop() {
int valA1=analogRead(sensePin1);
int valA2=analogRead(sensePin2);
Serial.println(valA1);
Serial.println(valA2);
delay(200);

switch(signalState)
{
case ST_GREEN:
signalgreen(valA1, valA2);
break;
case ST_RED1:
signalred1(valA1, valA2);
break;
case ST_RED2:
signalred2(valA1, valA2);
break;
case ST_YELLOW:
signalyellow(valA1, valA2);
break;
}
}

void signalgreen(int valA1, int valA2){
digitalWrite(GREEN,LOW);
digitalWrite(RED,HIGH);
digitalWrite(YELLOW,HIGH);
digitalWrite(POWER,HIGH);

if (valA1<500 && valA2>500){
signalState=ST_RED1;
}
else if (valA1>500 && valA2<500){
signalState=ST_RED2;
}
}

void signalred1(int valA1, int valA2){
digitalWrite(GREEN,HIGH);
digitalWrite(RED,LOW);
digitalWrite(YELLOW,HIGH);
digitalWrite(POWER,HIGH);

if (valA1>500 && valA2<500){
signalState=ST_YELLOW;
}
}

void signalred2(int valA1, int valA2){
digitalWrite(GREEN,HIGH);
digitalWrite(RED,LOW);
digitalWrite(YELLOW,HIGH);
digitalWrite(POWER,HIGH);

if (valA1<500 && valA2>500){
signalState=ST_YELLOW;
}

}

void signalyellow(int valA1, int valA2){
digitalWrite(GREEN,HIGH);
digitalWrite(RED,HIGH);
digitalWrite(YELLOW,LOW);
digitalWrite(POWER,HIGH);
delay(5000);

if (valA1>500 && valA2>500){
signalState=ST_GREEN;
}
}

How to I make this change?

Hi,
First in the IDE, press CTRL-T, this will format your code with indents to show your code structure better, then post it in a new post using code tags.
To add code please click this link;

You code will then be in a scrolling window to make it easier to read.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

int sensePin1 = A0;
int sensePin2 = A1;
int RED = 6;
int YELLOW = 5;
int GREEN = 3;
int POWER = 8;


void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(POWER, OUTPUT);

}
enum SIGNALSTATES
{
  ST_GREEN,
  ST_RED1,
  ST_RED2,
  ST_YELLOW,
};
SIGNALSTATES signalState = ST_GREEN;

void loop() {
  int valA1 = analogRead(sensePin1);
  int valA2 = analogRead(sensePin2);
  Serial.println(valA1);
  Serial.println(valA2);
  //delay(200);

  switch (signalState)
  {
    case ST_GREEN:
      signalgreen(valA1, valA2);

      break;
    case ST_RED1:
      signalred1(valA1, valA2);

      break;
    case ST_RED2:
      signalred2(valA1, valA2);

      break;
    case ST_YELLOW:
      signalyellow(valA1, valA2);

      break;
  }
}

void signalgreen(int valA1, int valA2) {
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, HIGH);
  digitalWrite(YELLOW, HIGH);
  digitalWrite(POWER, HIGH);

  if (valA1 < 500 && valA2 > 500) {
    signalState = ST_RED1;
  }
  else if (valA1 > 500 && valA2 < 500) {
    signalState = ST_RED2;
  }
}

void signalred1(int valA1, int valA2) {
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, LOW);
  digitalWrite(YELLOW, HIGH);
  digitalWrite(POWER, HIGH);

  if (valA1 > 500 && valA2 < 500) {
    signalState = ST_YELLOW;
  }
}

void signalred2(int valA1, int valA2) {
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, LOW);
  digitalWrite(YELLOW, HIGH);
  digitalWrite(POWER, HIGH);

  if (valA1 < 500 && valA2 > 500) {
    signalState = ST_YELLOW;
  }

}

void signalyellow(int valA1, int valA2) {
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, HIGH);
  digitalWrite(YELLOW, LOW);
  digitalWrite(POWER, HIGH);
  delay(1000);

  if (valA1 > 500 && valA2 > 500) {
    signalState = ST_GREEN;
  }
}

You say, "on when needed"... how would the program know when it's "needed"? Switch contacts, or...? How does that fit with the existing logic? I see that it's driven by two analog inputs but how should they affect the circuit, exactly?

Actually, I see no code that does any flashing! Your question dwells on flashing so this is a little strange...

In the state machine, why is it necessary to pass valA1 and valA2 to each output function such as 'signalgreen()' ?

Please provide a description of what the program that you posted does exactly, and also a description of what new features or changes you want to make.

Also, we're still waiting for the circuit diagram that was requested in reply #2.

To decide if state needs to be changed :wink: It might not be the right place in best coding practices but that is something else.

valA1 and valA2 are better declared as global variables in this simple program. The comparison logic belongs in the switch case statement where its operation is visible, not hidden away in the functions.

The effect of the current scheme, is to obfuscate the mechanics of the state machine.

Look at the early code that flashed.

@martinez_jm OK now you have posted a totally different code. What you should now is to describe what it actually does and what you want it to do.

I must say the state variable setting is rather labyrinthine.

In my first post is the address of the video that shows how it works.
The operation is simple, the IR's take a reading, when one is covered one turns on a red light. When the other one is covered the light turns back to yellow and then to green.
The problem is that the LEDs do not light up correctly because they blink. I don't understand what part of the code makes them blink.

Is the same code, only tabulated

It is not.
It has a switch / case structure which makes it run quite differently. It also uses enum to define case states.

I find it is hard to reconcile your earlier posts claiming not to know how to do things and then the production of this code.
Now it you are going to mess us about I for one will be increasingly reluctant to help you.

I took the same code and tabbed it inside the arduino.
Honestly all I need to know is why the LEDs flicker and not stay on permanently. If your desire is not to help, I understand and thank you for your participation.

Hello
Well, I guess you need a timer function to get your project working proper.

I am a beginner in arduino and what I am wanting to do is to make a signal system for the model railroad I am building. The signals are permanent light, they do not flicker.
I understand the code I am writing but I can't identify anything that makes me think why they are flashing. Thanks!

What is being displayed in Serial Monitor?
You should change:

  Serial.println(valA1);
  Serial.println(valA2);

to:

  Serial.print(valA1);
  Serial.print('\t');
  Serial.println(valA2);

That way valA1 and valA2 will be on the same line and you will be able to tell which is which.

Well being a signals guy myself, I sympathize. However if this is for an ABS style signal reading block occupancy signals, it doesn't need any states at all. Basic ABS signalling logic is completely stateless. It's combinatorial logic. I should know because I've written it for several processors already.

The optimal coding is a nested 'if' pattern, since if some track occupancy signals are active, the rest are in a "don't care" condition and so don't need to be tested.

But in real (railroad) life, the sensors and relays are all slow - which is actually a good thing because it ensures positive indications. So, my code has a bit of filtering to make sure the signals don't change rapidly. But that part is completely separate from the aspect logic. It's hinted at in reply #18.