IF statements

Good day, forgive me if this is a rather simple question, but I'm fairly new to writing Arduino Code.

First I'm using an Arduino Uno R3, with a RGB LED, and an HRLV-MaxSonar-EZ1. I'm trying to setup my code with the following conditions: range reading less than 350 the LED is red, range readings between 351 and 600 the LED is green, and anything past 601 turns the LED blue. The code works properly for the red and green, but when there are range readings past 600, I still have the green in the LED. Below is my code.

const int pwPin1 = 3;
int led1 = 9;
int led2 = 10;
int led3 = 11;

long sensor1;

void setup () {
Serial.begin(9600);
pinMode(pwPin1, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}

void read_sensor(){
sensor1 = pulseIn(pwPin1, HIGH);
}

void red_warning(){
if (sensor1 < 350){
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH); //turn on red LED
delay(10); //wait for 10 ms
digitalWrite(led1, LOW);
pinMode(led1, INPUT);
}
}

void green_warning(){
if (sensor1 < 600, sensor1 > 351){
pinMode(led2, OUTPUT);
digitalWrite(led2, HIGH); //turn on blue LED
delay(10); //wait for 10 ms
digitalWrite(led2, LOW);
pinMode(led2, INPUT);
}
}

void blue_warning(){
if (sensor1 > 601){
pinMode(led3, OUTPUT);
digitalWrite(led3, HIGH); //turn on green LED
delay(100);
digitalWrite(led3, LOW);
pinMode(led3, INPUT);
}
}

void loop () {
read_sensor();
blue_warning();
green_warning();
red_warning();
printall();
}

void printall(){
Serial.print("S1");
Serial.print(" = ");
Serial.print(sensor1);
Serial.println();
delay(100);
}

Read the how to use this forum sticky and post that code correctly.

if (sensor1 < 600, sensor1 > 351)
That is not the way to use an if statement
To get a range between 352 and 599 use:-
if (sensor1 < 600 && sensor1 > 351)

Grumpy thank you for the assistance I will give that a try!

Have a merry Christmas

Grumpy_Mike:
Read the how to use this forum sticky and post that code correctly.

Mike, I've always wondered about that comment about using the # for the code tags and other stuff. For the reason that I've never seen it in my browser so I end up typing it in by hand. Thus I took a screen shot of where those icon buttons should be, but aren't on my browsers. I'm not running Windows so maybe it's a Windows specific thing.

This might explain why some users don't use the code tags.

Mel

Forum Stickies.png

I'm surprised the comma even compiled? What is the meaning of that? I had to put it in my own environment to check, but yeah, the comma in the if statement does compile. In 20 years of doing C I don't think I've ever seen that!

In this section...

void loop () {
  read_sensor();
  blue_warning();
  green_warning();
  red_warning();
  printall();
}

This is a little weird - it's written as if all the warnings could happen at the same time, but they can't - by calling all the functions and always testing all three conditions, it's a performance issue. When you have three mutually exclusive conditions (and that can be proven) then you should consider them all together. Like this...

void loop () {
  read_sensor();

  if (sensor1 < 350){
    red();
  } 
  else if (sensor1 < 601) {
    green();
  } 
  else {
    blue();
  }

  printall();
}

Then, you need to write your functions red(), green() and blue() to turn on the proper LED, and turn the other ones off, and remove the delay. Note how the IF structure above works - in the first block, it runs the red() function if readings are less than 350. In the second block, we already know it's greater than 350 so we don't test that again, only test if it's under 601, and if that fails, we know the value is greater than 601 so we don't have to test it again.

In 20 years of doing C I don't think I've ever seen that!

You've never seen int a = 1, b= 5; ?

AWOL:

In 20 years of doing C I don't think I've ever seen that!

You've never seen int a = 1, b= 5; ?

Not in the condition of the IF statement. And, in this case it's two boolean expressions, not an assignment. Assignments return the left-most value, so would a list of conditions return the left-most condition's evaluated value?

so would a list of conditions return the left-most condition's evaluated value?

No, the comma operator returns the value of the right-most expression.

AWOL:

so would a list of conditions return the left-most condition's evaluated value?

No, the comma operator returns the value of the right-most expression.

OK that explains the behavior he was seeing. It would discard the first result, essentially branching on the second condition only.

wabbitguy:
Mike, I've always wondered about that comment about using the # for the code tags and other stuff. For the reason that I've never seen it in my browser so I end up typing it in by hand.

What browser is that screenshot from? I have used Windows, Mac and Linux and always see the same window with the icons and similies.
Dump attached.

As to the subject of the thread:-
It seems like there is all sorts of strange syntax with C crawling out of the woodwork this week. However if someone complains that an odd ball syntax ( even if it is in the standard ) gives trouble then the first resort always has to be to use conventional syntax.

Grumpy_Mike:
What browser is that screenshot from?

If you turn off javascript that's what it would look like in almost any browser. If people want to do that I suppose they can, but it's a horrible way to experience the web these days. Almost every site uses some javascript for something, and it's not the security issue it used to be.

Grumpy_Mike:

wabbitguy:
Mike, I've always wondered about that comment about using the # for the code tags and other stuff. For the reason that I've never seen it in my browser so I end up typing it in by hand.

What browser is that screenshot from? I have used Windows, Mac and Linux and always see the same window with the icons and similies.
Dump attached.

That's a screen shot of Safari 7.0.1 (9537.73.11), Mavericks 10.9.1, Mac Pro tower (same thing on my MacBook Pro though). All Javascript is enabled in the browsers. Not a big deal for me, I've been toying in HTML for years so I just type in the stuff, I just wondered what the comments were about since I've never seen anything remotely resembling buttons/icons or smilies.

Mel

jasmine2501:
Almost every site uses some javascript for something, and it's not the security issue it used to be.

Oh, I wouldn't bet on that!

Fact is, between NoScript and Adblock Plus, the Web is much cleaner.

Paul__B:

jasmine2501:
Almost every site uses some javascript for something, and it's not the security issue it used to be.

Oh, I wouldn't bet on that!

Fact is, between NoScript and Adblock Plus, the Web is much cleaner.

As a web developer well versed in JQuery and HTML5, CSS3 and all that hoodoo... I hate users like you. Merry Christmas! :slight_smile:

(I promise I won't JavaScriptHax you, honest)

like stated, I'm very very new to Arduino programming. I can write very basic scripts (reading sensor data and triggering the sensor at set intervals). Beyond that, i am new.

I am noting this information to make my code better. I appreciate all the help. I hope you all had a merry christmas