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 );
}
}
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.
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.
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.
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.
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.
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.
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!