Pages: [1] 2   Go Down
Author Topic: Help with if, then, else statements...  (Read 1590 times)
0 Members and 1 Guest are viewing this topic.
Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
(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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pffft...

>.>

<.<

>.>
Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Indeed. This:

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6632
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: