Help with if, then, else statements...

I'm trying to do a if, then, else statement that checks if a pin is high, but hell, I don't know, seems simple but it just doesn't work

The unincluded code right now is simplified to just turn on Green, delay, turn off Green turn on yellow, delay, turn off yellow turn on green

  if (Green == HIGH)
    digitalWrite(Red2, HIGH);
  else if (Yellow == HIGH)
    digitalWrite(Red2, HIGH); 
    else digitalWrite(Red2, LOW);

Here's what my first attempt was, this compiled, but Red2 never lit.. shrug

    if (digitalRead(Green, HIGH) == true)
    digitalWrite(Red2, HIGH);
  else if (digitalRead(Yellow, HIGH) == true)
    digitalWrite(Red2, HIGH); 
    else digitalWrite(Red2, LOW);

I thought of this about 15 minutes ago, but it doesn't compile

  if (digitalRead(Green) == HIGH)
    digitalWrite(Red2, HIGH);
  else if (digitalRead(Yellow) == HIGH)
    digitalWrite(Red2, HIGH);
  else digitalWrite(Red2, LOW);

As I was typing this I tried this one. And, it compiles, but... nothing

I'm probably missing something obvious, haven't played with Arduino in a couple weeks...

You'll really need to post the whole thing for anyone to have a good chance to help. Are your pinmode commands correct?

What is connected to the pins you are reading from? If there are switches, do you have the required external pull-down resistors? It's clear from the code that you aren't using the internal pull-up resistors or external pull-up resistors. That leaves only two choices - external pull-down or none. And none won't work.

The digitalRead() function does not take two arguments, so it seems highly unlikely that the first code snippet with digitalRead() actually compiled.

No, it didn't... Can I digitalRead an output pin? shrug they're all LEDs, by the way

Can I digitalRead an output pin?

Can you re-read what you wrote to an output pin? Yes, but why? Can't you simply remember what you wrote?

PaulS:

Can I digitalRead an output pin?

Can you re-read what you wrote to an output pin? Yes, but why? Can't you simply remember what you wrote?

Well I thought it'd be that simple, buy obviously I'm missing something, probably something obvious

Basically all I'm trying to do is

If output A OR output B are high, turn on output C, else output C is low

Valalvax:
Basically all I'm trying to do is

If output A OR output B are high, turn on output C, else output C is low

So why not do exactly that?

(untested code)
if ((digitalRead(Green) == HIGH) || (digitalRead(Yellow) == HIGH)) {
  digitalWrite(Red2, HIGH);
} else {
  digitalWrite(Red2, LOW);
}

This tests both Green and Yellow, and if either of them is HIGH, it will light up Red2 as well. And in your case, you will end up with either Green and Red2 or Yellow and Red2, or all three sitting HIGH at some point. Red2 will never go LOW unless you bring both Green and Yellow LOW as well.

FWIW, I think you are overthinking your reply...
look at his code...

not a { or } in sight.... THAT looks like it was the main issue, not the logic part....

No, it can be done without the braces. I just prefer to use braces.

I take it || = or?

So much easier than using else if, and probably will give me less problems

That's the problem with selflearning, you might not come across something like that :confused:

Thanks dude <3

(as a side note: I originally had the braces but after trying four or five different ways some of my code had them, some of it didn't, and some of them were in just the wrong place so rather than fix it before copying to the forums, I removed them, because functionally it's the same code with or without (obviously there are times where it's necessary)

Might I suggest going back to the Reference page Arduino - Home and reading up on the "Boolean Operators" section?

KirAsh4:
Might I suggest going back to the Reference page Arduino - Home and reading up on the "Boolean Operators" section?

Pffft...

.>

<.<

.>

Woot, got it working, only thing is, there's an easier way, I know it

/* This Sketch is a basic Red Light Circuit
 
 Timers:
 1400 ms on Red
 1000 ms on Green
 400 ms on Yellow
 */

int Green = 7;   // Lit during Green, Yellow, Red2 portion
int Yellow = 6;
int Red = 5;
int Green2 = 10;  // Lit during Green2, Yellow2, Red portion
int Yellow2 = 9;
int Red2 = 8;

void setup() {
  pinMode(Green, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Red2, OUTPUT);
  pinMode(Green2, OUTPUT);
  pinMode(Yellow2, OUTPUT);
  pinMode(Red, OUTPUT);
}

/* Checks the state of the Green and Yellow LEDs
   When a green or yellow LED is lit, light the opposite Red LED */

void RedLight() {
  if ((digitalRead(Green) == HIGH) || (digitalRead(Yellow) == HIGH)) {
    digitalWrite(Red2, HIGH);
  }
  else
    digitalWrite(Red2, LOW);
  
  if ((digitalRead(Green2) == HIGH) || (digitalRead(Yellow2) == HIGH)) 
    digitalWrite(Red, HIGH);
  else
    digitalWrite(Red, LOW);
}

void loop() {
  digitalWrite(Yellow2, LOW);
  digitalWrite(Green, HIGH);
  RedLight();  // Runs the RedLight function.
  delay(1000);
  digitalWrite(Green, LOW);
  digitalWrite(Yellow, HIGH);
  RedLight();
  delay(400);
  digitalWrite(Yellow, LOW);
  digitalWrite(Green2, HIGH);
  RedLight();
  delay(1000);
  digitalWrite(Green2, LOW);
  digitalWrite(Yellow2, HIGH);
  RedLight();
  delay(400);
}

Valalvax:
Woot, got it working, only thing is, there's an easier way, I know it

Indeed. This:

void RedLight() {
  digitalWrite(Red2, digitalRead(Green) || digitalRead(Yellow));
  digitalWrite(Red, digitalRead(Green2) || digitalRead(Yellow2));
}

I recommend ALWAYS using braces on if statements. It's a big help in not shooting yourself in the foot, and is part of many "official" coding standards. (a "coding standard" is the list of things you should do in the code you write that are beyond the requirements of the language itself. For even moderate sized companies, it is important that the multiple people writing code remain consistent with one another, in addition to any real or perceived technical advantages of a particular style. This will cover how identifiers are capitalized, the amount of indentation to use and where it goes, when (if ever) you can omit braces, and a bunch of other stuff.)

Thanks, it's a habit I'll have to force myself into...

Nick, that's awesome, didn't know I could do that, thanks (just so I'm sure I understand, it's just setting Red to the same state as Yellow2 or Green2 right?)

Specifically I was wondering if it was possible at all to loop the RedLight() function independently of the Loop() function so that I wouldn't have to call it at all? (maybe I got all the terminology right, terminology has ALWAYS been my weak point, no matter what the subject)

Valalvax:
Nick, that's awesome, didn't know I could do that, thanks (just so I'm sure I understand, it's just setting Red to the same state as Yellow2 or Green2 right?)

Yes, exactly. The || is the "logical or" as opposed to "bitwise or". The difference might be that great in this particular case. So if either Yellow2 or Green2 is on that will be a 1, which is then sent to digitalWrite.

Valalvax:
Specifically I was wondering if it was possible at all to loop the RedLight() function independently of the Loop() function so that I wouldn't have to call it at all?

Hmmm - the code isn't too bad now. You could do it with interrupts or timers, but I think that is adding needless complexity.

What you might do is make your own glue function that turns on or off a light and does the red light. Eg.

void SwitchLight (const byte which, const byte val)
{
digitalWrite(which, val);
RedLight ();
}

Now you could do:

SwitchLight(Yellow2, LOW);
SwitchLight(Green, HIGH);

... and the SwitchLight function has the side-effect of setting the red light correctly. This actually makes more calls to RedLight than are really needed but makes sure that the red light is correct whenever you change the other ones.

Thanks for the help thinks of a new project to do with his parts