Go Down

Topic: Help with if, then, else statements... (Read 1 time) previous topic - next topic

Valalvax

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


Code: [Select]
  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*

Code: [Select]
    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

Code: [Select]
  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...

wildbill

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

PaulS

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.

Valalvax

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

PaulS

Quote
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?

Valalvax


Quote
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

KirAsh4

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?
Code: [Select]
(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.

Everseeker

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....

KirAsh4

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

Valalvax

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 :/

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)

KirAsh4

Might I suggest going back to the Reference page http://arduino.cc/en/Reference/HomePage and reading up on the "Boolean Operators" section?

Valalvax


Might I suggest going back to the Reference page http://arduino.cc/en/Reference/HomePage and reading up on the "Boolean Operators" section?


Pffft...

>.>

<.<

>.>

Valalvax

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


Code: [Select]
/* 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);
}



Nick Gammon


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


Indeed. This:

Code: [Select]
void RedLight() {
  digitalWrite(Red2, digitalRead(Green) || digitalRead(Yellow));
  digitalWrite(Red, digitalRead(Green2) || digitalRead(Yellow2));
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

westfw

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.)

Go Up