Georgia, US
Offline
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« on: March 15, 2011, 05:56:51 am » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2354
|
 |
« Reply #1 on: March 15, 2011, 06:15:57 am » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #2 on: March 15, 2011, 06:42:58 am » |
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
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« Reply #3 on: March 15, 2011, 07:27:56 am » |
No, it didn't... Can I digitalRead an output pin? *shrug* they're all LEDs, by the way
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #4 on: March 15, 2011, 07:30:51 am » |
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
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« Reply #5 on: March 15, 2011, 10:54:29 am » |
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
Edison Member
Karma: 41
Posts: 1263
Reviving dead brain cells with Arduinos.
|
 |
« Reply #6 on: March 15, 2011, 11:15:18 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #7 on: March 15, 2011, 11:36:31 am » |
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
Edison Member
Karma: 41
Posts: 1263
Reviving dead brain cells with Arduinos.
|
 |
« Reply #8 on: March 15, 2011, 11:58:02 am » |
No, it can be done without the braces. I just prefer to use braces.
|
|
|
|
|
Logged
|
|
|
|
|
Georgia, US
Offline
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« Reply #9 on: March 15, 2011, 12:49:32 pm » |
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
Edison Member
Karma: 41
Posts: 1263
Reviving dead brain cells with Arduinos.
|
 |
« Reply #10 on: March 15, 2011, 01:08:52 pm » |
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
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« Reply #11 on: March 15, 2011, 04:51:21 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Georgia, US
Offline
Sr. Member
Karma: 4
Posts: 372
Arduino makes my head hurt :(
|
 |
« Reply #12 on: March 17, 2011, 09:01:06 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #13 on: March 18, 2011, 12:20:05 am » |
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)); }
|
|
|
|
|
Logged
|
|
|
|
|
SF Bay Area (USA)
Offline
Faraday Member
Karma: 78
Posts: 5455
Strongly opinionated, but not official!
|
 |
« Reply #14 on: March 18, 2011, 12:25:24 am » |
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
|
|
|
|
|
|