Sensor & Signal light

Hi @ all,
I'm new to the community and just started.
I like to create a sketch but would need help. The following I like to achieve:

I have a signal light with red and green (a lit green is the default state) and four sensors. Whenever one or more sensors are at high I like to switch the light from green to red. When all sensors return to low a time delay should start for 10 sec. until the signal light goes from red back to default state green. Below is the sketch I have so far but I'm a total beginner. The funny thing is it worked one time when I set an input at high but since then nothing. The sensor gives 5V to the Arduino input, I hope I have not fried the input.

#define SENSORONE 2
#define SENSORTWO 3
#define SENSORTHREE 4
#define SENSORFOUR 5
#define LEDRED 6
#define LEDGREEN 7

void setup() {
  // put your setup code here, to run once:
  pinMode ( SENSORONE, INPUT );
  pinMode ( SENSORTWO, INPUT );
  pinMode ( SENSORTHREE, INPUT );
  pinMode ( SENSORFOUR, INPUT );
  pinMode ( LEDRED, OUTPUT );
  pinMode ( LEDGREEN, OUTPUT );
  digitalWrite ( LEDGREEN , HIGH );
  digitalWrite ( LEDRED , LOW );
  
}

void loop() {
  // put your main code here, to run repeatedly:
  if ( digitalRead ( SENSORONE ) == HIGH )
  if ( digitalRead ( SENSORTWO ) == HIGH )
  if ( digitalRead ( SENSORTHREE ) == HIGH )
  if ( digitalRead ( SENSORFOUR ) == HIGH )
  {
   digitalWrite ( LEDGREEN, LOW );
   digitalWrite ( LEDRED, HIGH );
   delay ( 10000 );
   digitalWrite ( LEDGREEN, HIGH );
   digitalWrite ( LEDRED, LOW );
  }


}

Any help to get to my goal is really appreciated.

Thanks

Hi and welcome to the Arduino forum!

What sensors are attached?

Edit: Points for providing the sketch as code, it makes everything much easier. :+1:

Great, many thanks - that did the trick

#define SENSORONE 2
#define SENSORTWO 3
#define SENSORTHREE 4
#define SENSORFOUR 5
#define LEDRED 6
#define LEDGREEN 7

void setup() {
  // put your setup code here, to run once:
  pinMode ( SENSORONE, INPUT );
  pinMode ( SENSORTWO, INPUT );
  pinMode ( SENSORTHREE, INPUT );
  pinMode ( SENSORFOUR, INPUT );
  pinMode ( LEDRED, OUTPUT );
  pinMode ( LEDGREEN, OUTPUT );
  digitalWrite ( LEDGREEN , HIGH );
  digitalWrite ( LEDRED , LOW );
  
}

void loop() {
  // put your main code here, to run repeatedly:
  if ( digitalRead ( SENSORONE ) == HIGH || ( digitalRead ( SENSORTWO ) == HIGH ) || ( digitalRead ( SENSORTHREE ) == HIGH ) || ( digitalRead ( SENSORFOUR ) == HIGH ))
  {
   digitalWrite ( LEDGREEN, LOW );
   digitalWrite ( LEDRED, HIGH );
   delay ( 10000 );
   digitalWrite ( LEDGREEN, HIGH );
   digitalWrite ( LEDRED, LOW );
  }


}

ledsyn
The sensors are a small circuit with an IR diode with receiver, powered with 12VDC. Unfortunately they are NPN. So since I know that my sketch works I have to play around to get the sensors work on the inputs, maybe try the pull_up feature on pin mode. If this will not work I have to add resistors on my own. The goal ist to power the whole circuit with 12VDC.
Also, the LED's lights are switched to ground, so another challenge for a Newbie. :wink:

1 Like

What's the part number for the sensors?

PULL_UP could work.

Arduino outputs can source both Vcc and GND, so any way you wire the LEDs will work.

Being a Newbie is ok, as long as you're humble and willing to listen, and you're doing fine in that apartment so far.

a little less tedious
configure input with internal pullups
defines LED output states (on/off)

const byte PinSensor [] = { 2, 3, 4, 5 };
const byte LEDRED    = 6;
const byte LEDGREEN  = 7;

#define TimeOut  10000

const int Nsensor = sizeof(PinSensor);
enum { Red = 0, Grn = 1 };
enum { Off = HIGH, On = LOW };

void setup ()
{
    for (int n = 0; n < Nsensor; n++)
        pinMode (PinSensor [n], INPUT_PULLUP);

    pinMode (LEDGREEN, OUTPUT );
    pinMode (LEDRED, OUTPUT );
    digitalWrite (LEDGREEN, On);
    digitalWrite (LEDRED,   Off);
}


void loop()
{
    for (int n = 0; n < Nsensor; n++)  {
        if (LOW == digitalRead (PinSensor [n])) {  // active
            digitalWrite ( LEDGREEN, Off);
            digitalWrite ( LEDRED,   On);
            delay (TimeOut );
            digitalWrite ( LEDGREEN, On);
            digitalWrite ( LEDRED,   Off);
        }
    }
}
2 Likes

Of course you can. It is syntactically valid, if a bit of an unusual way to accomplish something.

In this case, it results in the logical AND function of the separate tests, so it is not what you want to do.

  if   ( digitalRead ( SENSORONE ) == HIGH )
    && ( digitalRead ( SENSORTWO ) == HIGH )
    && ( digitalRead ( SENSORTHREE ) == HIGH )
    && ( digitalRead ( SENSORFOUR ) == HIGH ) )
  {
   digitalWrite ( LEDGREEN, LOW );
   digitalWrite ( LEDRED, HIGH );
   delay ( 10000 );
   digitalWrite ( LEDGREEN, HIGH );
   digitalWrite ( LEDRED, LOW );
  }

There may be some subtle difference in whether the conditions get evaluated at all in executing this statement, I am too lazy to work through that. In this particular case it makes no difference whether the digitalRead()s happen or not.

a7

You know what @Delta_G means :wink: The whole point of @Delta_G's reply is that OP needs a logical OR, not a logical AND.

1 Like

That was uncalled for. Take it as a grown-up that people sometimes disagree or have different views.

You said you can't. You were not correct. Either you sincerely thought such statement would be in error, or you withheld the truth to protect the innocent. Or something in between. Perhaps you were speaking informally, hard to tell.

I didn't say one should write like that, or that it is good code or that anyone wants to read it.

You truncated the quote, leaving off

 if a bit of an unusual way to accomplish something.

People are here to learn. I didn't want anyone to "learn" that there are statements that are not usable as the body part of an if statement.

That's all.

a7

Up to you.

Every person has different and lifelong opportunities to learn something new. And learning is not a black and white process but a growth process of useless and useful knowledge.

1 Like

I thought the same as @alto777 first, but then "nah, @Delta_G knows this" and it turns out he was right all the time since OP wanted OR not AND.

Regarding syntax, well, this isn't some workplace where we have to adhere to standard practices.

The IDE DO accept both ways, so .. can we move along now? Please?

1 Like

I did not.

a7

Please tell me the signals from these sensors are not wired directly to your Arduino inputs. You say they're NPN, do you mean they are open-collector NPN, in which case it's perfectly valid to use an input pullup to pull the signal up to 5V, as long as the detector circuit's common is tied to the Arduino common.

1 Like

Hi ledsyn,
about the sensors, I got them from Amazon, here's the link: TCRT5000 Infrared Reflective Sensor

We all should practice more tolerance, understanding and respect for each other and it all starts is with the man in the mirror. :wink:

2 Likes

gcjr,
I understand, this is less clutter.
Multiple ways lead to a goal.

Thank you very much.

Currently I have not connected the sensors the way I like to since i don't want to fry the Arduino board. When I tested the sensor output I noticed that they switch to 0V. First I assumed I get a positive signal to my test led but it didn't work. So I switched Anode and Cathode in my breadboard around and applied 5V over a resistor to the LED and this time it lit up. So my understand of this is that the load is switch to negative what would mean NPN, right?

The way I like to do this is to power everything from the same 12VDC power supply. The sensor board have the same potential as the Arduino Board and the LED's. I looked it up that I can use either a resistor connected to the 5V pin of the Arduino and the pin I use for the sensor or the pull_up feature of the Arduino. I will try it with the pull_up feature and see if it works that way.

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

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

1 Like

You're new here, I get that; welcome! Unfortunately, a paragraph to describe your circuit is not really very helpful, as the best communication tool for showing circuit topology is a schematic. Please provide a sketch, with thorough labeling, showing your sensor and it's wiring to your Arduino, so we don't have to guess at, or play 20 questions with, the missing details; I think that what you're proposing is possibly acceptable, but...
Thanks!

1 Like

Tom,
thank you but the question I had was already answered quite awhile ago. I used if instead of or and the feature "pull_up" took care of the rest.