Traffic lights with sensor

Hi, I'm trying to set up traffic lights on my ho railway, so far with no success. I've followed a couple of lessons on line but they don't work for me. I don't want anything fancy, just a 3 light system that changes as a train passes.

Thanks in advance

What looks like the failing part?

And welcome to the community.

TIA for posting code, schematics and links to where your project came from if not your own brain!

a7

Picture too. :sunglasses:

Thanks for replying, what I'm trying to do is have a green light on until light is blocked to the sensor, then the yellow for a couple of seconds, then a red until the sensor is clear. I'm very new to programing so it's not hard to get me confused!

I've tried 2 or 3 different sketches which resulted in lights coming on when they shouldn't, so I wrote this one, I realise it's pretty basic but it works up to a point, the yellow light comes on although it's not supposed to, I suspected the Mega I have was defective so I tried the Nano with the same results

int sensor = A0;
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int POWER = 10;
int valA0 = analogRead(sensor);
int sensePin1=A0;
void setup()
{
Serial.println(valA0);
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(POWER, OUTPUT);
}
void loop(){
{
analogRead(valA0);
Serial.println(valA0);

int valA0=analogRead(sensePin1);
if (valA0 > 500)
{
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
digitalWrite(POWER, HIGH);
}
else if (valA0 < 500)
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, HIGH);
digitalWrite(POWER, HIGH);
delay(2000);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
digitalWrite(POWER, HIGH);
}
}

Start with a sketch that changes Green, Yellow, Red.

So, do LEDs come on with LOW or HIGH? That’s why we want to see a schematic…

This

int valA0 = analogRead(sensor);

just reads the sensor once, it does not make a continuing relationship between sensor and valA0.

Later this

 analogRead(valA0);

is not reading a pin, it’s reading a pin numbered some random sensor value.

There are a number of things that look like you could profit from reading reading reading and experimenting with a few simple examples.

There are some in the IDE, or find online resources and follow them slavishly - there is no room for improvisation in programming!

a7

I did that, but the yellow came on with the red. Obviously I screwed up somewhere, but where?

They come on HIGH,

I have no community college, no knowledgeable acquaintances or school kids to ask, so I have to try my best and look for help from the internet.
Why does

int valA0 = analogRead(sensor);

only read once?

What does this mean

Later this

 analogRead(valA0);

is not reading a pin, it’s reading a pin numbered some random sensor value.? It's connected to pin A0.

I've looked on line and compared other sketches, but still don't understand.

Why does analogRead(valA0); not read a pin, what should it be?

I value advice, criticism doesn't help.

See Notes --

Forget about the sensor junk for now.
Just make a sketch that runs the lights (and that's all).

You have received advice, not criticism.

You ask good questions, but it is not going to be an effective learning path to post non-working code and get answers to all the questions it raises one by one, it woukd be like spoon feeding an elephant.

I strongly advise you to google

 basic arduino programming

You will get a zillion hits. Use your best common sense to find tutorial courses or other sites that claim to take you step by baby step from noob to pro.

Give a few of them 10 to 15 minutes each, from where they begin.

This will be long enough for you to determine if they match your level of knowledge, whether you like the way the material is presented, be it video, or reading or whatever, your “learning style” as they say and whether you can tolerate or you even like the teacher. People like the ice tea guy, I do not. I like to watch the breezy young chick that makes everything look like fun… your mileage will vary, as is said. You may prefer to catch a few buzz words and google on your own. Add Arduino and C++ to just about any search and it will focus your results. There are millions of ppl using the platform, a high percentage like to share and show off and lead the way out of the darkness.

Above all be patient. There is no instant gratification in this hobby, we all started knowing nothing and no one gets very good at all without taking time and doing the work it will take.

All this is hard until it is easy, but the rewards are great if you stick with it.

a7

2 Likes

Hello tuggie
Take some time, study the IPO model and take a piece of paper plus a pencil and design a program structure. Identify modules could to be coded and tested separetly. All modules merged together after sucessfull testing to the needed project.
It is highly recommented to take a view into some powerful C++ instructions like STRUCT, ENUM, ARRAY, CASE/SWITCH,UDT, RANGE-BASED-FOR-LOOP, etc, too.

Have a nice day and enjoy coding in C++.

I've created a sketch where green stays on for 5 seconds, yellow and red for 1.5secs and red 5secs, then start again. How can I get this to be controlled by the sensor?
Thanks

Read the sensor as you did in your earlier program. If it's more than 500, just show green. Otherwise, run your sequence.

In the last sequence I couldn't read the serial monitor because I'd made an error with the pin numbers, but I don't know how it should be, I'd be grateful for advice on the correct procedure.

Thanks again.

Post what you have.

Maybe you should write a sketch that deals solely with the 'sensor' reliably and then that can be integrated later.

Hello tuggie
See below a simple sketch to interlock the photoelectric switch and a FSM. You may add the led sequences and a timer to the FSM.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/traffic-lights-with-sensor/967589
  Tested with Arduino: Mega[X] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Traffic lights with sensor"
constexpr byte Photoelectric_Pin {A9};
enum {State1, State2, State3, Wait} status = Wait;
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Time   : ")), Serial.println(__TIME__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  int photoelectric_Switch = analogRead(Photoelectric_Pin);
  static bool photoelectric_SwitchState {false};
  if (photoelectric_Switch < 512) {
    if (!photoelectric_SwitchState) photoelectric_SwitchState = !photoelectric_SwitchState,  status = State1;
  } else {
    if (photoelectric_SwitchState) photoelectric_SwitchState = !photoelectric_SwitchState,  status = State2;
  }
  switch (status) {
    case Wait:
      break;
    case State1:
      Serial.println(F("case State1:"));
      status = Wait;
      break;
    case State2:
      Serial.println(F("case State2:"));
      status = Wait;
      break;
    case State3:
      Serial.println(F("case State3:"));
      status = Wait;
      break;
  }
}

Have a nice day and enjoy coding in C++.

Sorry about posting the code here, but I couldn't figure out how to paste to this forum, fum\nny, I managed last time.

I get the error: 'else' without a previous 'if'. I see an earlier 'if' but can't figure out my error. There may be others but I have to cure this one first

Thanks again.

int GREEN = 3;
int YELLOW = 4;
int RED = 5;
int POWER = 10;
int valA0 = analogRead(A0);
int sensePin1=A0;
void setup() {
// put your setup code here, to run once:
Serial.println(valA0);
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(POWER, OUTPUT);
}

void loop() // put your main code here, to run repeatedly:
{
analogRead(valA0);
Serial.println(valA0);
int valA0=analogRead(sensePin1);
if (valA0 > 500)
digitalWrite(GREEN, LOW);//LOW is on
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, HIGH);
else (valA0 < 500)
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
delay(1500);
digitalWrite(GREEN,HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);

}

I did my best to add my led sketch to this, but all 3 lights stay on now! I have to say I'm not enjoying coding that much!
As for 'have a nice day' too late! But I do appreciate the help.

int sensor = A0;
int GREEN = 3;
int YELLOW = 4;
int RED = 5;
int POWER = 10;
int valA0 = analogRead(sensor);
int sensePin1=A0;

#define ProjectName "Traffic lights with sensor"
constexpr byte Photoelectric_Pin {A9};
enum {State1, State2, State3, Wait} status = Wait;
// -------------------------------------------------------------------

void setup() {
Serial.println(valA0);
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(POWER, OUTPUT);
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(FILE);
Serial.print(F("Date : ")), Serial.println(DATE);
Serial.print(F("Time : ")), Serial.println(TIME);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
int sensor = A0;
int GREEN = 3;
int YELLOW = 4;
int RED = 5;
int POWER = 10;

int sensePin1=A0;

}
void loop () {
unsigned long currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
int photoelectric_Switch = analogRead(Photoelectric_Pin);
static bool photoelectric_SwitchState {false};
if (photoelectric_Switch < 512) {
if (!photoelectric_SwitchState) photoelectric_SwitchState = !photoelectric_SwitchState, status = State1;
} else {
if (photoelectric_SwitchState) photoelectric_SwitchState = !photoelectric_SwitchState, status = State2;
}
switch (status) {
case Wait:
break;
case State1:
Serial.println(F("case State1:"));
status = Wait;
break;
case State2:
Serial.println(F("case State2:"));
status = Wait;
break;
case State3:
Serial.println(F("case State3:"));
status = Wait;
break;
int valA0=analogRead(sensePin1);
if (valA0 > 500)
{
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
delay(1500);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);

digitalWrite(POWER, HIGH);
}
else if (valA0 < 500)
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
digitalWrite(POWER, HIGH);
}
}
}