Programming help

So the current program is for lights on a roundhouse garage for a Model Railroad, when there is a train in the stall the sensor is tripped low so a red light turns on. If the stall is empty the sensor is not tripped it reads high and a green light turns on. I want to add led lights on the inside and want them to turn on for a specific time when a sensor change in state for any of the sensors.

int LDR1 = 0;
int LDR2 = 0;
int LDR3 = 0;
int LDR4 = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(A3, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  LDR1 = analogRead(A0);
  Serial.println(LDR1);
  if (LDR1 < 712)
  {
    digitalWrite(13, 1);
    digitalWrite(12, 0);
  }

  else if (LDR1 > 712)
  {
    digitalWrite(13, 0);
    digitalWrite(12, 1);
  }

  LDR2 = analogRead(A1);
  Serial.println(LDR2);
  if (LDR2 < 712)
  {
    digitalWrite(11, 1);
    digitalWrite(10, 0);
  }

  else if (LDR2 > 712)
  {
    digitalWrite(11, 0);
    digitalWrite(10, 1);
  }

  LDR3 = analogRead(A2);
  Serial.println(LDR3);
  if (LDR3 < 712)
  {
    digitalWrite(9, 1);
    digitalWrite(8, 0);
  }

  else if (LDR3 > 712)
  {
    digitalWrite(9, 0);
    digitalWrite(8, 1);

  } LDR4 = analogRead(A3);
  Serial.println(LDR4);
  if (LDR4 < 712)
  {
    digitalWrite(7, 1);
    digitalWrite(6, 0);
  }

  else if (LDR4 > 712)
  {
    digitalWrite(7, 0);
    digitalWrite(6, 1);`Use code tags to format code for the forum`
  }
}


int LDR1 = 0;

type or paste code here

your code checks 4 sensors - which one is the stall sensor?

There is no code for the "turn on for a specific time when a sensor change in state for any of the sensors"

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Welcome to the Arduino forum. Do you have the lights installed? Do you have power for the lights? The only function for your Arduino is just to turn on and off when you want. You can't power the light from an Arduino.

There are 4 stalls and they each have a sensor and a light that is ether green or red depending on the state of the sensor.

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

  • Do you know how to make a TIMER based on the millis( ) function ?

  • flesh out your required timing needs.

Brad_Roundhouse_Wiring_Design.pdf (23.0 KB)This is my wiring schematic i drew up. I am using 2 Nanos so i can have 7 sensors and 14 digital outputs

Firstly, you only need one output for each LED pair. Wire the output to one LED, then resistor to +, same output to other LED to resistor to GND. When the output is high, one LED will be on, when the output is low, the other LED will be on. That small change lets you run 14 LEDs on seven sensors. Now, add in your timed light function on the remaining outputs. Forget the second Arduino. If at some point you want both LEDs off, just set the pin as an INPUT.

1 Like

How do 7 sets of sensors/LEDs fit into 4 stalls?

Brad_Roundhouse_Wiring_Design.pdf (23.0 KB)
I have separate power for the leds and just need a step to turn them on.

Thanks for any help you can be

Hello bradm2322

What type of LDR sensors are used?

Post data sheet.

Infared
image

I was thinking your locomotives had head lights and went into the stalls forward, therefore LDR made sense to me.

How are the 7 sensors distributed among 4 stalls?

Hello bradm2322

Post a picture of the roundhouse garage of the Model Railroad.

2 Likes

There are seven stalls 4 are controlled by one arduino and 3 by another.

I see the problem...

1 Like

Lol, actually there is a unfortunate rib on the table that lines up there so i couldn't drill there.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.